0%

[題解]CSES 2165 Tower of Hanoi

Tower of Hanoi

題目連結

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0)
using namespace std;
int n;

void solve(int from,int to,int by,int n){
if(n < 1)return;
solve(from,by,to,n-1);
cout<<from<<" "<<to<<"\n";
solve(by,to,from,n-1);
}

signed main(){
IOS;
cin>>n;
cout<< (1<<n)-1 <<"\n";
solve(1,3,2,n);
return 0;
}