#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) bear[i][j] = 1e9;
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' || mp[nx][ny] == 'H') 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};
}
}
for(int i = 1; i<=n; ++i) for(int j = 1; j<=n; ++j) bee[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);
}
}
}
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:17:12: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
17 | if (w%s == 0) if((w+s-1)/s >= bee[x][y]) continue;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
7 |
Runtime error |
139 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
9 |
Incorrect |
7 ms |
7512 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Runtime error |
136 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 |
4444 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
4620 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
33 |
Incorrect |
4 ms |
4696 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
4700 KB |
Output isn't correct |
35 |
Runtime error |
137 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Incorrect |
5 ms |
4700 KB |
Output isn't correct |
37 |
Incorrect |
5 ms |
4952 KB |
Output isn't correct |
38 |
Runtime error |
139 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Incorrect |
7 ms |
5108 KB |
Output isn't correct |
40 |
Incorrect |
6 ms |
4952 KB |
Output isn't correct |
41 |
Runtime error |
166 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Incorrect |
8 ms |
5212 KB |
Output isn't correct |
43 |
Incorrect |
8 ms |
5208 KB |
Output isn't correct |
44 |
Runtime error |
179 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Incorrect |
9 ms |
5212 KB |
Output isn't correct |
46 |
Incorrect |
9 ms |
5212 KB |
Output isn't correct |
47 |
Runtime error |
174 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Incorrect |
10 ms |
5468 KB |
Output isn't correct |
49 |
Incorrect |
11 ms |
5468 KB |
Output isn't correct |
50 |
Runtime error |
160 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Incorrect |
15 ms |
5724 KB |
Output isn't correct |
52 |
Incorrect |
12 ms |
5720 KB |
Output isn't correct |
53 |
Runtime error |
170 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Incorrect |
15 ms |
5876 KB |
Output isn't correct |
55 |
Incorrect |
14 ms |
5724 KB |
Output isn't correct |
56 |
Runtime error |
186 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Incorrect |
16 ms |
5980 KB |
Output isn't correct |
58 |
Incorrect |
16 ms |
5980 KB |
Output isn't correct |
59 |
Runtime error |
198 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Incorrect |
18 ms |
5980 KB |
Output isn't correct |
61 |
Incorrect |
18 ms |
5976 KB |
Output isn't correct |
62 |
Runtime error |
194 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
90 ms |
6172 KB |
Output is correct |
64 |
Incorrect |
250 ms |
5976 KB |
Output isn't correct |
65 |
Incorrect |
160 ms |
6024 KB |
Output isn't correct |
66 |
Correct |
117 ms |
6196 KB |
Output is correct |
67 |
Incorrect |
98 ms |
6172 KB |
Output isn't correct |
68 |
Incorrect |
218 ms |
6212 KB |
Output isn't correct |
69 |
Incorrect |
202 ms |
6236 KB |
Output isn't correct |
70 |
Incorrect |
223 ms |
6236 KB |
Output isn't correct |
71 |
Incorrect |
215 ms |
6212 KB |
Output isn't correct |
72 |
Incorrect |
41 ms |
5980 KB |
Output isn't correct |
73 |
Runtime error |
174 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
143 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
146 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
159 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
170 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
146 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
137 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
139 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
168 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
149 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
160 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
157 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
146 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
161 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
152 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
141 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
136 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
141 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
157 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
163 ms |
65536 KB |
Execution killed with signal 9 |