-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcq_23.cpp
More file actions
77 lines (74 loc) · 2.36 KB
/
Copy pathcq_23.cpp
File metadata and controls
77 lines (74 loc) · 2.36 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
#include <iostream>
#include <queue>
#define INF 1000000000
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int t; cin >> t;
while (t--) {
int w, h; cin >> h >> w;
char grid[w][h];
int dist[w][h];
pair<int, int> st;
pair<int, int> par[w][h];
bool mark[w][h];
string s; getline(cin, s);
for (int i = 0; i < w; i++) {
getline(cin, s);
for (int j = 0; j < h; j++) {
grid[i][j] = ' ';
dist[i][j] = INF;
mark[i][j] = 0;
}
for (int j = 0; j < s.length(); j++) {
grid[i][j] = s[j];
if (s[j] == 'o') st = make_pair(i, j);
}
}
queue<pair<int, int> > q;
q.push(st);
dist[st.first][st.second] = 0;
int dir[4][2] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
pair<int, int> bestp;
int bestd = INF;
while (!q.empty()) {
pair<int, int> p = q.front(); q.pop();
int x = p.first, y = p.second;
int d = dist[x][y];
if (d > bestd) break;
char ch = grid[x][y];
if (ch == 'X') {
if (bestd == INF || bestp.first + bestp.second > x + y) {
bestp = p;
bestd = d;
}
continue;
}
for (int i = 0; i < 4; i++) {
int x2 = x + dir[i][0];
int y2 = y + dir[i][1];
if (x2 >= 0 && x2 < w && y2 >= 0 && y2 < h) {
if (dist[x2][y2] == INF && grid[x2][y2] != '#') {
par[x2][y2] = make_pair(x, y);
dist[x2][y2] = d + 1;
q.push(make_pair(x2, y2));
}
}
}
}
pair<int, int> cur = bestp;
while (cur != st) {
mark[cur.first][cur.second] = 1;
cur = par[cur.first][cur.second];
}
for (int i = 0; i < w; i++) {
int last = 0;
for (int j = 0; j < h; j++) if (grid[i][j] != ' ') last = j;
for (int j = 0; j <= last; j++) {
if (grid[i][j] == ' ' && mark[i][j]) cout << '.';
else cout << grid[i][j];
} cout << endl;
}
}
return 0;
}