|
|
|
|
|
|
You have undefined behavior in your move function. When the list pointed to by to is empty, it is undefined behavior to invoke to->back() because no elements exist in to. |
|
|
The problem is that you're trying to code an algorithm that you don't understand. Stop writing code. Fish out 3 or 4 coins from your pocket and see if you can follow the algorithm to a solution. Doing this will help you understand the algo. Once you understand the algo, return to your code. |
Simpler statement of iterative solution Alternating between the smallest and the next-smallest disks, follow the steps for the appropriate case: For an even number of disks: make the legal move between pegs A and B make the legal move between pegs A and C make the legal move between pegs B and C repeat until complete For an odd number of disks: make the legal move between pegs A and C make the legal move between pegs A and B make the legal move between pegs C and B repeat until complete In each case, a total of 2n-1 moves are made. |
|
|
============== =====top=======1 5 4 3 2 1 empty empty ============== ============== =====top=======2 5 4 3 2 empty 1 ============== ============== =====top=======3 5 4 3 2 1 ============== ============== =====top=======4 5 4 3 2 1 empty ============== ============== =====top=======5 5 4 2 1 3 ============== |
|
|
============== =====top=======1 3 2 1 empty empty ============== ============== =====top=======2 3 2 empty 1 ============== ============== =====top=======3 3 2 1 ============== ============== =====top=======4 3 1 2 empty ============== ============== =====top=======5 3 2 1 empty ============== ============== =====top=======6 empty 2 1 3 ============== ============== =====top=======7 1 2 3 ============== ============== =====top=======8 empty 2 3 1 ============== ============== =====top=======9 2 empty 3 1 ============== ============== =====top=======10 2 1 empty 3 ============== ============== =====top=======11 2 1 3 ============== ============== =====top=======12 empty 1 3 2 ============== ============== =====top=======13 1 empty 3 2 ============== ============== =====top=======14 empty empty 3 2 1 ============== |
|
|