June 2017
Beginner
1296 pages
69h 23m
English
... peg 1 as a temporary holding area.
The process ends when the last task involves moving disk (i.e., the base case). This task is accomplished by moving the disk, without using a temporary holding area.
In Fig. 18.11, method solveTowers (lines 5–22) solves the Towers of Hanoi, given the total number of disks (in this case 3), the starting peg, the ending peg, and the temporary holding peg as parameters.
1 // Fig. 18.11: TowersOfHanoi.java
2 // Towers of Hanoi solution with a recursive method.
3 public class TowersOfHanoi {
4 // recursively move disks between towers
5 public static void solveTowers(int disks, int sourcePeg,
6 int destinationPeg, int tempPeg) {
7 // base case -- only one disk to move
8 if (disks == 1) {
9 System.out.printf( ...