ハノイの塔
2007/01/02
ハノイの塔
public class Hanoi {
public static void main(String[] args) {
hanoi(16, 'A', 'B', 'C');
System.out.println(cnt);
}
static long cnt = 0;
public static void hanoi(int n, int a, int b, int c){
if(0<n){
hanoi(n - 1, a, c, b);
System.out.format("%c --> %c\n", a,c);
hanoi(n - 1, b, a, c);
cnt++;
}
}
}
public static void main(String[] args) {
hanoi(16, 'A', 'B', 'C');
System.out.println(cnt);
}
static long cnt = 0;
public static void hanoi(int n, int a, int b, int c){
if(0<n){
hanoi(n - 1, a, c, b);
System.out.format("%c --> %c\n", a,c);
hanoi(n - 1, b, a, c);
cnt++;
}
}
}
: