CSCI 20 Programming assignment #2, the Towers of Hanoi (160... Write a program to solve the Towers of Hanoi problem...

advertisement
CSCI 20 Programming assignment #2, the Towers of Hanoi (160 points) due 3/10/16
Write a program to solve the Towers of Hanoi problem by brute force, using the breadthfirst search algorithm we discuss in class. Any deviations from this algorithm must be
cleared with me in advance. You will use a queue of positions, each with 3 pegs called
A, B and C (implemented as stacks) per position, N disks on the pegs, numbered 1 – N in
increasing size (maximum size of 9, so you can use chars for the stacks), and a list of
moves taken to reach that position.
Create a "stack of characters" class, a list class (you may modify and re-use the singly
linked list class from HUGE_INT, possibly extended with a tail pointer for easier use) for
the list of moves, and a queue class, which must be implemented using a linked list of
tower positions. For simplicity, I suggest that your queue class manage its linked list
directly, not as a subclass, and keep its own head and tail pointers into the linked queue.
Your "moves" linked list element will contain 3 chars: the disk number for this move and
the "from" and "to" pegs. Your queue element for the positions will contain 3 stacks for
the pegs (A, B, C) and a list for the moves. It may also contain the next pointer for the
queue's links.
Your program will solve the problem of moving N disks from peg A to peg B using peg
C as the spare peg.
Start with the initial position (all disks on peg A) on the queue. Then loop, dequeueing
positions and checking for legal moves. You must check for legal (possible) moves in
this sequence from each position:
A -> B
A -> C
B -> A
B -> C
C -> A
C -> B
For each of the legal moves from the current position, make a copy of the current
position, make the move (pop() and push() the disk from and to the appropriate peg).
Then, if the position is the solution, print the list of moves and stop. If not, add the new
position to the queue of positions. When each position is completed, free its storage (you
may want to manage your own free lists to minimize fragmentation of the system heap)
and get the next position off of the queue to check.
Turn in your source code for all files and the output list of moves. Your code must work
correctly for 1, 2, 3 and 4 disks. Try to get it to work for more, say up to 7 or 8 disks. If
you're adventurous, try 9 disks. It will take a while (maybe overnight, or all weekend).
There are some optimizations available to speed this up.
This should work as is in Quincy for up to 9 disks, unless you leak memory. I haven't
tried this yet for 9 disks on Code::Blocks, but I think it should work, again, unless you
leak memory. To do this in Visual Studio, you will have to resize the system heap
(different versions of VS may do this slightly differently):
1)
2)
3)
4)
Go to the 'Project' toolbar and click on '[project's name] properties'.
To the left of the window that pops up is a list of labeled folders. Click on 'Linker'.
This spawns a list of sub-choices in the left frame. Click on 'System'.
In the large frame several labels appear with adjacent text boxes. Two near the top
should say 'Heap Reserve Size' and 'Heap Commit Size'. Set both of these to
whatever size (in bytes, in decimal) you want.
Download