31 lines
720 B
Java
Raw Normal View History

2025-12-02 06:54:32 +01:00
public class TowerOfHanoiStatic {
public static int moveCount = 0;
public static int hanoi(int n, int from, int to, int aux) {
if (n == 1) {
moveCount = moveCount + 1;
return moveCount;
}
hanoi(n - 1, from, aux, to);
moveCount = moveCount + 1;
hanoi(n - 1, aux, to, from);
return moveCount;
}
public static int main() {
moveCount = 0;
System.out.println(hanoi(1, 1, 3, 2));
moveCount = 0;
System.out.println(hanoi(2, 1, 3, 2));
moveCount = 0;
System.out.println(hanoi(3, 1, 3, 2));
moveCount = 0;
System.out.println(hanoi(4, 1, 3, 2));
return 0;
}
}