tower_of_hanoi.cpp (633B)
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 using ui = unsigned int; 6 using l = long; 7 using ul = unsigned long; 8 using ll = long long; 9 using ull = unsigned long long; 10 11 using vi = vector<int>; 12 using vui = vector<ui>; 13 using vl = vector<l>; 14 using vul = vector<ul>; 15 using vll = vector<ll>; 16 using vull = vector<ull>; 17 18 void move(int n, int from, int to) { 19 if (n == 1) { 20 cout << from << " " << to << "\n"; 21 return; 22 } 23 24 move(n-1, from, 6-(from+to)); /* ;) */ 25 cout << from << " " << to << "\n"; 26 move(n-1, 6-(from+to), to); 27 } 28 29 void solve() 30 { 31 int n; 32 cin >> n; 33 cout << (1<<n) - 1 << "\n"; 34 move(n, 1, 3); 35 } 36 37 int main() 38 { 39 solve(); 40 }