-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathE.cpp
94 lines (89 loc) · 3.01 KB
/
E.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n;
cin >> n;
bool a[8][8]{};
for (int i = 0; i < n; ++i) {
char r, f;
cin >> r >> f;
a[r - 'a'][f - '1'] = true;
}
int dx[8]{-2, -2, -1, -1, 1, 1, 2, 2};
int dy[8]{1, -1, 2, -2, 2, -2, 1, -1};
auto check = [](int x, int y) {
return 0 <= x && x < 8 && 0 <= y && y < 8;
};
vector<string> ans;
for (int i = 0; i < n; ++i) {
int r = i % 8, f = i / 8;
if (a[r][f]) continue;
pair<int, int> p[8][8]{};
p[r][f] = {-1, -1};
bool vis[8][8]{};
vis[r][f] = true;
queue<pair<int, int>> q;
q.emplace(r, f);
vector<string> cans;
while (!q.empty()) {
auto[x, y] = q.front();
q.pop();
int j = x + 8 * y;
if (a[x][y] && j > i) {
vector<pair<int, int>> path;
int cx = x, cy = y;
while (~cx && ~cy) {
path.emplace_back(cx, cy);
auto l = p[cx][cy];
cx = l.first, cy = l.second;
}
std::reverse(path.begin(), path.end());
for (int k = 0; k < path.size()-1; ++k) {
cx = path[k].first, cy = path[k].second;
int nx = path[k + 1].first, ny = path[k + 1].second;
string cur, nxt;
cur.push_back('a' + cx);
cur.push_back('1' + cy);
nxt.push_back('a' + nx);
nxt.push_back('1' + ny);
if (a[nx][ny] && (nx != x || ny != y)) cans.push_back(nxt+"-"+cur);
}
for (int k = path.size()-2; ~k; --k) {
cx = path[k].first, cy = path[k].second;
int nx = path[k + 1].first, ny = path[k + 1].second;
string cur, nxt;
cur.push_back('a' + cx);
cur.push_back('1' + cy);
nxt.push_back('a' + nx);
nxt.push_back('1' + ny);
if (!(a[nx][ny] && (nx != x || ny != y))) cans.push_back(nxt+"-"+cur);
}
a[x][y] = false;
a[r][f] = true;
break;
}
for (int k = 0; k < 8; ++k) {
int nx = x + dx[k], ny = y + dy[k];
if (!check(nx, ny) || vis[nx][ny]) continue;
vis[nx][ny] = true;
q.emplace(nx, ny);
p[nx][ny] = {x, y};
}
}
for (auto &s : cans)
ans.push_back(s);
}
cout << ans.size() << '\n';
for (auto &s : ans)
cout << s << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tc = 1;
// cin >> tc;
while (tc--) {
solve();
}
}