文章
68
粉丝
691
获赞
26
访问
575.6k
https://blog.csdn.net/csyifanZhang/article/details/106075180
↑完整题解
#define ll int
#define inf 0x3ffffff
#define vec vector<ll>
ll tot = 0;
void pprint(char s, char t) {
if (tot == 5)printf("\n"), tot = 0;
printf("%c-->%c ", s, t); tot++;
}
//将cnt个圆盘从S盘经过M移动到T
void hanoi(char S, char T, char M, ll cnt) {
if (cnt == 1)
pprint(S, T);
else {
hanoi(S, M, T, cnt - 1);//把上面cnt-1个硬盘从S经过T移动到M
hanoi(S, T, M, 1);//把最底下的1个硬盘移动到T
hanoi(M, T, S, cnt - 1);//把移动到M上的cnt-1个硬盘通过S移到终点T
}
}
int main() {
ll n;
while (cin >> n && n) {
hanoi('A', 'C', 'B', n);
printf("\n"); tot = 0;
}
}
登录后发布评论
暂无评论,来抢沙发