Java Reference
In-Depth Information
LISTING 12.3
//********************************************************************
// SolveTowers.java Author: Lewis/Loftus
//
// Demonstrates recursion.
//********************************************************************
public class SolveTowers
{
//-----------------------------------------------------------------
// Creates a TowersOfHanoi puzzle and solves it.
//-----------------------------------------------------------------
public static void main (String[] args)
{
TowersOfHanoi towers = new TowersOfHanoi (4);
towers.solve();
}
}
OUTPUT
Move one disk from 1 to 2
Move one disk from 1 to 3
Move one disk from 2 to 3
Move one disk from 1 to 2
Move one disk from 3 to 1
Move one disk from 3 to 2
Move one disk from 1 to 2
Move one disk from 1 to 3
Move one disk from 2 to 3
Move one disk from 2 to 1
Move one disk from 3 to 1
Move one disk from 2 to 3
Move one disk from 1 to 2
Move one disk from 1 to 3
Move one disk from 2 to 3
describing that particular move. If the stack contains more than one disk, we
call moveTower again to get the N −1 disks out of the way, then move the largest
disk, then move the N −1 disks to their final destination with yet another call to
moveTower .
Search WWH ::




Custom Search