This should give you enough to get well started.
You can make it more interesting with a random initial allocation of disks,
in which case you find the first disk on C which is out of place (the bad
disk), move everything above the bad disk off it (to A or B - the choice
matters), move the bad disk (to B or A) and then move the correct disk to C.
But that may not be optimal either.
Bill, first things first.
Guys and Ladies let me apologize about how long my writing will be.
I just think there is something really cool about this and I don't want
to miss it.
Ben gave me great insight into thinking about the problem, but
I'm having trouble tracing my thoughts by hand. pencil and paper.
I think I'm on the right track, but apparently I don't fully understand.
So, if you all don't mind, please a little more discussion???
Given:
Disks
Three Pegs (or poles) (for this problem)
restrictions: smaller disk must always be on top of a larger disk
only one disk can be moved at a time to a peg/pole.
function parameters:
The number of disks to be moved
The peg on which those disks are initially threaded
The peg to which this stack of disks is to be moved
The peg to be used as a temporary holding area.
Ben discussion of decomposing the problem to a smaller
and smaller problem. simpler and simpler
Find: a recursive algorithm that solves the Tower of Hanoi.
Solution:
Let the poles have the names A, B, and C. ( I found that I had to make
the names a little more generic as I try to trace the function by hand)
The disks will start on A and all will have to be moved to C, with the constraints
mentioned above.
In the simplest of cases, there is only one disk. So that disk can be moved
to the final destination. A direct move. from A to C. so let me call A the
source of the disk and C the destination of the disk.
move from source to destination.
The next simplest of cases, there are two disk. So, one the smallest disk
which is on top of a larger disk must be move to a temporary locations,
amongst the three pegs.
So move a disk from A to B. From source to the temporary peg named B.
Now a direct move can take place. move a disk from A to C. I say direct only
because I am envisioning the pegs and disk as if I can reach out and grab and
move them. Of course, this is the larger of the two disk.
move a disk from A to C
move a disk from source to destination.
( here I realize the destination is changing, the destination will not always
be the same peg for each move.)
Finally move the disk on B to C. The peg B temporary held the smallest disk.
move a disk from source to destination. ( here again I note that source is not
A peg, source should be the peg from which I am grabbing the disk.
so, yes, move a disk from source to destination. that thought took a while
to grasp even with Ben's very very good hint. (different sources and different
destination. i mean i new that each disk may have to go to a different peg, but
using A,B,C, the concrete, instead of the generic source, destination, temporary
kept tying me up, mentally.
Ok, armed with the generic thought of, move a disk from a source to a destination
the next case, starting with three disk. This is where I seem to loose it.
With three disk I understand I'm going to need to temporary move a disk to
another peg. Also, I realize that must happen more than once. Meaning that there
are not pegs to place the smaller disk so that a direct move of the largest
disk can go to the final destination. ( OK, too many word, ok )
The concrete: A, B, C, three disk on A with smallest on top, largest at the bottom.
move a disk from A to C ( i know do this from trial and error otherwise i would
end doing more moves...)
move a disk from A to B
at this point, the largest disk is by itself on peg A, but it cannot be moved to
peg C because the smallest disk is there.
so, the smallest disk, which is on C, has to be moved to a temporary location
which meets the constraints, smaller on top of larger. Therefore the disk on C
can be moved to B.
Now the disk on A can be moved to C. This places the largest disk on C.
There are two disk on B so, there are actually two places where the smallest disk
may go and hold to the rules. The smallest could be moved to C or the smallest
could be moved to A. Moving the smallest to B, is not helping. so, I temporarily
place the smallest disk on A, or move a disk from B to A temporarily.
move a disk from B to C
move a disk from A to C
done.
P-code:
solveTowerOfHanoi ( int n, int source, int destination, int temporaryPeg)
{
if ( n == 1)
print the move "one disk from source to destination" // source: A,B,C
// destination: A,B,C
else
begin
solveTowerOfHanoi( n -1, source, temporaryPeg, destination)
/*
this is what i think is confusing me, the names, here i realize
i am passing into the function the destination, temporaryPeg
*/
move from source to destination (here destination is temporaryPeg)
/*
the following call's placement of parameters seem to take some
insight, right? i'm missing it.
*/
solveTowerOfHanoi( n - 1, ............ what.................)
end /* else */
I had this problem before and I thought I had mastered it. once the base case is occurs,
tracing the function back up, so to speak (write).
n=3
solveTowerOfHanoi ( 3 - 1, A, C, B )
solveTowerOfHanoi ( 2 - 1, A, B, C )
[--- solveTowerOfHanoi ( 1 -1, A, C, B )
here n = 1
print the move one disk from source to destination
so move one disk from A to C
[-----------
aren't i back at the: solveTowerOfHanoi ( 2 - 1, A, B, C )
so that means ???
move from source to destination
move from A to B
solveTowerOfHanoi ( 2 - 1, ...... what........ )
here's where i have confused myself. do i think about the second to last move or
the next move? at this point the source must change? the source needs to
be C and destination needs to be B... so
solveTowerOfHanoi ( 2- 1, C, B, A) which would lead to
solveTowerOfHanoi ( n-1, destination, temporaryPeg, source)
so:
P-code:
solveTowerOfHanoi ( int n, int source, int destination, int temporaryPeg)
{
if ( n == 1)
print the move "one disk from source to destination" // source: A,B,C
// destination: A,B,C
else
begin
solveTowerOfHanoi( n -1, source, temporaryPeg, destination )
move from source to destination (here destination is temporaryPeg)
solveTowerOfHanoi( n - 1, destination, temporaryPeg, source )
end /* else */
} /* solveTowerOfHanoi */
right? or close? i mean where did i lose it?
thanks, i know it's wordy .... sorry....
g.