0%

[題解]三色河內塔

a117/ 三色河內塔

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>
#define int long long
#define N 16
#define mod 1000000007
#define FOR(i,n) for(int i=0;i<n;i++)
#define ios ios::sync_with_stdio(0)
using namespace std;
int t;

void move(int n,char from,char to,char by){
if(n<1)return;
move(n-1,from,by,to);
cout<<" ring "<<n<<" : "<<from<<" => "<<to<<endl;t++;
move(n-1,by,to,from);
}

void honai(int n,char from,char to,char by){
if(n<1)return;
move(n-1,from,by,to);
cout<<" ring "<<n<<" : "<<from<<" => "<<to<<endl;t++;
honai(n-2,by,from,to);
}
signed main(){
ios;
int n;cin>>n;
t = 0;
honai(n,'A','C','B');
cout<<"共需"<<t<<"個移動"<<endl;
}