// Author : Apostolos Syropoulos import java.io.*; public class hanoiE1 { 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, int Pole, int dir) { if (height >= 1) { hanoi(height-1, Pole, Opp(dir)); moveDisk(height, Pole, dir); hanoi(height-1, Neigh(Pole, Opp(dir)), Opp(dir)); } } static void moveDisk(int disk, int fromPole, int dir) { char fromPeg = PoleName(fromPole), toPeg = PoleName(Neigh(fromPole, dir)); moves++; System.out.print(fromPeg); System.out.print(toPeg); System.out.print(((moves % 20)==0) ? '\n' : ' '); } static int Opp(int dir) { return (dir == 1) ? 0 : 1; } static int Neigh(int Pole, int dir) { return (Pole + dir) % 3 +1; } static char PoleName(int pole) { switch (pole) { case 1: return 'A'; case 2: return 'B'; case 3: return 'C'; } return 'E'; } public static void main(String[] args) { long time1, time2; //for benchmarking int TowerHeight; int FromPole = 1, Dir = 0; System.out.println("Enter Tower height..."); System.out.print("?"); TowerHeight = getInt(); time1 = System.currentTimeMillis(); hanoi(TowerHeight, FromPole, Dir); time2 = System.currentTimeMillis(); System.out.println(); System.out.print(time2-time1); //print execution time in msec System.out.println(" msec execution time"); } }