#include<iostream> using namespace std; //汉罗塔递归求解函数 从a移到c void move(int m,char a,char c); void hanoi(int n,char a,char b,char c) { if(1==n) { move(n,a,c); return; } hanoi(n-1,a,c,b); move(n,a,c); hanoi(n-1,b,a,c); } //将编号为m的圆盘从a移到c void move(int m,char a,char c) { cout<<m<<" : "<<a<<"---->"<<c<<endl; } int main() { char a = ‘A‘,b = ‘B‘,c = ‘C‘; hanoi(3,a,b,c); return 0; }
递归法求解汉罗塔hanoi问题
时间: 2024-11-06 07:12:48