Wednesday, March 23, 2011

class hanoi
{
public static void main (String args[])
{
Integer N = new Integer(3);
H_dohanoi(N.intValue(), 1,3, 2);
System.exit(0);
}

static void H_dohanoi(int n, int from, int to, int via)
{
if (n > 0) {
H_dohanoi(n-1, from, via, to);
H_moveit(from, to);
H_dohanoi(n-1, via, to, from);
}
}

static void H_moveit(int from, int to)
{
System.out.print("move ");
System.out.print(from);
System.out.print(" --> ");
System.out.println(to);
}
}
Run Here https://ideone.com/Qy5nh

No comments :