from one rod to a nother, following three simple rules: 1. Only one disk c a n be moved a t a time, 2. A l a rger disk c a nnot be pl a ced on top of a sm a ller one. 7
to_rod, char aux_rod) { if (n == 0) return; towerOfHanoi(n - 1, from_rod, aux_rod, to_rod); System.out.println("Move disk " + n + " from rod “ + from_rod + " to rod “ + to_rod); towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); } public static void main(String args[]) { int N = 3; // A, B, and C are the names of the rods towerOfHanoi(N, 'A', 'C', 'B'); } } 10
[email protected] Summer 2024 Copyright. These slides can only be used as study material for the class CSC305 at Cal Poly. They cannot be distributed or used for another purpose.