OI-Codes/XSMOJ/hanoi(汉诺塔).cpp

31 lines
630 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <stack>
#include <ctime>
#include <cmath>
#include <queue>
typedef long long l;
typedef double d;
typedef char c;
using namespace std;
void s(char a,char b,char c,int n){
//参数a表示起始塔b表示辅助塔c表示目标塔n表示层数
if(n==1){
printf("%c->%c\n",a,c);//递归到1个盘子直接从起始塔到目标塔
return;
}
s(a,c,b,n-1);//先将n-1层 由A到B
printf("%c->%c\n",a,c);
s(b,a,c,n-1);//再将n-1层 由B到C
}
int main(){
char a,b,c;
int d;
cin >>d>>a>>b>>c;
s(a,b,c,d);
return 0;
}