Hannuobeita game

Tower of Hanoi: Tower of Hanoi (also known as the Tower of Hanoi) problem stems from an ancient Indian legend of educational toys. When Brahma created the world to do three diamond pillars, from the bottom up pile with 64 gold discs in order of size to a post.

Brahma Brahman command to the disk in order of size from the bottom again placed on the other pillars. And predetermined, the disc can not be enlarged in a small disk, a disk can only be moved between the three columns

AHP @: A-> B A-> C B-> C A: 1 2 -> C: 1 2;

 

#include<iostream>
using namespace std;

int times = 0;

void move(char src, char dst)
{
times++; //移动次数
cout << times << ": " << src << "--->" << dst << endl;
}

void hanNuoTower (int n, char first , char mid, char last) // n: number of plates first: a first memory with positive plates mid: Transfer to plates last: storing the final dish
{
IF (n-==. 1) // recursive outlet not forget
Move (First, Last);
the else
{
hanNuoTower (n--. 1, First, Last, MID);
Move (First, Last);
hanNuoTower (n--. 1, MID, First, Last);
}
}

int main()
{
int n = 4;
hanNuoTower(n, 'A', 'B', 'C');

return 0;

}
//1 : A--->C
//2 : A--->B
//3 : C--->B
//4 : A--->C
//5 : B--->A
//6 : B--->C
//7 : A--->C


//1: A--->B
//2 : A--->C
//3 : B--->C
//4 : A--->B
//5 : C--->A
//6 : C--->B
//7 : A--->B
//8 : A--->C
//9 : B--->C
//10 : B--->A
//11 : C--->A
//12 : B--->C
//13 : A--->B
//14 : A--->C
//15 : B--->C

Guess you like

Origin www.cnblogs.com/xcb-1024day/p/11334980.html