#include <stdio.h>
/*
思路:1、將1到n-1號盤子借助C移到B上
2、將n號盤子移到C上
3、將1到n-1號盤子借助A移到C上
*/
//初始化步數
int i = 0;
void move(int,char,char);
void hannuota(int,char,char,char);
void main(void){
int n;
printf("請輸入漢諾塔盤子的個數:");
scanf("%d",&n);
hannuota(n,'A','C','B');
}
//移動盤子方法
void move(int n,char from,char to){
printf("第%d步,將%d號盤子從%c移到%c\n",++i,n,from,to);
}
//漢諾塔遞歸算法
void hannuota(int n,char from,char to,char depend_on){
if(n == 1){
move(1,from,to);
}else{
hannuota(n-1,from,depend_on,to);
move(n,from,to);
hannuota(n-1,depend_on,to,from);
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。