murray
2012-11-22 10:49:36 UTC
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...