Write a recursive program that prints the steps for solving “reverse” Tower of Hanoi. The rules are explained using the figure below, As depicted in the above figure, the pegs are upside down and the disks are held at the top using magnets. • The order of disks in a tower is the same: smallest disk at the top, largest disk at the bottom. • The movement of disks is reversed: you can only move the largest disk from the bottom of one tower to the bottom of a smaller disk in another tower (or onto an empty peg). • You will implement the reverseToH(...) method. Do not change the signature for this method. This method needs to be recursive. The listing below shows the output that your program needs to print (Lines 6–12 below) for the sample input given in Run.java (Lines 1–4 below). The first three steps are depicted in the above figure. 1 Task1 t1 = new Task1(); 2 int size = 3; 3 t1. setTowerSize ( size ); 4 t1. reverseToH( size , 1, 3); 5 6 Move Disk 3 from Peg 1 to Peg 3 7 Move Disk 2 from Peg 1 to Peg 2 8 Move Disk 3 from Peg 3 to Peg 2 9 Move Disk 1 from Peg 1 to Peg 3 10 Move Disk 3 from Peg 2 to Peg 1 11 Move Disk 2 from Peg 2 to Peg 3 12 Move Disk 3 from Peg 1 to Peg 3
Write a recursive program that prints the steps for solving “reverse” Tower of Hanoi. The rules are explained using the figure below, As depicted in the above figure, the pegs are upside down and the disks are held at the top using
magnets.
• The order of disks in a tower is the same: smallest disk at the top, largest disk at the bottom.
• The movement of disks is reversed: you can only move the largest disk from the bottom of one
tower to the bottom of a smaller disk in another tower (or onto an empty peg).
• You will implement the reverseToH(...) method. Do not change the signature for this method.
This method needs to be recursive.
The listing below shows the output that your program needs to print (Lines 6–12 below) for the sample
input given in Run.java (Lines 1–4 below). The first three steps are depicted in the above figure.
1 Task1 t1 = new Task1();
2 int size = 3;
3 t1. setTowerSize ( size );
4 t1. reverseToH( size , 1, 3);
5
6 Move Disk 3 from Peg 1 to Peg 3
7 Move Disk 2 from Peg 1 to Peg 2
8 Move Disk 3 from Peg 3 to Peg 2
9 Move Disk 1 from Peg 1 to Peg 3
10 Move Disk 3 from Peg 2 to Peg 1
11 Move Disk 2 from Peg 2 to Peg 3
12 Move Disk 3 from Peg 1 to Peg 3
Unlock instant AI solutions
Tap the button
to generate a solution