What is Towers of Hanoi ?
Figure1 shows an tower of hanoi setup with three discs on the first pole. Note that all the discs are a different size. The a < b < c.
The Rules
The goal is move all discs from first pole to the last pole. There are three rules:
1. A large disc can never be placed on top of a small disc.
2. You can only move one disc at a time
3. You can move discs on first pole to third pole with one movement.
Solving the Puzzle
1. Move a to 3.
2. Move b to 2.
3. Move a to 2.
4. Move c to 3.
5. Move a to 1.
6. Move b to 3.
7. Move a to 3.
Solving the Puzzle with a Computer
Look at Figure2. To solve the Towers of Hani with four discs.
Step1. Move 3 discs to the second pole.
Step2. Move biggest disc to the third pole.
Step3. Move 3 discs to the third pole.
How to Move 3 discs to the second pole?
Step1. Move 2 discs to the third pole.
Step2. Move biggest disc to the second pole.
Step3. Move 2 discs to the second pole.
So if you want to move n discs:
Step1. Move the top n - 1 discs to the pole 2 (Move top n - 1 discs From beginning pole to empty)
Step2. Move the nthdisc to the pole 3 (Move the nth From beginning pole to destination)
step3. Move the n -1 discs from the pole 2 to 3. (Move the n - 1 discs From empty pole to destination)
int findEmpty(int from, int destination){
if((from == 1 && destination == 2) || (from == 2 && destination == 1))
return 3;
else if((from == 2 && destination == 3) || (from == 3 && destination == 2))
return 1;
else if((from == 1 && destination == 3) || (from == 3 && destination == 1))
return 2;
}
void moveTown(int totalNum, int beginning, int destination){
if(totalNum == 1){
cout << "move "<< totalNum << " from " << beginning << " to " << destination << endl;
}else{
int empty = findEmpty(beginning, destination);
moveTown(totalNum - 1, beginning, empty);
cout << "move "<< totalNum << " from " << beginning << " to " << destination << endl;
moveTown(totalNum - 1, empty, destination);
}
}
For example, If you want computer to solve the 3 discs tower.
just call
moveTown(3, 1, 3);
Look At moveTown function. Every time we need findEmpty function to find the empty pole. There are only 3 poles. So we can tell the function what the empty pole is.
void Hanoi(const int& n,const int& beginning,const int& destination,const int& empty )
{
if( n > 0 )
{
Hanoi( n-1, beginning, empty, destination );
cout << "Moving " << n << " from " << beginning << " to " << destination << endl;
Hanoi( n-1, empty, destination, beginning );
}
}
The whole Code
// Hanoi.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int findEmpty(int from, int destination){
if((from == 1 && destination == 2) || (from == 2 && destination == 1))
return 3;
else if((from == 2 && destination == 3) || (from == 3 && destination == 2))
return 1;
else if((from == 1 && destination == 3) || (from == 3 && destination == 1))
return 2;
}
//slow one
void moveTown(int totalNum, int beginning, int destination){
if(totalNum == 1){
cout << "move "<< totalNum << " from " << beginning << " to " << destination << endl;
}else{
int empty = findEmpty(beginning, destination);
moveTown(totalNum - 1, beginning, empty);
cout << "move "<< totalNum << " from " << beginning << " to " << destination << endl;
moveTown(totalNum - 1, empty, destination);
}
}
//better one
void Hanoi(const int& n,const int& beginning,const int& destination,const int& empty )
{
if( n > 0 )
{
Hanoi( n-1, beginning, empty, destination );
cout << "Moving " << n << " from " << beginning << " to " << destination << endl;
Hanoi( n-1, empty, destination, beginning );
}
}
int main(int argc, char* argv[])
{
//moveTown(3, 1, 3);
cout << endl;
Hanoi(3, 1, 3, 2);
return 0;
}
http://www.waitingfy.com/?p=502