这个例子写的太多了,到处都是,不过作为自己的笔记还是贴出来,如果大家的数据结构教材上的代码调试不通的话,这个代码还是有点作用的^_^, 另外个人觉得这个例子也确实是递归的经典用途
![](/Emoticons/regular_smile.gif)
,下面的代码参考了<<c程序设计的抽象思维>>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2005/12/24
created: 24:12:2005 10:42
filename: hanoi.c
author: Liu Qi
purpose: hanoi problem
*********************************************************************/
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
#include <stdio.h>
#include <assert.h>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
#define COUNT 3
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*===========================================================================
* Function name: MoveSingleDisk
* Parameter: start:从start柱子开始,移动到finish柱子
* Precondition: void
* Description: 如果只有一个盘子,直接从开始得那根柱子移动到结束得柱子就可以了
* Return value: void
* Author: Liu Qi, [12/24/2005]
===========================================================================*/
void MoveSingleDisk(char start, char finish)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
printf("%c -> %c\n", start, finish);
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*===========================================================================
* Function name: MoveTower
* Parameter: count:number of disks, start:开始的那根柱子![](/Images/dot.gif)
* Precondition: count > 0
* Description: 将count个disk从start移动到finish,借助temp
* Return value: void
* Author: Liu Qi, [12/24/2005]
===========================================================================*/
void MoveTower(int count, char start, char finish, char temp)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
assert( count > 0 );
![](/Images/OutliningIndicators/InBlock.gif)
if (count == 1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MoveSingleDisk(start, finish);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MoveTower(count - 1, start, temp, finish);
MoveSingleDisk(start, finish);
MoveTower(count - 1, temp, finish, start);
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
int main(int argc, char *argv[])
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
MoveTower(COUNT, 'A', 'B', 'C');
return 0;
}运行结果如下:
![hanio](/ngaut/admin/FtbWebResource.axd?a=FreeTextBox%2c+Version%3d3.1.1.34886%2c+Culture%3dneutral%2c+PublicKeyToken%3d5962a4e684a48b87&r=FreeTextBoxControls.Resources.Images.Utility.spacer.gif&t=632699064217187500)