package com.charles.algorithm; public class Hanoi { private String loc; private int num; public Hanoi() { } public Hanoi(Hanoi h) { this.num = h.num; this.loc = h.loc; } public Hanoi(String loc, int num) { this.loc = loc; this.num = num; } public static void main(String[] args) { final int INT_NUM = 3; Hanoi A = new Hanoi("A", INT_NUM), B = new Hanoi("B", 0), C = new Hanoi("C", 0); int sum = A.num; new Hanoi().hanoi(sum, A, B, C); System.out.println(A); System.out.println(B); System.out.println(C); } public void hanoi(int sum, Hanoi A, Hanoi B, Hanoi C) { if (1 == sum) { System.out.println(A.loc + " -------> " + C.loc); A.num--; C.num++; } else { hanoi(sum-1, A, C, B); hanoi(1, A, B, C); hanoi(sum-1, B, A, C); } } public void hanoi(int num, String A, String B, String C) { if (1 == num) { System.out.println(A + " -------> " + C); } else { hanoi(num - 1, A, C, B); hanoi(1, A, B, C); hanoi(num - 1, B, A, C); } } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } @Override public String toString() { return "Hanoi [loc=" + loc + ", num=" + num + "]"; } }