#include <bits/stdc++.h>
using namespace std;
#define task ""
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (b), _a = (a); i >= _a; --i)
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const int MAX = 800;
int n, S;
int xM, yM;
int xD, yD;
char a[MAX][MAX];
int bee[MAX][MAX];
int bear[MAX][MAX];
queue<pair<int, int>> q;
bool ok(int add) {
if (bee[xM][yM] <= add) return false;
memset(bear, -1, sizeof bear);
q.push({xM, yM});
bear[xM][yM] = 0;
while (q.size()) {
int u = q.front().fi;
int v = q.front().se;
q.pop();
for (int i = 0; i < 4; ++i) {
int x = u + dx[i], y = v + dy[i];
if (x < 0 || x >= n) continue;
if (y < 0 || y >= n) continue;
if (a[x][y] == 'M' || a[x][y] == 'G') {
int candidate = bear[u][v] + 1;
if (candidate / S < bee[x][y] - add) {
if (bear[x][y] == -1) {
q.push({x, y});
bear[x][y] = candidate;
}
}
}
}
}
for (int i = 0; i < 4; ++i) {
int x = xD + dx[i], y = yD + dy[i];
if (x < 0 || x >= n) continue;
if (y < 0 || y >= n) continue;
if (bear[x][y] == -1) continue;
if (a[x][y] == 'G' || a[x][y] == 'M') {
if (bear[x][y] / S < bee[x][y] - add) return true;
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> S;
memset(bee, -1, sizeof bee);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> a[i][j];
if (a[i][j] == 'H') {
bee[i][j] = 0;
q.push({i, j});
}
if (a[i][j] == 'D') {
xD = i, yD = j;
}
if (a[i][j] == 'M') {
xM = i, yM = j;
}
}
}
//
while (q.size()) {
int u = q.front().fi;
int v = q.front().se;
q.pop();
for (int i = 0; i < 4; ++i) {
int x = u + dx[i], y = v + dy[i];
if (x < 0 || x >= n) continue;
if (y < 0 || y >= n) continue;
if (a[x][y] == 'G' || a[x][y] == 'M') {
if (bee[x][y] == -1) {
q.push({x, y});
bee[x][y] = bee[u][v] + 1;
}
}
}
}
//
int l = 0;
int r = n * n;
while (l <= r) {
int mid = l + r >> 1;
if (ok(mid)) l = mid + 1;
else r = mid - 1;
}
cout << l - 1;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:115:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
115 | int mid = l + r >> 1;
| ~~^~~
mecho.cpp:72:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:73:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5204 KB |
Output is correct |
2 |
Correct |
2 ms |
5332 KB |
Output is correct |
3 |
Correct |
2 ms |
5332 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
2 ms |
5332 KB |
Output is correct |
7 |
Correct |
136 ms |
6000 KB |
Output is correct |
8 |
Correct |
3 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5332 KB |
Output is correct |
10 |
Correct |
2 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5332 KB |
Output is correct |
12 |
Correct |
3 ms |
5332 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Correct |
3 ms |
5332 KB |
Output is correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
3 ms |
5332 KB |
Output is correct |
17 |
Correct |
3 ms |
5332 KB |
Output is correct |
18 |
Correct |
3 ms |
5252 KB |
Output is correct |
19 |
Correct |
3 ms |
5332 KB |
Output is correct |
20 |
Correct |
3 ms |
5284 KB |
Output is correct |
21 |
Correct |
3 ms |
5332 KB |
Output is correct |
22 |
Correct |
3 ms |
5332 KB |
Output is correct |
23 |
Correct |
3 ms |
5332 KB |
Output is correct |
24 |
Correct |
3 ms |
5332 KB |
Output is correct |
25 |
Correct |
3 ms |
5276 KB |
Output is correct |
26 |
Correct |
4 ms |
5376 KB |
Output is correct |
27 |
Correct |
4 ms |
5332 KB |
Output is correct |
28 |
Correct |
4 ms |
5332 KB |
Output is correct |
29 |
Correct |
3 ms |
5332 KB |
Output is correct |
30 |
Correct |
4 ms |
5332 KB |
Output is correct |
31 |
Correct |
3 ms |
5328 KB |
Output is correct |
32 |
Correct |
3 ms |
5332 KB |
Output is correct |
33 |
Correct |
7 ms |
5544 KB |
Output is correct |
34 |
Correct |
7 ms |
5588 KB |
Output is correct |
35 |
Correct |
28 ms |
5612 KB |
Output is correct |
36 |
Correct |
7 ms |
5588 KB |
Output is correct |
37 |
Correct |
12 ms |
5648 KB |
Output is correct |
38 |
Correct |
48 ms |
5628 KB |
Output is correct |
39 |
Correct |
10 ms |
5620 KB |
Output is correct |
40 |
Correct |
11 ms |
5588 KB |
Output is correct |
41 |
Correct |
51 ms |
5692 KB |
Output is correct |
42 |
Correct |
11 ms |
5716 KB |
Output is correct |
43 |
Correct |
12 ms |
5704 KB |
Output is correct |
44 |
Correct |
64 ms |
5716 KB |
Output is correct |
45 |
Correct |
12 ms |
5716 KB |
Output is correct |
46 |
Correct |
13 ms |
5760 KB |
Output is correct |
47 |
Correct |
74 ms |
5852 KB |
Output is correct |
48 |
Correct |
14 ms |
5716 KB |
Output is correct |
49 |
Correct |
19 ms |
5728 KB |
Output is correct |
50 |
Correct |
76 ms |
5716 KB |
Output is correct |
51 |
Correct |
20 ms |
5844 KB |
Output is correct |
52 |
Correct |
17 ms |
5776 KB |
Output is correct |
53 |
Correct |
100 ms |
5776 KB |
Output is correct |
54 |
Correct |
21 ms |
5812 KB |
Output is correct |
55 |
Correct |
24 ms |
5820 KB |
Output is correct |
56 |
Correct |
126 ms |
5836 KB |
Output is correct |
57 |
Correct |
19 ms |
5844 KB |
Output is correct |
58 |
Correct |
22 ms |
5848 KB |
Output is correct |
59 |
Correct |
132 ms |
5912 KB |
Output is correct |
60 |
Correct |
23 ms |
5880 KB |
Output is correct |
61 |
Correct |
24 ms |
5888 KB |
Output is correct |
62 |
Correct |
178 ms |
5948 KB |
Output is correct |
63 |
Correct |
100 ms |
5940 KB |
Output is correct |
64 |
Correct |
190 ms |
5940 KB |
Output is correct |
65 |
Correct |
154 ms |
5936 KB |
Output is correct |
66 |
Correct |
117 ms |
5836 KB |
Output is correct |
67 |
Correct |
108 ms |
5844 KB |
Output is correct |
68 |
Correct |
51 ms |
5964 KB |
Output is correct |
69 |
Correct |
46 ms |
5912 KB |
Output is correct |
70 |
Correct |
37 ms |
5976 KB |
Output is correct |
71 |
Correct |
39 ms |
5924 KB |
Output is correct |
72 |
Correct |
28 ms |
5964 KB |
Output is correct |
73 |
Correct |
35 ms |
6168 KB |
Output is correct |
74 |
Correct |
67 ms |
6152 KB |
Output is correct |
75 |
Correct |
86 ms |
6096 KB |
Output is correct |
76 |
Correct |
71 ms |
6216 KB |
Output is correct |
77 |
Correct |
77 ms |
6148 KB |
Output is correct |
78 |
Correct |
86 ms |
6164 KB |
Output is correct |
79 |
Correct |
52 ms |
6100 KB |
Output is correct |
80 |
Correct |
66 ms |
6188 KB |
Output is correct |
81 |
Correct |
76 ms |
6220 KB |
Output is correct |
82 |
Correct |
70 ms |
6148 KB |
Output is correct |
83 |
Correct |
106 ms |
6152 KB |
Output is correct |
84 |
Correct |
84 ms |
6096 KB |
Output is correct |
85 |
Correct |
91 ms |
6100 KB |
Output is correct |
86 |
Correct |
95 ms |
6052 KB |
Output is correct |
87 |
Correct |
111 ms |
6152 KB |
Output is correct |
88 |
Correct |
95 ms |
6056 KB |
Output is correct |
89 |
Correct |
96 ms |
6092 KB |
Output is correct |
90 |
Correct |
105 ms |
6100 KB |
Output is correct |
91 |
Correct |
101 ms |
6164 KB |
Output is correct |
92 |
Correct |
120 ms |
6092 KB |
Output is correct |