#include<bits/stdc++.h>
using namespace std;
int n,s, bee[808][808], xx[] = {0, 0, -1, 1}, xy[] = {-1, 1, 0, 0}, bear[808][808], ans;
char mp[808][808];
pair<int, int> start, en;
queue<tuple<int, int, int>> q;
bool solve(int mid){
for(int i = 1; i<=n; ++i) for(int j = 1; j<=n; ++j) bee[i][j] = bear[i][j] = 1e9;
for(int i = 1; i<=n; ++i){
for(int j = 1; j<=n; ++j){
if(mp[i][j] == 'H'){
bee[i][j] = 0;
q.emplace(i, j, 0);
}
}
}
while(!q.empty()){
int x = get<0>(q.front()), y = get<1>(q.front()), w = get<2>(q.front());
q.pop();
for(int i = 0; i<4; ++i){
int nx = x + xx[i];
int ny = y + xy[i];
if(nx < 1 || n < 1 || nx > n || ny > n || mp[nx][ny] == 'T') continue;
if(bee[nx][ny] > w + 1){
bee[nx][ny] = w + 1;
q.emplace(nx, ny, w + 1);
}
}
}
bear[start.first][start.second] = mid*s;
q.emplace(start.first, start.second, mid*s);
while(!q.empty()){
int x = get<0>(q.front()), y = get<1>(q.front()), w = get<2>(q.front()); q.pop();
if(x == en.first && y == en.second) return true;
if (w%s == 0) if((w+s-1)/s >= bee[x][y]) continue;
else if((w+s-1)/s > bee[x][y]) continue;
bear[x][y] = w;
for(int i = 0; i<4; ++i){
int nx = x + xx[i];
int ny = y + xy[i];
if(nx < 1 || ny < 1 || nx > n || ny > n || mp[nx][ny] == 'T') continue;
if(bear[nx][ny] > w + 1) q.emplace(nx, ny, w + 1);
}
}
return false;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> s;
for(int i = 1; i<=n; ++i){
for(int j = 1; j<=n; ++j){
cin >> mp[i][j];
if(mp[i][j] == 'M') start = {i, j};
else if(mp[i][j] == 'D') en = {i, j};
}
}
int l = 1, r = 1e9;
while(l <= r){
int mid = (l + r) >> 1;
if(solve(mid)){
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << ans;
}
Compilation message
mecho.cpp: In function 'bool solve(int)':
mecho.cpp:38:12: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
38 | if (w%s == 0) if((w+s-1)/s >= bee[x][y]) continue;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4568 KB |
Output is correct |
5 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
7 |
Runtime error |
138 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
9 |
Incorrect |
7 ms |
4444 KB |
Output isn't correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Runtime error |
131 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
4564 KB |
Output isn't correct |
19 |
Incorrect |
3 ms |
4444 KB |
Output isn't correct |
20 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
22 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
23 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
24 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
26 |
Incorrect |
4 ms |
4444 KB |
Output isn't correct |
27 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
28 |
Incorrect |
4 ms |
4444 KB |
Output isn't correct |
29 |
Incorrect |
2 ms |
4444 KB |
Output isn't correct |
30 |
Incorrect |
3 ms |
4444 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
4440 KB |
Output isn't correct |
32 |
Incorrect |
4 ms |
4444 KB |
Output isn't correct |
33 |
Incorrect |
32 ms |
4700 KB |
Output isn't correct |
34 |
Incorrect |
38 ms |
4700 KB |
Output isn't correct |
35 |
Runtime error |
147 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Incorrect |
43 ms |
4952 KB |
Output isn't correct |
37 |
Incorrect |
48 ms |
4952 KB |
Output isn't correct |
38 |
Runtime error |
137 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Incorrect |
54 ms |
5212 KB |
Output isn't correct |
40 |
Incorrect |
65 ms |
5216 KB |
Output isn't correct |
41 |
Runtime error |
169 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Incorrect |
75 ms |
5392 KB |
Output isn't correct |
43 |
Incorrect |
82 ms |
5208 KB |
Output isn't correct |
44 |
Runtime error |
152 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Incorrect |
80 ms |
5468 KB |
Output isn't correct |
46 |
Incorrect |
87 ms |
5588 KB |
Output isn't correct |
47 |
Runtime error |
188 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Incorrect |
95 ms |
5776 KB |
Output isn't correct |
49 |
Incorrect |
110 ms |
5724 KB |
Output isn't correct |
50 |
Runtime error |
151 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Incorrect |
111 ms |
5980 KB |
Output isn't correct |
52 |
Incorrect |
121 ms |
5964 KB |
Output isn't correct |
53 |
Runtime error |
166 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Incorrect |
133 ms |
6280 KB |
Output isn't correct |
55 |
Incorrect |
144 ms |
6196 KB |
Output isn't correct |
56 |
Runtime error |
173 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Incorrect |
152 ms |
6328 KB |
Output isn't correct |
58 |
Incorrect |
163 ms |
6376 KB |
Output isn't correct |
59 |
Runtime error |
181 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Incorrect |
202 ms |
6780 KB |
Output isn't correct |
61 |
Incorrect |
193 ms |
6372 KB |
Output isn't correct |
62 |
Runtime error |
188 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
470 ms |
6736 KB |
Output is correct |
64 |
Incorrect |
631 ms |
6548 KB |
Output isn't correct |
65 |
Incorrect |
548 ms |
6616 KB |
Output isn't correct |
66 |
Correct |
513 ms |
6740 KB |
Output is correct |
67 |
Incorrect |
496 ms |
6740 KB |
Output isn't correct |
68 |
Incorrect |
647 ms |
6648 KB |
Output isn't correct |
69 |
Incorrect |
630 ms |
6596 KB |
Output isn't correct |
70 |
Incorrect |
702 ms |
6640 KB |
Output isn't correct |
71 |
Incorrect |
660 ms |
6652 KB |
Output isn't correct |
72 |
Incorrect |
644 ms |
6640 KB |
Output isn't correct |
73 |
Runtime error |
180 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
140 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
154 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
160 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
175 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
152 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
147 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
142 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
167 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
152 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
187 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
150 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
144 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
149 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
156 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
150 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
159 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
152 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
159 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
145 ms |
65536 KB |
Execution killed with signal 9 |