汉诺塔

Author Avatar
Axell Jan 08, 2019
  • Read this article on other devices

题目描述

给出A塔上的盘子个数,输出最少的移动方案

代码

#include <bits/stdc++.h>
using namespace std;

void move(int n,char a,char b,char c){
    if (n==0) return;
    move(n-1,a,c,b);
    printf("move from %c to %c\n",a,c);
    move(n-1,b,a,c);
}

int main(){
    int n;
    cin>>n;
    cout<<pow(2,n)-1<<endl;
    move(n,'A','B','C');
    return 0;
}

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Link to this article: https://blog.axell.top/archives/52/