fork download
  1. #include<bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define pp push_back
  5. #define endl '\n'
  6. #define all(x) x.begin(),x.end()
  7. #define ld long double
  8. #define PI acos(-1)
  9. #define ones(x) __builtin_popcountll(x)
  10. //#define int ll
  11.  
  12. using namespace std;
  13.  
  14. void Drakon() {
  15. ios_base::sync_with_stdio(false);
  16. cin.tie(nullptr);
  17. cout.tie(nullptr);
  18. #ifdef Clion
  19. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  20. #endif
  21. }
  22.  
  23. unsigned long long inf = 1e10;
  24. const double EPS = 1e-6;
  25. const int MOD = 1000000007, N = 200005, LOG = 25;
  26.  
  27. ll mul(const ll &a, const ll &b) {
  28. return (a % MOD + MOD) * (b % MOD + MOD) % MOD;
  29. }
  30.  
  31. ll add(const ll &a, const ll &b) {
  32. return (a + b + 2 * MOD) % MOD;
  33. }
  34.  
  35. ll pw(ll x, ll y) {
  36. ll ret = 1;
  37. while (y > 0) {
  38. if (y % 2 == 0) {
  39. x = mul(x, x);
  40. y = y / 2;
  41. } else {
  42. ret = mul(ret, x);
  43. y = y - 1;
  44. }
  45. }
  46. return ret;
  47. }
  48.  
  49. set<vector<vector<int>>> s;
  50. int n;
  51. vector<pair<int, int>> bestMoves;
  52.  
  53. void slv(vector<vector<int>> tower, vector<pair<int, int>> moves) {
  54. if(tower.back().size() == n) {
  55. if(moves.size() < bestMoves.size() || bestMoves.empty()) bestMoves = moves;
  56. return;
  57. }
  58. if(s.count(tower)) return;
  59. s.insert(tower);
  60. for(int i = 0; i < 3; ++i) {
  61. for(int j = 0; j < 3; ++j) {
  62. if(i == j || tower[i].empty()) continue;
  63. int back = tower[i].back();
  64. if(tower[j].empty() || back < tower[j].back()) {
  65. tower[j].push_back(back);
  66. tower[i].pop_back();
  67. moves.push_back({i + 1, j + 1});
  68.  
  69. slv(tower, moves);
  70.  
  71. tower[j].pop_back();
  72. tower[i].push_back(back);
  73. moves.pop_back();
  74. }
  75. }
  76. }
  77. }
  78.  
  79. void solve() {
  80. cin >> n;
  81. vector<vector<int>> tower = vector<vector<int>>(3);
  82. for (int i = n; i >= 1; --i) {
  83. tower[0].push_back(i);
  84. }
  85. slv(tower, {});
  86. cout << bestMoves.size() << endl;
  87. for(auto &[x, y] : bestMoves) cout << x << ' ' << y << endl;
  88. }
  89.  
  90. signed main() {
  91. Drakon();
  92. int t = 1;
  93. //cin >> t;
  94. while (t--) {
  95. solve();
  96. }
  97. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
0