第一步:将n-1个盘子从A柱移动至B柱(借助C柱为过渡柱)
第二步:将A柱底下最大的盘子移动至C柱
第三步:将B柱的n-1个盘子移至C柱(借助A柱为过渡柱)
include <iostream>
#include <stdio.h>using namespace std;
static int step = 0;
void move ( char sour, char dest )
{
printf ( "move from %c to %c \n", sour, dest );
}
void hanoi ( int n, char sour, char temp, char dest )
{
if ( n == 1 )
{
move ( sour, dest );
++step;
}
else
{
hanoi ( n-1, sour, dest, temp );
move ( sour,dest );
++step;
hanoi ( n-1, temp, sour, dest );
}
int main ( int argc, char **argv )
{
int n = 4;
hanoi ( n, 'A', 'B', 'C' );
printf ( "Total steps is %d\n", step );
return 0;
}
}
或者:
- #include<cstdio>
- int i; //记录步数
- //i表示进行到的步数,将编号为n的盘子由from柱移动到to柱(目标柱)
- void move(int n,char from,char to){
- printf("第%d步:将%d号盘子%c---->%c\n",i++,n,from,to);
- }
- //汉诺塔递归函数
- //n表示要将多少个"圆盘"从起始柱子移动至目标柱子
- //start_pos表示起始柱子,tran_pos表示过渡柱子,end_pos表示目标柱子
- void Hanio(int n,char start_pos,char tran_pos,char end_pos){
- if(n==1){ //很明显,当n==1的时候,我们只需要直接将圆盘从起始柱子移至目标柱子即可.
- move(n,start_pos,end_pos);
- }
- else{
- Hanio(n-1,start_pos,end_pos,tran_pos); //递归处理,一开始的时候,先将n-1个盘子移至过渡柱上
- move(n,start_pos,end_pos); //然后再将底下的大盘子直接移至目标柱子即可
- Hanio(n-1,tran_pos,start_pos,end_pos); //然后重复以上步骤,递归处理放在过渡柱上的n-1个盘子
- //此时借助原来的起始柱作为过渡柱(因为起始柱已经空了)
- }
- }
- int main(){
- int n;
- while(scanf("%d",&n)==1&&n){
- i = 1; //全局变量赋初始值
- Hanio(n,'1','2','3');
- printf("最后总的步数为%d\n",i-1);
- }
- return 0;