ººÅµËþÎÊÌâµÄËã·¨·ÖÎö¼°C++ʵÏÖ
½ËÕ¹àÄÏÏØ ËïС»Û
ÕªÒª£º¸ÃÎĶԾµäµÄººÅµËþÎÊÌâ½øÐÐÁËÏêϸµÄ·ÖÎö£¬¸ø³öÁ˵ݹéµÄºÍ·ÇµÝ¹éµÄËã·¨£¬²¢ÓÃc++ÓïÑÔ¶ÔÕâÁ½ÖÖËã·¨½øÐÐÁËʵÏÖÓë±È½Ï¡£
¹Ø¼ü×Ö£º¡¡ººÅµËþ£¬µÝ¹éËã·¨£¬·ÇµÝ¹éËã·¨
Algorithm Analysis and C++ Realization of Hanio Issue
Sun Xiaohui
Abstract: This text carries on detailed analysis about classical Hanio issue ,then describe it with both Recursive algorithm and Non-recursive algorithm, provides realization of algorithm in C++.
Keywords£ºTower of hanoi£¬Recursive algorithm£¬Non-recursive algorithm
Ò»¡¢ ÎÊÌâÃèÊö
ÎÊÌâÌá³ö£º
ÉèÓÐÈý¸öËþÖù£¨·Ö±ðΪAºÅ£¬BºÅºÍCºÅ£©¡£³õʼʱ£¬ÓÐn ¸öÔ²ÅÌ°´×ÔÏÂÏòÉÏ¡¢´Ó´óµ½Ð¡µÄ´ÎÐòµþÖÃÔÚA ËþÉÏ¡£ÏÖÒª½«A ËþÉϵÄËùÓÐÔ²ÅÌ£¬½èÖúÓÚBËþ£¬È«²¿Òƶ¯µ½CËþÉÏ£¬ÇÒÈÔ°´ÕÕÔÀ´µÄ´ÎÐòµþÖá£Òƶ¯µÄ¹æÔòÈçÏ£ºÕâЩԲÐÎÅÌÖ»ÄÜÔÚ¸öËþ¼ä½øÐÐÒƶ¯£¬Ò»´ÎÖ»ÄÜÒƶ¯Ò»¸öÅÌ×Ó£¬ÇÒÈκÎʱºò¶¼²»ÔÊÐí½«½Ï´óµÄÅÌ×ÓѹÔÚ±ÈËüСµÄÅÌ×ÓµÄÉÏÃæ¡£ÒªÇóÈçÏ£º´Ó¼üÅÌÊäÈë³õʼԲÐÎÅÌ×Ó¸öÊýn£¬ÓÃC ÓïÑÔʵÏÖn ¸öÅÌ×Ó×î¼ÑÒƶ¯µÄÈ«¹ý³Ì[1]¡£
±¾ÌâµÄÄ¿µÄÊÇÉè¼ÆÒ»¸öÅÌ×ÓÒƶ¯µÄ·½°¸£¬Ê¹µÃA ºÅËþÉϵÄËùÓÐÅÌ×Ó½èÖúÓÚBËþ°´ÕÕÔÀ´µÄ´ÎÐòÒƶ¯µ½CËþÉÏ£¬²¢ÇÒ£¬Òª¸ø³öÍêÕûµÄÅÌ×ÓÒƶ¯µÄ·½°¸¡£ÏÂÃæÎÒÃǴӵݹéºÍ·ÇµÝ¹éÁ½ÖÖ·½Ãæ½øÐп¼ÂÇ[2]¡£
¶þ¡¢ µÝ¹é½â·¨¼°ÆäC++ʵÏÖ
ÎÒÃÇÏÈÀ´¿¼ÂÇϵݹéÇó½âÕâ¸öÎÊÌâµÄÇé¿ö£º
¼ÙÉè¹²ÓÐn¸öÅÌ×Ó£¬ÎÞÂÛnÊǶàÉÙ£¬Ò²²»¹ÜÔõôȥÒƶ¯ÅÌ×Ó£¨µ±È»ÊÇ°´¹æÔòÀ´Òƶ¯£©£¬µ«ÔÚÒƶ¯µÄ¹ý³ÌÖУ¬½«Ê¼ÖÕ»á³öÏÖÕâÑùµÄ״̬Çé¿ö£º£¨n£1£©¸öÅÌ×Ó½«»áÒÔ×ÔÏÂÏòÉÏ¡¢´Ó´óµ½Ð¡µÄ´ÎÐòµþÖÃÔÚB ËþÉÏ£¬Õâʱ£¬AËþÉϵÚn ¸öÅÌ×Ó¾ÍÄܱ»Çá¶øÒ׾ٵصþ·Åµ½C ËþÉÏ£»½Ó×Å£¬ÔÙ°ÑB ËþÉϵĹ²£¨n£1£©¸öÅÌ×ÓÒƶ¯µ½C ËþÉÏ£¬ÎÊÌâºÃÏñÒѾ½â¾ö¡£µ«£¬B ËþÉÏ£¨n£1£©¸öÅÌ×ÓÔõôÒƶ¯µ½C ËþÉÏÄØÑ¿ÕâÊÇÒª½â¾öµÄµÚ¶þ¸öÎÊÌ⡣ͬÑù£¬²»¹ÜÎÒÃÇÔõôÒƶ¯£¬Ò²½«»á³öÏÖÕâÑùµÄ״̬Çé¿ö£º£¨n£2£© ¸öÅÌ×Ó½«»áÒÔ´ÓÉϵ½Ï¡¢´Ó´óµ½Ð¡µÄ´ÎÐòµþÖÃÔÚA ËþÉÏ£¬Õâʱ£¬B ËþÉϵڣ¨n£1£© ¸öÅÌ×Ó¾ÍÄܱ»Çá¶øÒ׾ٷŵ½C ËþÉÏ£»½Ó×Å£¬°ÑA ËþÉϵĹ²£¨n£2£©¸öÅÌ×ÓÒƶ¯µ½C ËþÉÏ¡£ÕâÑù£¬²»¶ÏÉîÈ룬²»¶ÏϸС»¯£¬×îÖÕ£¬½«µ½´ï½öÓÐÒ»¸öÅ̵ÄÇéÐΣ¬Õâʱ£¬µÝ¹éÒ²¾ÍÖÕÖ¹ÁË£¬ÎÊÌâÒ²µÃµ½Á˽â¾ö¡£Í¨¹ýÒÔÉÏ·ÖÎö£¬ÕâÀïÓкÜÃ÷ÏԵĵݹé¹Øϵ¡£
ÓÉ´Ë£¬¿ÉÒÔ¸ø³öººÅµËþÎÊÌâµÄµÝ¹éËã·¨ÈçÏ£º
1. µ±½öÓÐ1¸öÅÌ×Óʱ£¬°ÑÕâ¸öÅÌ×Ó´ÓAËþÖùÒƶ¯µ½CËþÖùÉÏ
2. µ±Ô²Å̵ĸöÊý¶àÓÚ1¸öʱ£¬ÈçϽâ¾ö£º
£¨1£© ÏȽ«AËþÖùÉϵģ¨n-1£©¸öÔ²ÅÌͨ¹ýCËþÖùÒƶ¯µ½BËþÖùÉÏ
£¨2£© ÔÙ½«AËþÖùÉϵĵÚn¸öÔ²ÅÌÖ±½ÓÒƶ¯µ½CËþÖùÉÏ
£¨3£© ×îºóBËþÖùÉϵģ¨n-1£©¸öÔ²ÅÌͨ¹ýAËþÖùÒƶ¯µ½CËþÖùÉÏ
¸ÃËã·¨¶ÔÓ¦µÄ´úÂëÈçÏ£º
void hanoi(int n,char a,char b,char c)
{
if(n==1){
cout<<a<<"->"<<c<<endl;
}
else {
hanoi(n-1,a,c,b);
cout<<a<<"->"<<c<<endl;
hanoi(n-1,b,a,c);
}
}
Èý¡¢ ·ÇµÝ¹é½â·¨¼°ÆäC++ʵÏÖ
Ê×ÏÈ°ÑÈý¸ùÖù×Ó°´Ë³ÐòÅųÉÆ·×ÖÐÍ£¬°ÑËùÓеÄÔ²ÅÌ°´´Ó´óµ½Ð¡µÄ˳Ðò·ÅÔÚÖù×ÓAÉÏ¡£
¸ù¾ÝÔ²Å̵ÄÊýÁ¿È·¶¨Öù×ÓµÄÅÅ·Å˳Ðò£ºÈônΪżÊý£¬°´Ë³Ê±Õë·½ÏòÒÀ´Î°Ú·Å A B C£»
£¨1£©ÈônΪÆæÊý£¬°´Ë³Ê±Õë·½ÏòÒÀ´Î°Ú·Å A C B¡£
°´Ë³Ê±Õë·½Ïò°ÑÔ²ÅÌ1´ÓÏÖÔÚµÄÖù×ÓÒƶ¯µ½ÏÂÒ»¸ùÖù×Ó£¬¼´µ±nΪżÊýʱ£¬ÈôÔ²ÅÌ1ÔÚÖù×ÓA£¬Ôò°ÑËüÒƶ¯µ½B£»ÈôÔ²ÅÌ1ÔÚÖù×ÓB£¬Ôò°ÑËüÒƶ¯µ½C£»ÈôÔ²ÅÌ1ÔÚÖù×ÓC£¬Ôò°ÑËüÒƶ¯µ½A¡£
£¨2£©½Ó×Å£¬°ÑÁíÍâÁ½¸ùÖù×ÓÉÏ¿ÉÒÔÒƶ¯µÄÔ²ÅÌÒƶ¯µ½ÐµÄÖù×ÓÉÏ¡£
¼´°Ñ·Ç¿ÕÖù×ÓÉϵÄÔ²ÅÌÒƶ¯µ½¿ÕÖù×ÓÉÏ£¬µ±Á½¸ùÖù×Ó¶¼·Ç¿Õʱ£¬Òƶ¯½ÏСµÄÔ²ÅÌ
ÕâÒ»²½Ã»ÓÐÃ÷È·¹æ¶¨Òƶ¯ÄĸöÔ²ÅÌ£¬Äã¿ÉÄÜÒÔΪ»áÓжàÖÖ¿ÉÄÜÐÔ£¬Æäʵ²»È»£¬¿ÉʵʩµÄÐж¯ÊÇΨһµÄ¡£
£¨3£©·´¸´½øÐУ¨1£©£¨2£©²Ù×÷£¬×îºó¾ÍÄÜ°´¹æ¶¨Íê³ÉººÅµËþµÄÒƶ¯[3]¡£
C++´úÂëÈçÏ£º
const int MAX = 64; //Ô²Å̵ĸöÊý×î¶àΪ64
struct st{ //ÓÃÀ´±íʾÿ¸ùÖù×ÓµÄÐÅÏ¢
int s[MAX]; //Öù×ÓÉϵÄÔ²ÅÌ´æ´¢Çé¿ö
int top; //Õ»¶¥£¬ÓÃÀ´×îÉÏÃæµÄÔ²ÅÌ
char name; //Öù×ÓµÄÃû×Ö£¬¿ÉÒÔÊÇA£¬B£¬CÖеÄÒ»¸ö
int Top(){//È¡Õ»¶¥ÔªËØ
return s[top];
}
int Pop(){//³öÕ»
return s[top--];
}
void Push(int x){//ÈëÕ»
s[++top] = x;
}
} ;
long Pow(int x, int y); //¼ÆËãx^y
void Creat(st ta[], int n); //¸ø½á¹¹Êý×éÉèÖóõÖµ
void Hannuota(st ta[], long max); //Òƶ¯ººÅµËþµÄÖ÷Òªº¯Êý
int main(void)
{
int n;
cin >> n; //ÊäÈëÔ²Å̵ĸöÊý
st ta[3]; //Èý¸ùÖù×ÓµÄÐÅÏ¢ÓýṹÊý×é´æ´¢
Creat(ta, n); //¸ø½á¹¹Êý×éÉèÖóõÖµ
long max = Pow(2, n) - 1;//¶¯µÄ´ÎÊýÓ¦µÈÓÚ2^n - 1
Hannuota(ta, max);//Òƶ¯ººÅµËþµÄÖ÷Òªº¯Êý
system("pause");
return 0;
}
void Creat(st ta[], int n)
{
ta[0].name = 'A';
ta[0].top = n-1;
for (int i=0;i<n;i++)//°ÑËùÓеÄÔ²ÅÌ°´´Ó´óµ½Ð¡µÄ˳Ðò·ÅÔÚÖù×ÓAÉÏ
ta[0].s[i] = n - i;
ta[1].top = ta[2].top = 0;//Öù×ÓB£¬CÉÏ¿ªÊ¼Ã»ÓÐûÓÐÔ²ÅÌ
for (int i=0; i<n; i++)
ta[1].s[i] = ta[2].s[i] = 0;
if (n%2 == 0) //ÈônΪżÊý£¬°´Ë³Ê±Õë·½ÏòÒÀ´Î°Ú·Å A B C
{
ta[1].name = 'B';
ta[2].name = 'C';
}
else //ÈônΪÆæÊý£¬°´Ë³Ê±Õë·½ÏòÒÀ´Î°Ú·Å A C B
{
ta[1].name = 'C';
ta[2].name = 'B';
}
}
long Pow(int x, int y)
{
long sum = 1;
for (int i=0; i<y; i++)
sum *= x;
return sum;
}
void Hannuota(st ta[], long max)
{
int k = 0; //ÀÛ¼ÆÒƶ¯µÄ´ÎÊý
int i = 0;
int ch;
while (k < max)
{
//°´Ë³Ê±Õë·½Ïò°ÑÔ²ÅÌ1´ÓÏÖÔÚµÄÖù×ÓÒƶ¯µ½ÏÂÒ»¸ùÖù×Ó
ch = ta[i%3].Pop();
ta[(i+1)%3].Push(ch);
cout << ++k << ": " << "Move disk " << ch << " from " << ta[i%3].name << " to " << ta[(i+1)%3].name << endl;
i++;
//°ÑÁíÍâÁ½¸ùÖù×ÓÉÏ¿ÉÒÔÒƶ¯µÄÔ²ÅÌÒƶ¯µ½ÐµÄÖù×ÓÉÏ
if (k < max)
{//°Ñ·Ç¿ÕÖù×ÓÉϵÄÔ²ÅÌÒƶ¯µ½¿ÕÖù×ÓÉÏ£¬µ±Á½¸ùÖù×Ó¶¼Îª¿Õʱ£¬Òƶ¯½ÏСµÄÔ²ÅÌ
if (ta[(i+1)%3].Top() == 0 || ta[(i-1)%3].Top() > 0 && ta[(i+1)%3].Top() > ta[(i-1)%3].Top())
{
ch = ta[(i-1)%3].Pop();
ta[(i+1)%3].Push(ch);
cout << ++k << ": " << "Move disk " << ch << " from " << ta[(i-1)%3].name << " to " << ta[(i+1)%3].name << endl;
}
else
{
ch = ta[(i+1)%3].Pop();
ta[(i-1)%3].Push(ch);
cout << ++k << ": " << "Move disk " << ch << " from " << ta[(i+1)%3].name << " to " << ta[(i-1)%3].name << endl;
}
}
}
}
ËÄ¡¢ ±È½ÏÓë×ܽá
ººÅµËþÎÊÌâÊǾµäµÄµÝ¹éËã·¨Àý×Ó¡£±¾Îĸø³öÁ˵ݹéºÍ·ÇµÝ¹éÁ½ÖÖ½â¾öººÅµËþÎÊÌâµÄËã·¨¡£µÝ¹éµÄººÅµËþÎÊÌâ½â·¨ÓŵãÊÇÂß¼ÇåÎú£¬Ò×ÓÚÀí½â£¬¿É¶ÁÐԺã¬Ëã·¨Óï¾äÉÙ£¬Ò²ÓÐÖúÓÚÀí½âµÝ¹éµÄ˼Ï룬µ«ÐèÒª´óÁ¿µÄÕ»¿Õ¼ä£¬ÔËÐÐЧÂʲ»¸ß¡£Èç¹ûÓ÷ǵݹ鷽·¨½â¾öººÅµËþÎÊÌâµÄËã·¨£¬ÓŵãÊǽöÓÃÉÙÁ¿µÄ´æ´¢¿Õ¼ä¿Ë·þÁ˵ݹéËã·¨µÄ²»×㣬¼æ¹ËÁËʱ¼äÓë¿Õ¼äµÄЧÂÊ£¬È±µãÊÇÀí½âÆðÀ´¿ÉÄܱȽÏÀ§ÄÑ£¬¿É¶ÁÐÔÒ²²»ÊǺܺã¬ÔÚʵ¼ÊÖУ¬¿É¸ù¾ÝÐèҪѡÔñÒ»ÖÖ½øÐнâ¾ö¡£
²Î¿¼ÎÄÏ×
[1] Íõ´ºÉ£®³ÌÐòÔ±½Ì³Ì[M]£® ±±¾©£ºÇ廪´óѧ³ö°æÉ磬2001-05£®
[2] ÖÜÃô. ººÅµËþÎÊÌâµÄËã·¨·ÖÎö¼°CÓïÑÔʵÏÖ[j].µçÄÔѧϰ£¬2009Äê10ÔÂ
[3] ̸Ïé°Ø Öø.ÊýѧӪÑø²Ë[M]. ÖйúÉÙÄê¶ùͯ³ö°æÉç,2004-5-1
ÏÂһƪ£ºÇ³Ì¸ÈçºÎÉϺÃÖÐѧÒôÀÖÐÀÉÍ¿Î