#include <bits/stdc++.h>
using namespace std;
int n, s;
char inp[808][808];
int bee[808][808];
int dis[808][808];
const int dr[] = {1, -1, 0, 0}, dc[] = {0, 0, 1, -1};
pair<int, int> st;
bool solve(int mid){
queue<tuple<int, int, int>> q;
q.emplace(st.first, st.second, 0);
for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) dis[i][j] = (inp[i][j] == 'M' ? 0 : 1e9);
while(!q.empty()){
auto [x, y, d1] = q.front(); q.pop();
if(dis[x][y] < d1 || (dis[x][y] / s >= bee[x][y] - mid && bee[x][y] != -1)) continue;
if(inp[x][y] == 'D') return true;
for(int i = 0; i < 4; ++i){
int nx = x+dr[i];
int ny = y+dc[i];
if(nx <= 0 || nx > n || ny <= 0 || ny > n) continue;
if(inp[nx][ny] != 'T' && dis[nx][ny] > d1 + 1){
q.emplace(nx, ny, dis[nx][ny] = d1+1);
}
}
}
return false;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
queue<pair<int, int>> q;
cin >> n >> s;
memset(bee, -1, sizeof bee);
for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j){
cin >> inp[i][j];
if(inp[i][j] == 'M') st = {i, j};
else if(inp[i][j] == 'H'){
q.emplace(i, j);
bee[i][j] = 0;
}
}
while(!q.empty()){
auto [x, y] = q.front(); q.pop();
for(int i = 0; i < 4; ++i){
int nx = x+dr[i];
int ny = y+dc[i];
if(nx <= 0 || nx > n || ny <= 0 || ny > n) continue;
if(inp[nx][ny] == 'D' || inp[nx][ny] == 'T' || bee[nx][ny] != -1) continue;
bee[nx][ny] = bee[x][y] + 1;
q.emplace(nx, ny);
}
}
int l = 0, r = 1e6, ans = -1;
while(l <= r){
int mid = (l+r)>>1;
if(solve(mid)) ans = mid, l = mid+1;
else r = mid-1;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3152 KB |
Output is correct |
2 |
Correct |
2 ms |
3152 KB |
Output is correct |
3 |
Correct |
2 ms |
3324 KB |
Output is correct |
4 |
Correct |
1 ms |
3152 KB |
Output is correct |
5 |
Correct |
2 ms |
3320 KB |
Output is correct |
6 |
Correct |
2 ms |
3152 KB |
Output is correct |
7 |
Correct |
65 ms |
6224 KB |
Output is correct |
8 |
Correct |
2 ms |
3408 KB |
Output is correct |
9 |
Correct |
2 ms |
3408 KB |
Output is correct |
10 |
Correct |
2 ms |
3408 KB |
Output is correct |
11 |
Correct |
2 ms |
3408 KB |
Output is correct |
12 |
Correct |
2 ms |
5456 KB |
Output is correct |
13 |
Correct |
2 ms |
3408 KB |
Output is correct |
14 |
Correct |
2 ms |
5456 KB |
Output is correct |
15 |
Correct |
2 ms |
3408 KB |
Output is correct |
16 |
Correct |
2 ms |
3576 KB |
Output is correct |
17 |
Correct |
2 ms |
3408 KB |
Output is correct |
18 |
Correct |
1 ms |
3408 KB |
Output is correct |
19 |
Correct |
2 ms |
3576 KB |
Output is correct |
20 |
Correct |
1 ms |
3348 KB |
Output is correct |
21 |
Correct |
2 ms |
3576 KB |
Output is correct |
22 |
Correct |
1 ms |
3408 KB |
Output is correct |
23 |
Correct |
2 ms |
3408 KB |
Output is correct |
24 |
Correct |
2 ms |
3408 KB |
Output is correct |
25 |
Correct |
2 ms |
5456 KB |
Output is correct |
26 |
Correct |
2 ms |
5456 KB |
Output is correct |
27 |
Correct |
2 ms |
5456 KB |
Output is correct |
28 |
Correct |
2 ms |
5456 KB |
Output is correct |
29 |
Correct |
2 ms |
5456 KB |
Output is correct |
30 |
Correct |
2 ms |
5456 KB |
Output is correct |
31 |
Correct |
2 ms |
5456 KB |
Output is correct |
32 |
Correct |
2 ms |
5456 KB |
Output is correct |
33 |
Correct |
6 ms |
5712 KB |
Output is correct |
34 |
Correct |
6 ms |
5712 KB |
Output is correct |
35 |
Correct |
13 ms |
5712 KB |
Output is correct |
36 |
Correct |
6 ms |
5712 KB |
Output is correct |
37 |
Correct |
7 ms |
5712 KB |
Output is correct |
38 |
Correct |
18 ms |
5712 KB |
Output is correct |
39 |
Correct |
8 ms |
5712 KB |
Output is correct |
40 |
Correct |
8 ms |
5712 KB |
Output is correct |
41 |
Correct |
24 ms |
5712 KB |
Output is correct |
42 |
Correct |
9 ms |
5968 KB |
Output is correct |
43 |
Correct |
8 ms |
5968 KB |
Output is correct |
44 |
Correct |
28 ms |
6136 KB |
Output is correct |
45 |
Correct |
10 ms |
5968 KB |
Output is correct |
46 |
Correct |
10 ms |
5988 KB |
Output is correct |
47 |
Correct |
35 ms |
5968 KB |
Output is correct |
48 |
Correct |
11 ms |
5968 KB |
Output is correct |
49 |
Correct |
12 ms |
5968 KB |
Output is correct |
50 |
Correct |
38 ms |
6068 KB |
Output is correct |
51 |
Correct |
13 ms |
5968 KB |
Output is correct |
52 |
Correct |
14 ms |
5968 KB |
Output is correct |
53 |
Correct |
49 ms |
5968 KB |
Output is correct |
54 |
Correct |
14 ms |
5968 KB |
Output is correct |
55 |
Correct |
14 ms |
5968 KB |
Output is correct |
56 |
Correct |
55 ms |
5968 KB |
Output is correct |
57 |
Correct |
18 ms |
5968 KB |
Output is correct |
58 |
Correct |
18 ms |
5980 KB |
Output is correct |
59 |
Correct |
63 ms |
5980 KB |
Output is correct |
60 |
Correct |
17 ms |
5968 KB |
Output is correct |
61 |
Correct |
20 ms |
5968 KB |
Output is correct |
62 |
Correct |
77 ms |
5968 KB |
Output is correct |
63 |
Correct |
74 ms |
5968 KB |
Output is correct |
64 |
Correct |
129 ms |
5968 KB |
Output is correct |
65 |
Correct |
101 ms |
5968 KB |
Output is correct |
66 |
Correct |
88 ms |
5968 KB |
Output is correct |
67 |
Correct |
72 ms |
5968 KB |
Output is correct |
68 |
Correct |
39 ms |
6224 KB |
Output is correct |
69 |
Correct |
41 ms |
6228 KB |
Output is correct |
70 |
Correct |
35 ms |
6224 KB |
Output is correct |
71 |
Correct |
32 ms |
6224 KB |
Output is correct |
72 |
Correct |
32 ms |
6068 KB |
Output is correct |
73 |
Correct |
34 ms |
6276 KB |
Output is correct |
74 |
Correct |
45 ms |
6480 KB |
Output is correct |
75 |
Correct |
48 ms |
6480 KB |
Output is correct |
76 |
Correct |
44 ms |
6480 KB |
Output is correct |
77 |
Correct |
49 ms |
6304 KB |
Output is correct |
78 |
Correct |
47 ms |
6288 KB |
Output is correct |
79 |
Correct |
38 ms |
6224 KB |
Output is correct |
80 |
Correct |
42 ms |
6236 KB |
Output is correct |
81 |
Correct |
45 ms |
6224 KB |
Output is correct |
82 |
Correct |
37 ms |
6224 KB |
Output is correct |
83 |
Correct |
53 ms |
6224 KB |
Output is correct |
84 |
Correct |
47 ms |
6236 KB |
Output is correct |
85 |
Correct |
47 ms |
6416 KB |
Output is correct |
86 |
Correct |
56 ms |
6216 KB |
Output is correct |
87 |
Correct |
49 ms |
6224 KB |
Output is correct |
88 |
Correct |
52 ms |
6396 KB |
Output is correct |
89 |
Correct |
52 ms |
6224 KB |
Output is correct |
90 |
Correct |
54 ms |
6224 KB |
Output is correct |
91 |
Correct |
57 ms |
6224 KB |
Output is correct |
92 |
Correct |
56 ms |
6392 KB |
Output is correct |