Question:
Hardest Computer Science problem ever, 10 pts?
murray
2012-11-22 10:49:36 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.

A good way to represent the tower problem is with a list or array of values where each value is the name of a tower. Suppose you call the three towers A, B, and C. Then the string BAACBC could represent instructions for building a configuration. If we place disks going from the largest to the smallest, then the string above means:

put the biggest disk on tower B
put the next smaller disk on tower A
put the next smaller disk on tower A
put the next smaller disk on tower C
put the next smaller disk on tower B
put the next smaller disk on tower C


This representation has the advantage that it cannot represent illegal configurations, where a bigger disk is over a smaller one. It also makes it easy to recursively decompose the problem. Suppose we want to transform one configuration into another, represented by a method call:

transform("BAACBC", "BCBABC")

meaning to transform the configuration represented by BAACBC into the configuration represented by BCBABC

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();
}

}

NOTE::

The problem is not to move from one tower to another, but to transform one initial configuration example: tower A, tower B, tower C, user defines initial configuration as tower A has 2 disks, tower B has 3 disks, tower C has 2 disks and the final configuration tower A has 3 disks, tower B has 1 disk, tower c has 3 disks. how would you transform an initial configuration to another. It is not the simple "moving from one tower A to tower C" problem...
Three answers:
Ciaron
2012-11-22 11:37:43 UTC
The towers of Hanoi is one of the classic programs there are two ways iteratively or recursively one was to solving the puzzle is to recognize that it can be solved by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached. This is the old Java code I had for a very simple version of the problem



import java.io.*;

public class hanoiA

{

static int moves=0; //number of moves so far

static int getInt()

{

String line;

BufferedReader in =

new BufferedReader(new InputStreamReader(System.in));

try

{

line = in.readLine();

int i = Integer.valueOf(line).intValue();

return i;

}

catch (Exception e)

{

System.err.println("Error: Input is not a number.\n" +

"Input assumed to be the number 1");

return 1;

}

}



static void hanoi(int height,

char fromPole,

char toPole,

char withPole)

{

if (height >= 1)

{

hanoi(height-1, fromPole, withPole, toPole);

moveDisk(fromPole, toPole);

hanoi(height-1, withPole, toPole, fromPole);

}

}



static void moveDisk(char fromPole, char toPole)

{

moves++;

System.out.print(fromPole);

System.out.print(toPole);

System.out.print(((moves % 20)==0) ? '\n' : ' ');

}



public static void main(String[] args)

{



int TowerHeight;

char FromPole='A', ToPole='B', WithPole='C';



System.out.println("Enter Tower height...");

System.out.print("?");

TowerHeight = getInt();



hanoi(TowerHeight, FromPole, ToPole, WithPole);



System.out.println();



}



}

This code should give you a few ideas on how to approach the problem.
2012-11-22 13:20:16 UTC
Another poster already caught your NOTE in an earlier duplicate question from you. You've added no code since then.



If you need to move disk from one pin to another, then you MUST have disks in a single stack on the only remaining pin (disks ... having already been placed in their final positions.) It's trivial to write code to achieve this.
KaitoX
2012-11-22 11:21:56 UTC
You have been given plenty of information the first dozen times you asked this. Why not take some time to actually study the material rather than trying to force someone to do it for you?


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