650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/80/04/wKioL1c0mLOj9UFaAAMmSLBAO4U112.png "title=" capture. PNG "alt=" Wkiol1c0mloj9ufaaammslbao4u112.png "/>
Recursive solution of Hanoi Tower problem:
Implementation program:
#include <iostream>
using namespace Std;
void Move (int n, char I, Char j)
{
cout << "Move" << N << "from" << I << "to" << J << Endl;
}
void Hanoi (int n, char x, char y, char z)
{
if (n = = 1)
{
Move (1, x,z);
}
Else
{
Hanoi (n-1, X, z, y);
Move (n, x, z);
Hanoi (n-1, y, x, z);
}
}
int main ()
{
cout << "The following is the solution of the 3-layer Hanoi:" << Endl;
Hanoi (3, ' x ', ' y ', ' z ');
cout << "Output finished! "<< Endl;
System ("pause");
return 0;
}
Operation Result:
Here are the solutions for the 3-layer Hanoi:
Move number 1th from X to Z.
Move number 2nd from X to Y.
Move number 1th from Z to Y.
Move number 3rd from X to Z.
Move number 1th from Y to X.
Move number 2nd from Y to Z.
Move number 1th from X to Z.
The output is complete!
Please press any key to continue ...
This article is from the "Rock Owl" blog, please be sure to keep this source http://yaoyaolx.blog.51cto.com/10732111/1772860
Recursive solution of Hanoi Tower problem