forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhidingplaces.cpp
74 lines (62 loc) · 1.61 KB
/
hidingplaces.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
#include <bits/stdc++.h>
using namespace std;
int inf = 1 << 15;
bool inrange(int i, int j) {
return i >= 0 && j >= 0 && i < 8 && j < 8;
}
void bfs(vector<vector<int>>& dist, int starti, int startj) {
queue<pair<int,int>> q;
q.push({starti,startj});
dist[starti][startj] = 0;
vector<int> dx = {1,2,2,1,-1,-2,-2,-1};
vector<int> dy = {-2,-1,1,2,2,1,-1,-2};
while(!q.empty()) {
int curri = q.front().first;
int currj = q.front().second;
q.pop();
for(int k = 0; k < 8; k++) {
int nexti = curri + dx[k];
int nextj = currj + dy[k];
if(!inrange(nexti,nextj)) continue;
if(dist[nexti][nextj] > dist[curri][currj] + 1) {
dist[nexti][nextj] = dist[curri][currj] + 1;
q.push({nexti,nextj});
}
}
}
}
void solve() {
string s;
cin >> s;
// Run BFS
vector<vector<int>> dist(8, vector<int>(8,inf));
bfs(dist, s[0]-'a', s[1]-'1');
// Find farthest dist, print it
int far = 0;
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
far = max(far, dist[i][j]);
}
}
cout << far << " ";
// Find all at that dist
vector<string> ans;
for(int j = 7; j >= 0; j--) {
for(int i = 0; i < 8; i++) {
if(dist[i][j] == far) {
string here;
here += i+'a';
here += j+'1';
cout << here << " ";
}
}
}
cout << endl;
}
int main() {
int cases;
cin >> cases;
while(cases--) {
solve();
}
}