Thursday, April 24, 2008

Better "Tower of Hanoi"

public class Hanoi {
public static void main(String [] args)
{
runHanoi(3,'A', 'B', 'C');
}

public static void runHanoi(int n, char x, char y, char z)
{
if(n==1)
System.out.println("Move top disk from peg " + x
+ " to peg " + z);
else
{
runHanoi(n-1,x,z,y);
runHanoi(1,x,y,z);
runHanoi(n-1,y,x,z);
}
}
}

No comments: