-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31248.cpp
More file actions
57 lines (50 loc) · 1.13 KB
/
31248.cpp
File metadata and controls
57 lines (50 loc) · 1.13 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef pair<char, char>P;
int moveCount = 0;
vector<P> v;
void move(char start, char end) {
v.push_back(P(start, end));
moveCount++;
}
void hanoi(int n, char now, char next, char tmp) {
if (n == 1) {
move(now, next);
return;
}
hanoi(n - 1, now, tmp, next);
move(now, next);
hanoi(n - 1, tmp, next, now);
}
void modHanoi(int n, char now, char next, char tail, char dest) {
if (n == 1) {
move(now, dest);
return;
}
else if(n == 2){
move(now, next);
move(now, dest);
move(next, dest);
return;
}
hanoi(n - 2, now, next, tail);
move(now, tail);
move(now, dest);
move(tail, dest);
modHanoi(n - 2, next, now, tail, dest);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
modHanoi(N, 'A', 'B', 'C', 'D');
cout << moveCount << "\n";
for (P p : v) {
cout << p.first << " " << p.second << "\n";
}
return 0;
}