Skip to content

Instantly share code, notes, and snippets.

@5pecia1
Last active March 16, 2017 12:23
Show Gist options
  • Save 5pecia1/35abe70c884361b3dfe16a9e06ab8187 to your computer and use it in GitHub Desktop.
Save 5pecia1/35abe70c884361b3dfe16a9e06ab8187 to your computer and use it in GitHub Desktop.
hanoi test
import java.math.BigInteger;
public class Hanoi {
private static final BigInteger ONE = new BigInteger("1");
private static BigInteger DISK_MOVE_COUNTER = new BigInteger("0");
private static int STACK_COUNT = 0;
private static int CURRENT_BIGGEST_DISK = 0;
public static void main(String[] args) {
hanoi(3, "A", "B", "C");
}
private static void hanoi(int disk, String a, String b, String c) {
STACK_COUNT++;
System.out.println("stack count : " + STACK_COUNT);
if (disk <= 0) {
STACK_COUNT--;
System.out.println("stack count : " + STACK_COUNT);
return;
}
hanoi(disk - 1, a, c, b);
DISK_MOVE_COUNTER = DISK_MOVE_COUNTER.add(ONE);
if (CURRENT_BIGGEST_DISK < disk) {
CURRENT_BIGGEST_DISK = disk;
}
System.out.println("[" + DISK_MOVE_COUNTER + "] "
+ " Biggest disk : " + CURRENT_BIGGEST_DISK
+ " Disk : " + disk
+ " sol : " + a + " tmp : " + b + " dest : " + c);
hanoi(disk - 1, b, a, c);
STACK_COUNT--;
System.out.println("stack count : " + STACK_COUNT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment