Question:
Hardest Computer Science problem ever, 10 pts?
murray
2012-11-21 17:35:58 UTC
Code a recursive solution to tower of hanoi where there are three towers A, B and C. The user specify the initial configuration and the user specify the final configuration. The rule is the n
disks are all of different sizes. Legal configuration is if the disks are distributed on a tower so that on any tower a larger disk is never on a smaller disk. The program prints out a series of moves to transform the initial configuration to the final configuration.


my code so far:

public class TOfHanois {
char config1[]=new char[10];
char config2[]=new char[10];

public static void move(char config1s[], char config2s[],int ndisks)
{
System.out.println("done");
}






public static void main(String [] args)
{

Scanner scan=new Scanner(System.in);
Scanner scan2=new Scanner(System.in);

int disks;
System.out.println("Please enter the number of disks in both towers: ");
disks=scan.nextInt();

char [] config1 = new char[disks];
config1 = getInput();
//String []letters=new String[disks];
//char input;
//char[] getInput();
///String input1=config1.toString();

char [] config2 = new char[disks];
config2 = getInput();

String s=new String(config1);
String s2=new String(config2);
System.out.println("first char in array is: "+config1[4]);

if(s.compareTo(s2)==0)
{
System.out.println("done");

}
else{
System.out.println("continue");
}


}
static char[] getInput() {
Scanner scan = new Scanner(System.in);
System.out.print("Enter string: ");
String str = scan.nextLine();
return str.toCharArray();
}

}
Four answers:
Jonathan
2012-11-21 22:01:17 UTC
In addition to modulo's post, which nails your approach, I have just a small note to the other posters who imagine the problem is the same as the usual "tower of hanoi."



Take note that the OP writes, "The user specify the initial configuration and the user specify the final configuration." So although the solution has a similar mid-portion, there is an initial set of steps and a final set of steps which must be added, and the midportion doesn't start with an entire tower in all cases... only some.



I'm actually quite happy with whomever assigned this problem, because trying to google up an answer will only yield tens of thousands of answers that aren't useful for this case, without a fair degree of additional thought. One of the posters to this OP provides an algorithm that quite simply will not be able to be applied here, in fact. It presumes a starting and ending case in order to work.



I love teachers who do this to students who don't want to develop and use their creative skills. And I'm also curious about why and how many people posting have failed to actually READ what the OP copies out from the teacher's problem wording.
Dan
2012-11-21 17:51:53 UTC
This might help (here's an algorithm, which assumes that the three towers are numbered 0, 1 and 2). The algorithm says that to move n disks from a source tower to a destination tower, you move n-1 disks to the other tower (not the source or destination), then reposition 1 disk from the source tower to the destination, then move n-1 disks from the other tower to the destination.



So, to "move" 5 disks from tower 0 to tower 2, you "move" 4 disks from tower 0 onto tower 1, then you reposition 1 disk from tower 0 to tower 2, then you "move" 4 disks from tower 1 to tower 2.



Note that "move" is defined in terms of itself (a recursive definition). Also note that if the towers are numbered 0, 1 and 2, then if you add together the numbers of any two towers and subtract that from three, you'll get the number of the remaining tower. For example, if you have towers 0 and 2, then 0+2=2, and 3-2=1 and 1 is the other tower. This algorithm makes use of that fact.



Note that I didn't write it in C - that's your job - this is an algorithm. However, the C is pretty close...



to "move" n disks from tower x to tower y

begin

  if n>0 then

    begin

        "move" n-1 disks from tower x to tower 3-(x+y)

        output "One disk from tower "+x+" to tower "+y

        "move" n-1 disks from tower 3-(x+y) to tower y

    end

end



I predict that your next problem will be to find all the ways to put 8 queens on a chess board so that they can't take each other.
Jared
2012-11-21 17:56:48 UTC
Solving Tower's of Hanoi is an extremely simple problem...that's why I clicked this your question--to see what ridiculously easy problem you thought was "the hardest ever".



Seriously solving the Towers of Hanoi is simple:



Base Case: 1 peg: move it to the other side



Case n: solve the problem by solving the (n - 1) pegs to the middle, move the large peg to the right, then solve the problem of moving the (n - 1) pegs to the right.
modulo_function
2012-11-21 17:55:38 UTC
So, you're going to keep posting this question until someone does it for you, eh?



That's not the best way to learn programming.



As has been mentioned before, there are a number of recursive algorithms available by searching the interwebs. Find one and 'code it up'.



+add

Here's what your problem solving methodology looks like:



public class Murray{



do{

...YA.postProblem();

} while( !YA.solution() );



}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...