Current location - Training Enrollment Network - Mathematics courses - Realization method of hanover tower
Realization method of hanover tower
Hanoi tower non-recursive algorithm

/*

Problem description: There are three pillars, A, B and C.A, and N boards are stacked on the pillars, each of which is a little smaller than the one below.

You can use 1, 2, ..., n from top to bottom. It is necessary to move all the plates on the A-pillar to the C-pillar by means of the B-pillar.

The moving condition is 1, and only one plate can be moved at a time;

2. When moving, the large market cannot be placed on the small one, but only on the large one.

*/

/*

Recursive algorithms are known to most people, and non-recursive algorithms have also appeared.

For example, from /club/old_showbbs.asp? id=96548

Author: qq590240

# include & ltiostream & gt

# include & ltstdlib.h & gt

#ifdef _WIN32

Use namespace std

#endif

Static empty river (int height)

{

int fromPole,toPole,Disk

int *BitStr = new int[height],

* Hold = new int[height];

char Place[] = {'A ',' B ',' C ' };

int i,j,temp

for(I = 0; I am < height; i++)

{

BitStr[I]= 0;

hold[I]= 1;

}

Temp = 3-(height% 2);

int total moves =( 1 & lt; & lt height)-1;

for(I = 1; I<= TotalMovesi++)

{

for(j = 0; BitStr[j]! = 0; j++)

{

BitStr[j]= 0;

}

BitStr[j]= 1;

disk = j+ 1;

if (Disk == 1)

{

from pole = Hold[0];

toPole = 6-from pole-temp;

temp = fromPole

}

other

{

from pole = Hold[Disk- 1];

toPole = 6-Hold[0]-Hold[Disk- 1];

}

Cout & lt& lt moving disk "<& lt disk & lt" comes from "<& lt place [from pole-1].

& lt& lt "Recipient"<< place [topole-1] < & ltendl

hold[Disk- 1]= toPole;

}

}

int main(int argc,char *argv[])

{

Cout & lt& lt Tower of Hanoi:<& lt endel

< < "using magnetic pole c to move the tower of n disks from magnetic pole a to magnetic pole b" <: & ltendl

Cout < < "Enter the height of the original tower:";

Int height;

CIN & gt; & gt height;

Hanoi (height);

System ("suspended");

Return EXIT _ SUCCESS

}

////////////////////////////////////////////////////////////

Here, I wrote a program to realize it according to the method provided by mathematical nutrition dishes (Tan).

*/

/*

Algorithm introduction:

First of all, it is easy to prove that when the number of plates is n, the number of moves should be equal to 2 n- 1.

An American scholar has found an unexpected method, which only takes two steps in turn.

First, arrange the three pillars into a finished glyph in sequence, and put all the disks on pillar A in the order from big to small.

According to the number of disks, determine the order of columns: if n is even, put AB C; clockwise in turn;

If n is odd, put A C B in turn clockwise.

(1) Move the disk 1 clockwise from the current column to the next column, that is, when n is even, if the disk 1 is in column A, it will be moved to column B;

If the disk 1 is in column b, move it to c; If the disc 1 is in column c, move it to a. ..

(2) Then, move the removable disks on the other two columns to the new column.

That is, the optical disc on a non-empty column is moved to an empty column, and the smaller optical disc is moved when both columns are not empty.

This step does not specify which disk to move. You may think there are many possibilities, but it is not. The only action that can be implemented is.

(3) Repeat the operation of (1)(2), and finally complete the movement of Hanoi Tower according to the regulations.

*/

# include & ltiostream & gt

Use namespace std

Const int MAX = 64// The maximum number of disks is 64.

Struct st{ // is used to represent the information of each column.

int s[MAX]; //Disk storage on the column

Int top// stack top, used for the uppermost disk.

Character name; //The name of the column, which can be one of A, B and C..

Int Top()// Get the top element of the stack.

{

return s[top];

}

Int Pop()// Exit stack

{

return s[top-];

}

Void Push(int x)// stack

{

s[++ top]= x;

}

} ;

long Pow(int x,int y); //calculate x y

void Creat(st ta[],int n); //Set the initial value of the structure array

Void Tower of Hanoi (st ta[], Long Max); //Main functions of mobile Hanoi Tower

int main(void)

{

int n;

CIN & gt; & gtn; //Enter the number of disks.

ST ta[3]; //The information of the three pillars is stored in the structure array.

Creat(ta,n); //Set the initial value of the structure array

long max = Pow(2,n)- 1; //The number of moves should be equal to 2 n- 1.

Tower of Hanoi (ta, max); //Main functions of mobile Hanoi Tower

System ("suspended");

Returns 0;

}

Empty creation (st ta[], int n)

{

ta[0]。 name = ' A

ta[0]。 top = n- 1;

for(int I = 0; I & ltn;; I++) // Arrange all the disks in column A in descending order.

ta[0]。 s[I]= n-I;

ta[ 1]。 top = ta[2]。 top = 0; //There were no disks on columns B and C at first.

for(int I = 0; I & ltn;; i++)

ta[ 1]。 s[i] = ta[2]。 s[I]= 0;

If (n%2 == 0) // If n is even, put a B C clockwise.

{

ta[ 1]。 name = ' B

ta[2]。 name = ' C

}

Else // If n is odd, put A C B in turn clockwise.

{

ta[ 1]。 name = ' C

ta[2]。 name = ' B

}

}

Long power (integer x, integer y)

{

Length =1;

for(int I = 0; I & lty;; i++)

sum * = x;

Returns the sum;

}

Invalid Tower of Hanoi (st ta[], long max)

{

int k = 0; //Cumulative number of moves

int I = 0;

int ch

while(k & lt; Max)

{

//Move the disc 1 clockwise from the current column to the next column.

ch = ta[i%3]。 pop();

ta[(i+ 1)%3]。 Push (ch);

cout & lt& lt++ k & lt; & lt":" & lt& lt "mobile disk"<& ltch & lt& lt comes from "<& ltta[i%3]. Name & lt& lt "Recipient"<& ltta[(i+ 1)%3]. Name & lt& ltendl

i++;

//Move the removable disks on the other two columns to the new column.

if(k & lt; Max)

{//Move the disc on a non-empty column to an empty column. When both columns are empty, move the smaller disc.

if (ta[(i+ 1)%3]。 Top() == 0 || ta[(i- 1)%3]。 top()& gt; 0 & amp& ampta[(i+ 1)%3]。 top()& gt; ta[(i- 1)%3]。 Top())

{

ch = ta[(i- 1)%3]。 pop();

ta[(i+ 1)%3]。 Push (ch);

cout & lt& lt++ k & lt; & lt":" & lt& lt "mobile disk"<& ltch & lt& lt comes from "<& ltta[(i- 1)%3]. Name & lt& lt "Recipient"<& ltta[(i+ 1)%3]. Name & lt& ltendl

}

other

{

ch = ta[(i+ 1)%3]。 pop();

ta[(i- 1)%3]。 Push (ch);

cout & lt& lt++ k & lt; & lt":" & lt& lt "mobile disk"<& ltch & lt& lt comes from "<& ltta[(i+ 1)%3]. Name & lt& lt "Recipient"<& ltta[(i- 1)%3]. Name & lt& ltendl

}

}

}

}