-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25vlqg3_pour.cpp
More file actions
78 lines (66 loc) · 1.96 KB
/
Copy path25vlqg3_pour.cpp
File metadata and controls
78 lines (66 loc) · 1.96 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
using State = array<int, 3>;
struct MoveInfo {
State prev;
int from;
int to;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
State init;
if (!(cin >> init[0] >> init[1] >> init[2])) return 0;
if (init[0] == 0 || init[1] == 0 || init[2] == 0) {
cout << 0;
return 0;
}
queue<State> q;
unordered_map<string, MoveInfo> parent;
auto encode = [&](const State &s){ return to_string(s[0]) + "," + to_string(s[1]) + "," + to_string(s[2]); };
string init_key = encode(init);
parent[init_key] = { {}, -1, -1 };
q.push(init);
State goal;
bool found = false;
while (!q.empty() && !found) {
State cur = q.front(); q.pop();
for (int i = 0; i < 3 && !found; ++i) {
for (int j = 0; j < 3 && !found; ++j) {
if (i == j) continue;
int vi = cur[i], vj = cur[j];
if (vj <= 0) continue;
if (vi < vj) continue;
State nxt = cur;
nxt[i] = vi - vj;
nxt[j] = vj * 2;
string key = encode(nxt);
if (parent.count(key)) continue;
parent[key] = { cur, i, j };
if (nxt[0] == 0 || nxt[1] == 0 || nxt[2] == 0) {
goal = nxt;
found = true;
break;
}
q.push(nxt);
}
}
}
if (!found) {
cout << "-1";
return 0;
}
vector<pair<int,int>> moves;
string key = encode(goal);
while (true) {
MoveInfo info = parent[key];
if (info.from == -1) break;
moves.emplace_back(info.from + 1, info.to + 1);
key = encode(info.prev);
}
reverse(moves.begin(), moves.end());
cout << moves.size() << '\n';
for (auto &mv : moves) {
cout << mv.first << ' ' << mv.second << '\n';
}
}