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) {
|
2025-12-05 11:16:00 +01:00
|
|
|
TowerOfHanoiStatic.moveCount = TowerOfHanoiStatic.moveCount + 1;
|
|
|
|
|
return TowerOfHanoiStatic.moveCount;
|
2025-12-02 06:54:32 +01:00
|
|
|
}
|
|
|
|
|
hanoi(n - 1, from, aux, to);
|
2025-12-05 11:16:00 +01:00
|
|
|
TowerOfHanoiStatic.moveCount = TowerOfHanoiStatic.moveCount + 1;
|
2025-12-02 06:54:32 +01:00
|
|
|
hanoi(n - 1, aux, to, from);
|
2025-12-05 11:16:00 +01:00
|
|
|
return TowerOfHanoiStatic.moveCount;
|
2025-12-02 06:54:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int main() {
|
2025-12-05 11:16:00 +01:00
|
|
|
TowerOfHanoiStatic.moveCount = 0;
|
2025-12-02 06:54:32 +01:00
|
|
|
System.out.println(hanoi(1, 1, 3, 2));
|
|
|
|
|
|
2025-12-05 11:16:00 +01:00
|
|
|
TowerOfHanoiStatic.moveCount = 0;
|
2025-12-02 06:54:32 +01:00
|
|
|
System.out.println(hanoi(2, 1, 3, 2));
|
|
|
|
|
|
2025-12-05 11:16:00 +01:00
|
|
|
TowerOfHanoiStatic.moveCount = 0;
|
2025-12-02 06:54:32 +01:00
|
|
|
System.out.println(hanoi(3, 1, 3, 2));
|
|
|
|
|
|
2025-12-05 11:16:00 +01:00
|
|
|
TowerOfHanoiStatic.moveCount = 0;
|
2025-12-02 06:54:32 +01:00
|
|
|
System.out.println(hanoi(4, 1, 3, 2));
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|