7 more
This commit is contained in:
38
CSES - CSES Problem Set/Tower_of_Hanoi.cpp
Normal file
38
CSES - CSES Problem Set/Tower_of_Hanoi.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// Tower of Hanoi
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
void hanoi(deque<int> ¤t, deque<int> &destination, deque<int> &tmp, int n, vector<pair<int, int>> &remember, tuple<int, int, int> stack_names) {
|
||||
if (n == 1) {
|
||||
auto top = current.back();
|
||||
current.pop_back();
|
||||
destination.push_back(top);
|
||||
remember.push_back({get<0>(stack_names), get<1>(stack_names)});
|
||||
return;
|
||||
}
|
||||
hanoi(current, tmp, destination, n - 1, remember, {get<0>(stack_names), get<2>(stack_names), get<1>(stack_names)});
|
||||
auto top = current.back();
|
||||
current.pop_back();
|
||||
destination.push_back(top);
|
||||
remember.push_back({get<0>(stack_names), get<1>(stack_names)});
|
||||
hanoi(tmp, destination, current, n - 1, remember, {get<2>(stack_names), get<1>(stack_names), get<0>(stack_names)});
|
||||
return ;
|
||||
};
|
||||
|
||||
int main() {
|
||||
int size;
|
||||
cin >> size;
|
||||
deque<int> start;
|
||||
deque<int> s2;
|
||||
deque<int> s3;
|
||||
for (int i = size; i > 0; i--) {
|
||||
start.push_front(i);
|
||||
}
|
||||
vector<pair<int, int>> r; //c, t, d
|
||||
hanoi(start, s2, s3, size, r, {1, 3, 2});
|
||||
cout << r.size() << endl;
|
||||
for (auto &[a, b]: r) cout << a << " " << b << endl;
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user