问题
I have no idea about Tower of Hanoi. I want to write a program on this using recursion.
回答1:
From Wikipedia:
The Tower of Hanoi or Towers of Hanoi (also known as The Towers of Brahma) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks neatly stacked in order of size on one rod, the smallest at the top, thus making a conical shape.
Check out the recursive solution.
回答2:
Another homework assignment. Pass your teacher's A to me ;)
Source: http://www.soc.napier.ac.uk/~andrew/hanoi/rechelp.html
Bonus: a step-by-step YouTube video.
A little bit about the Tower of Hanoi
An analysis of this and a discussion of the (invented) mythology and of the four peg version can be found in the rec.puzzles FAQ look for induction/hanoi.s
The Tower of Hanoi problem has a nice recursive solution.
Working out recursive solutions
To solve such problems ask yourself: "if I had solved the n-1 case could I solve the n case?"
If the answer to this question is positive you proceed under the outrageous assumption that the n-1 case has been solved. Oddly enough this works, so long as there is some base case (often when n is zero or one) which can be treated as a special case.
How to move n rings from pole A to pole C?
If you know how to move n-1 rings from one pole to another then simply move n-1 rings to the spare pole - there is only one ring left on the source pole now, simply move it to the destination, then pile the rest of them from the spare pole onto the destination pole.
For example when n is 4...
First move three onto the spare pole (worry how to do this later).
Now move one ring from the source pole to the destination pole.
Now move three rings from the spare pole to the destination pole
(again, we can worry about how to do this later).
We have finished!
More succinctly...
To move n rings from A to C using B as spare:
- if n is 1
- just do it,
- otherwise...
- move n-1 rings from A to B using C as spare
- move one ring from A to C
- move n-1 rings from B to C using A as spare
As with most recursive solutions we have to treat some base case specially - here the base case occurs where we have only one ring to move.
How to do it in C
/* Tower of Hanoi - the answer */
/* How to move four rings from pin 1 to pin 3 using pin 2 as spare */
#include <stdio.h>
void move(n, A, C, B)
/* number to move, source pole, destination pole and
spare pole respectively */
int n, A, B, C; {
if (n == 1) {
printf("Move from %d to %d.\n", A, C);
} else {
move(n - 1, A, B, C);
move(1, A, C, B);
move(n - 1, B, C, A);
}
}
main() {
move(4, 1, 3, 2);
}
回答3:
Here is a compact implementation in Lisp: http://www.kernelthread.com/projects/hanoi/html/gcl.html. It is certainly recursive, but I did not verify it's correctness.
回答4:
#!/usr/bin/env python
discs = 3
T = [range(discs, 0, -1), [], []]
def show_towers():
"""Render a picture of the current state of the towers"""
def render_disc(t, y):
return ("-"*(t[y]*2-1) if y < len(t) else "|").center(discs*2)
for y in range(discs):
print " ".join(render_disc(t, discs-y-1) for t in T)
print "="*(discs*6+3)
def move(n, source, destination):
"""Recursively move n discs from source to destination"""
while n > 0:
temp = 3 - source - destination
move(n-1, source, temp)
T[destination].append(T[source].pop())
show_towers()
n, source = n-1, temp # Simulate tail recursion
show_towers()
move(discs, 0, 2)
output for discs = 3
- | |
--- | |
----- | |
=====================
| | |
--- | |
----- | -
=====================
| | |
| | |
----- --- -
=====================
| | |
| - |
----- --- |
=====================
| | |
| - |
| --- -----
=====================
| | |
| | |
- --- -----
=====================
| | |
| | ---
- | -----
=====================
| | -
| | ---
| | -----
=====================
回答5:
The Structure and Interpretation of Computer Programs video lectures contain helpful tips on solving this problem and a wealth of knowledge besides.
回答6:
See wikipedia towers of hanoi article for the description of recursive algorithm.
It goes something like this:
#include <iostream> // ostream
#include <algorithm> // for_each
#include <deque> // I can iterate over towers & print state,<stack> works as well
#include <boost/array.hpp> // just a wrapper for array
#include <boost/lambda/lambda.hpp> // easy one line for_each iterating
using namespace std;
typedef std::deque< int > tower_t; // stack works as well, deque for printing
typedef boost::array< tower_t ,3 > towers_t; // 3 towers
enum peg { A = 0, B = 1, C = 2 };
Printing:
ostream & show(ostream & os, const tower_t & t)
{
os << "[";
for_each (t.begin(), t.end(), os << boost::lambda::_1 );
return os << "]";
}
ostream & show(ostream & os, const towers_t & t)
{
show(os, t[0]); show(os, t[1]); show(os, t[2]);
return os;
}
Solve:
void move(peg from, peg to, towers_t & t)
{
// show move and state before move
cout << "mv: " << t[from].back() << " " << from << " --> " << to << "\t\t";
show(cout, t); cout << " --> ";
// the actual move: move top peg `from` stick `to` stick (and `pop` old top)
t[to].push_back(t[from].back());
t[from].pop_back();
// show state after move
show(cout, t); cout << endl;
}
// move n discs from A to B via C
void move(int n, peg from, peg to, peg via, towers_t & t)
{
if (n == 1) { move(from, to, t); return; }
move(n-1, from, via, to, t);
move(from, to, t);
move(n-1, via, to, from, t);
return;
}
Usage, solve tower with 4 pegs:
int main()
{
towers_t ttt;
tower_t & first_tower(ttt[0]);
first_tower.push_back(4);
first_tower.push_back(3);
first_tower.push_back(2);
first_tower.push_back(1);
move(first_tower.size(), A, C, B, ttt); // move n from A to C via B
}
Solved 3 towers with 4 pegs on the first tower, the biggest peg has the highest number, the smallest one is 1.
Output (mv: PegX FromTower ---> ToTower
) followed by state before and after move, each tower from left to right showing pegs from bottom to top - top is on right:
mv: 1 0 --> 1 [4321][][] --> [432][1][]
mv: 2 0 --> 2 [432][1][] --> [43][1][2]
mv: 1 1 --> 2 [43][1][2] --> [43][][21]
mv: 3 0 --> 1 [43][][21] --> [4][3][21]
mv: 1 2 --> 0 [4][3][21] --> [41][3][2]
mv: 2 2 --> 1 [41][3][2] --> [41][32][]
mv: 1 0 --> 1 [41][32][] --> [4][321][]
mv: 4 0 --> 2 [4][321][] --> [][321][4]
mv: 1 1 --> 2 [][321][4] --> [][32][41]
mv: 2 1 --> 0 [][32][41] --> [2][3][41]
mv: 1 2 --> 0 [2][3][41] --> [21][3][4]
mv: 3 1 --> 2 [21][3][4] --> [21][][43]
mv: 1 0 --> 1 [21][][43] --> [2][1][43]
mv: 2 0 --> 2 [2][1][43] --> [][1][432]
mv: 1 1 --> 2 [][1][432] --> [][][4321]
来源:https://stackoverflow.com/questions/1147264/tower-of-hanoi-using-recursion