#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 800 + 2;
const int inf = 1e9;
string s[N];
int b[N][N], a[N][N], n, k;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
bool Can(int x, int y) {
if (x < 0 || x >= n) return 0;
if (y < 0 || y >= n) return 0;
if (s[x][y] == 'T') return 0;
return 1;
}
bool Ok(int x, int y, int val, int m) {
return (b[x][y] - val / k - m) > 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int ii, jj, iii, jjj;
ii = jj = iii = jjj = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'M') {
ii = i;
jj = j;
}
if (s[i][j] == 'D') {
iii = i;
jjj = j;
}
}
}
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'H') {
q.push({i, j});
b[i][j] = 0;
}
else b[i][j] = inf;
}
}
while (!q.empty()) {
auto u = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = u.first + dx[i];
int ny = u.second + dy[i];
if (!Can(nx, ny) || (nx == iii && ny == jjj)) continue;
if (b[nx][ny] > b[u.first][u.second] + 1) {
q.push({nx, ny});
b[nx][ny] = b[u.first][u.second] + 1;
}
}
}
auto Moze = [&] (int mid) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = inf;
}
}
if (mid >= b[iii][jjj]) return false;
q.push({ii, jj});
a[ii][jj] = 0;
while (!q.empty()) {
auto u = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = u.first + dx[i];
int ny = u.second + dy[i];
if (!Can(nx, ny)) continue;
if (Ok(nx, ny, a[u.first][u.second] + 1, mid) && a[nx][ny] > a[u.first][u.second] + 1) {
q.push({nx, ny});
a[nx][ny] = a[u.first][u.second] + 1;
}
}
}
return a[iii][jjj] != inf;
};
int l = 0, r = b[ii][jj] - 1, ans = -1;
while (l <= r) {
int mid = l + r >> 1;
if (Moze(mid)) {
l = mid + 1;
ans = mid;
}
else r = mid - 1;
}
cout << ans << en;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:92:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
92 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
91 ms |
6772 KB |
Output is correct |
8 |
Correct |
1 ms |
420 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
488 KB |
Output is correct |
18 |
Correct |
1 ms |
484 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
612 KB |
Output is correct |
23 |
Correct |
1 ms |
608 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
744 KB |
Output is correct |
27 |
Correct |
1 ms |
740 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
5 ms |
2756 KB |
Output is correct |
34 |
Correct |
5 ms |
2772 KB |
Output is correct |
35 |
Correct |
21 ms |
2772 KB |
Output is correct |
36 |
Correct |
7 ms |
3156 KB |
Output is correct |
37 |
Correct |
7 ms |
3156 KB |
Output is correct |
38 |
Correct |
31 ms |
3180 KB |
Output is correct |
39 |
Correct |
9 ms |
3568 KB |
Output is correct |
40 |
Correct |
12 ms |
3532 KB |
Output is correct |
41 |
Correct |
55 ms |
3576 KB |
Output is correct |
42 |
Correct |
10 ms |
3924 KB |
Output is correct |
43 |
Correct |
16 ms |
3924 KB |
Output is correct |
44 |
Correct |
79 ms |
3952 KB |
Output is correct |
45 |
Correct |
13 ms |
4404 KB |
Output is correct |
46 |
Correct |
11 ms |
4308 KB |
Output is correct |
47 |
Correct |
58 ms |
4312 KB |
Output is correct |
48 |
Correct |
15 ms |
4800 KB |
Output is correct |
49 |
Correct |
16 ms |
4776 KB |
Output is correct |
50 |
Correct |
62 ms |
4852 KB |
Output is correct |
51 |
Correct |
14 ms |
5312 KB |
Output is correct |
52 |
Correct |
16 ms |
5300 KB |
Output is correct |
53 |
Correct |
96 ms |
5300 KB |
Output is correct |
54 |
Correct |
22 ms |
5712 KB |
Output is correct |
55 |
Correct |
18 ms |
5716 KB |
Output is correct |
56 |
Correct |
88 ms |
5732 KB |
Output is correct |
57 |
Correct |
18 ms |
6172 KB |
Output is correct |
58 |
Correct |
21 ms |
6200 KB |
Output is correct |
59 |
Correct |
117 ms |
6152 KB |
Output is correct |
60 |
Correct |
22 ms |
6636 KB |
Output is correct |
61 |
Correct |
22 ms |
6640 KB |
Output is correct |
62 |
Correct |
132 ms |
6600 KB |
Output is correct |
63 |
Correct |
103 ms |
6664 KB |
Output is correct |
64 |
Correct |
206 ms |
6732 KB |
Output is correct |
65 |
Correct |
177 ms |
6584 KB |
Output is correct |
66 |
Correct |
130 ms |
6596 KB |
Output is correct |
67 |
Correct |
117 ms |
6660 KB |
Output is correct |
68 |
Correct |
56 ms |
6604 KB |
Output is correct |
69 |
Correct |
51 ms |
6648 KB |
Output is correct |
70 |
Correct |
37 ms |
6604 KB |
Output is correct |
71 |
Correct |
30 ms |
6628 KB |
Output is correct |
72 |
Correct |
29 ms |
6608 KB |
Output is correct |
73 |
Correct |
37 ms |
6936 KB |
Output is correct |
74 |
Correct |
63 ms |
6936 KB |
Output is correct |
75 |
Correct |
71 ms |
6868 KB |
Output is correct |
76 |
Correct |
67 ms |
6944 KB |
Output is correct |
77 |
Correct |
68 ms |
6944 KB |
Output is correct |
78 |
Correct |
79 ms |
6820 KB |
Output is correct |
79 |
Correct |
70 ms |
6860 KB |
Output is correct |
80 |
Correct |
73 ms |
6912 KB |
Output is correct |
81 |
Correct |
79 ms |
6888 KB |
Output is correct |
82 |
Correct |
69 ms |
6912 KB |
Output is correct |
83 |
Correct |
113 ms |
6876 KB |
Output is correct |
84 |
Correct |
81 ms |
6796 KB |
Output is correct |
85 |
Correct |
116 ms |
6808 KB |
Output is correct |
86 |
Correct |
118 ms |
6872 KB |
Output is correct |
87 |
Correct |
71 ms |
6812 KB |
Output is correct |
88 |
Correct |
87 ms |
6820 KB |
Output is correct |
89 |
Correct |
88 ms |
6828 KB |
Output is correct |
90 |
Correct |
112 ms |
6820 KB |
Output is correct |
91 |
Correct |
103 ms |
6820 KB |
Output is correct |
92 |
Correct |
110 ms |
6752 KB |
Output is correct |