#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ar array
const int mxN = 800;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int n, K;
vector<string> g(mxN);
pair<int,int> mecho, home;
vector<pair<int,int>> hives;
vector<vector<int>> disthives(mxN, vector<int>(mxN, (int) 1e9)), dist(mxN, vector<int>(mxN, (int) 1e9));
bool checkhives(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n && g[x][y] == 'G' && disthives[x][y] == (int) 1e9;
}
bool checkbeer(int DIST, int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n && g[x][y] != 'T' && g[x][y] != 'H' && disthives[x][y] > DIST && dist[x][y] == (int) 1e9;
}
void bfs(int m) {
priority_queue<ar<int, 3>, vector<ar<int, 3>>, greater<ar<int, 3>>> pq;
pq.push({m, mecho.first, mecho.second});
while (!pq.empty()) {
int d = pq.top()[0], x = pq.top()[1], y = pq.top()[2]; pq.pop();
if (dist[x][y] < d) {
continue;
}
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k], ny = y + dy[k];
int DIST = (d + 1) / K;
// if ((d + 1) % K) {
// ++DIST;
// }
if (checkbeer(DIST, nx, ny)) {
pq.push({d + 1, nx, ny});
dist[nx][ny] = d + 1;
}
}
}
// cout << dist[home.first][home.second] << '\n';
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < n; ++j) {
// cout << dist[i][j] << ' ';
// }
// cout << '\n';
// }
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> K;
for (int i = 0; i < n; ++i) {
cin >> g[i];
for (int j = 0; j < n; ++j) {
if (g[i][j] == 'M') {
mecho = {i, j};
} else if (g[i][j] == 'D') {
home = {i, j};
} else if (g[i][j] == 'H') {
hives.push_back({i, j});
disthives[i][j] = 0;
}
}
}
priority_queue<ar<int, 3>, vector<ar<int, 3>>, greater<ar<int, 3>>> pq;
for (auto i : hives) {
pq.push({0, i.first, i.second});
}
while (!pq.empty()) {
int d = pq.top()[0], x = pq.top()[1], y = pq.top()[2]; pq.pop();
if (disthives[x][y] < d) {
continue;
}
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k], ny = y + dy[k];
if (checkhives(nx, ny)) {
pq.push({d + 1, nx, ny});
disthives[nx][ny] = d + 1;
}
}
}
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < n; ++j) {
// cout << disthives[i][j] << ' ';
// }
// cout << '\n';
// }
// cerr << "HELLO\n";
int l = 0, r = mxN * mxN;
while (l <= r) {
int m = (l + r) >> 1;
dist = vector<vector<int>>(mxN, vector<int>(mxN, (int) 1e9));
dist[mecho.first][mecho.second] = m * K;
bfs(m * K);
if (dist[home.first][home.second] < 1e9) {
l = m + 1;
} else {
r = m - 1;
}
}
cout << l - 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
15512 KB |
Output is correct |
2 |
Correct |
29 ms |
15532 KB |
Output is correct |
3 |
Correct |
33 ms |
15532 KB |
Output is correct |
4 |
Correct |
31 ms |
15508 KB |
Output is correct |
5 |
Correct |
32 ms |
15492 KB |
Output is correct |
6 |
Correct |
29 ms |
15584 KB |
Output is correct |
7 |
Correct |
466 ms |
16592 KB |
Output is correct |
8 |
Correct |
32 ms |
15552 KB |
Output is correct |
9 |
Correct |
34 ms |
15496 KB |
Output is correct |
10 |
Correct |
30 ms |
15520 KB |
Output is correct |
11 |
Correct |
29 ms |
15516 KB |
Output is correct |
12 |
Incorrect |
32 ms |
15504 KB |
Output isn't correct |
13 |
Incorrect |
30 ms |
15544 KB |
Output isn't correct |
14 |
Correct |
30 ms |
15532 KB |
Output is correct |
15 |
Correct |
29 ms |
15500 KB |
Output is correct |
16 |
Correct |
29 ms |
15536 KB |
Output is correct |
17 |
Correct |
31 ms |
15516 KB |
Output is correct |
18 |
Correct |
30 ms |
15500 KB |
Output is correct |
19 |
Correct |
30 ms |
15532 KB |
Output is correct |
20 |
Correct |
31 ms |
15516 KB |
Output is correct |
21 |
Correct |
30 ms |
15516 KB |
Output is correct |
22 |
Correct |
31 ms |
15512 KB |
Output is correct |
23 |
Correct |
30 ms |
15516 KB |
Output is correct |
24 |
Correct |
30 ms |
15548 KB |
Output is correct |
25 |
Correct |
33 ms |
15508 KB |
Output is correct |
26 |
Correct |
31 ms |
15564 KB |
Output is correct |
27 |
Correct |
30 ms |
15560 KB |
Output is correct |
28 |
Correct |
34 ms |
15500 KB |
Output is correct |
29 |
Correct |
33 ms |
15504 KB |
Output is correct |
30 |
Correct |
39 ms |
15520 KB |
Output is correct |
31 |
Correct |
25 ms |
15516 KB |
Output is correct |
32 |
Correct |
31 ms |
15500 KB |
Output is correct |
33 |
Correct |
36 ms |
15668 KB |
Output is correct |
34 |
Correct |
27 ms |
15612 KB |
Output is correct |
35 |
Correct |
122 ms |
15660 KB |
Output is correct |
36 |
Correct |
37 ms |
15752 KB |
Output is correct |
37 |
Correct |
29 ms |
15680 KB |
Output is correct |
38 |
Correct |
153 ms |
15736 KB |
Output is correct |
39 |
Correct |
26 ms |
15692 KB |
Output is correct |
40 |
Correct |
38 ms |
15776 KB |
Output is correct |
41 |
Correct |
197 ms |
15776 KB |
Output is correct |
42 |
Correct |
27 ms |
15776 KB |
Output is correct |
43 |
Correct |
37 ms |
15844 KB |
Output is correct |
44 |
Correct |
230 ms |
15824 KB |
Output is correct |
45 |
Correct |
35 ms |
15900 KB |
Output is correct |
46 |
Correct |
47 ms |
15848 KB |
Output is correct |
47 |
Correct |
266 ms |
15876 KB |
Output is correct |
48 |
Correct |
37 ms |
15964 KB |
Output is correct |
49 |
Correct |
32 ms |
15884 KB |
Output is correct |
50 |
Correct |
325 ms |
16188 KB |
Output is correct |
51 |
Correct |
41 ms |
16068 KB |
Output is correct |
52 |
Correct |
35 ms |
15944 KB |
Output is correct |
53 |
Correct |
414 ms |
16032 KB |
Output is correct |
54 |
Correct |
41 ms |
16084 KB |
Output is correct |
55 |
Correct |
37 ms |
16024 KB |
Output is correct |
56 |
Correct |
461 ms |
16144 KB |
Output is correct |
57 |
Correct |
49 ms |
16280 KB |
Output is correct |
58 |
Correct |
45 ms |
16160 KB |
Output is correct |
59 |
Correct |
539 ms |
16240 KB |
Output is correct |
60 |
Correct |
50 ms |
16248 KB |
Output is correct |
61 |
Correct |
54 ms |
16240 KB |
Output is correct |
62 |
Correct |
626 ms |
16256 KB |
Output is correct |
63 |
Correct |
357 ms |
16324 KB |
Output is correct |
64 |
Correct |
635 ms |
16256 KB |
Output is correct |
65 |
Correct |
484 ms |
16240 KB |
Output is correct |
66 |
Correct |
381 ms |
16300 KB |
Output is correct |
67 |
Correct |
372 ms |
16256 KB |
Output is correct |
68 |
Correct |
160 ms |
16352 KB |
Output is correct |
69 |
Correct |
158 ms |
16428 KB |
Output is correct |
70 |
Correct |
129 ms |
16356 KB |
Output is correct |
71 |
Correct |
143 ms |
16352 KB |
Output is correct |
72 |
Incorrect |
126 ms |
16400 KB |
Output isn't correct |
73 |
Incorrect |
367 ms |
17104 KB |
Output isn't correct |
74 |
Correct |
287 ms |
17096 KB |
Output is correct |
75 |
Correct |
373 ms |
17076 KB |
Output is correct |
76 |
Correct |
314 ms |
17152 KB |
Output is correct |
77 |
Correct |
300 ms |
17112 KB |
Output is correct |
78 |
Correct |
375 ms |
17020 KB |
Output is correct |
79 |
Correct |
263 ms |
16984 KB |
Output is correct |
80 |
Correct |
318 ms |
16988 KB |
Output is correct |
81 |
Correct |
329 ms |
17004 KB |
Output is correct |
82 |
Correct |
286 ms |
16968 KB |
Output is correct |
83 |
Correct |
433 ms |
16948 KB |
Output is correct |
84 |
Correct |
385 ms |
16852 KB |
Output is correct |
85 |
Correct |
393 ms |
16860 KB |
Output is correct |
86 |
Correct |
395 ms |
16856 KB |
Output is correct |
87 |
Correct |
404 ms |
16912 KB |
Output is correct |
88 |
Correct |
432 ms |
16812 KB |
Output is correct |
89 |
Correct |
432 ms |
16752 KB |
Output is correct |
90 |
Correct |
463 ms |
16828 KB |
Output is correct |
91 |
Correct |
456 ms |
16792 KB |
Output is correct |
92 |
Correct |
482 ms |
16788 KB |
Output is correct |