#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] != 'G' || 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 |
2 ms |
3152 KB |
Output is correct |
2 |
Correct |
1 ms |
3152 KB |
Output is correct |
3 |
Correct |
1 ms |
3152 KB |
Output is correct |
4 |
Correct |
2 ms |
3152 KB |
Output is correct |
5 |
Correct |
1 ms |
3152 KB |
Output is correct |
6 |
Correct |
1 ms |
3284 KB |
Output is correct |
7 |
Correct |
57 ms |
6736 KB |
Output is correct |
8 |
Correct |
2 ms |
3580 KB |
Output is correct |
9 |
Correct |
2 ms |
3408 KB |
Output is correct |
10 |
Correct |
2 ms |
3408 KB |
Output is correct |
11 |
Correct |
1 ms |
3408 KB |
Output is correct |
12 |
Incorrect |
2 ms |
5628 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
3544 KB |
Output isn't correct |
14 |
Correct |
2 ms |
5456 KB |
Output is correct |
15 |
Correct |
2 ms |
3408 KB |
Output is correct |
16 |
Correct |
1 ms |
3408 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 |
3408 KB |
Output is correct |
20 |
Correct |
2 ms |
3580 KB |
Output is correct |
21 |
Correct |
2 ms |
3408 KB |
Output is correct |
22 |
Correct |
1 ms |
3408 KB |
Output is correct |
23 |
Correct |
1 ms |
3408 KB |
Output is correct |
24 |
Correct |
2 ms |
3576 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 |
5968 KB |
Output is correct |
34 |
Correct |
6 ms |
5968 KB |
Output is correct |
35 |
Correct |
14 ms |
5888 KB |
Output is correct |
36 |
Correct |
7 ms |
5968 KB |
Output is correct |
37 |
Correct |
7 ms |
5968 KB |
Output is correct |
38 |
Correct |
18 ms |
5968 KB |
Output is correct |
39 |
Correct |
8 ms |
5968 KB |
Output is correct |
40 |
Correct |
8 ms |
5968 KB |
Output is correct |
41 |
Correct |
22 ms |
5968 KB |
Output is correct |
42 |
Correct |
9 ms |
6224 KB |
Output is correct |
43 |
Correct |
10 ms |
6044 KB |
Output is correct |
44 |
Correct |
28 ms |
6052 KB |
Output is correct |
45 |
Correct |
11 ms |
6224 KB |
Output is correct |
46 |
Correct |
11 ms |
6224 KB |
Output is correct |
47 |
Correct |
35 ms |
6224 KB |
Output is correct |
48 |
Correct |
15 ms |
6316 KB |
Output is correct |
49 |
Correct |
14 ms |
6224 KB |
Output is correct |
50 |
Correct |
43 ms |
6224 KB |
Output is correct |
51 |
Correct |
17 ms |
6652 KB |
Output is correct |
52 |
Correct |
16 ms |
6480 KB |
Output is correct |
53 |
Correct |
52 ms |
6480 KB |
Output is correct |
54 |
Correct |
19 ms |
6276 KB |
Output is correct |
55 |
Correct |
18 ms |
6480 KB |
Output is correct |
56 |
Correct |
58 ms |
6480 KB |
Output is correct |
57 |
Correct |
23 ms |
6480 KB |
Output is correct |
58 |
Correct |
20 ms |
6480 KB |
Output is correct |
59 |
Correct |
63 ms |
6480 KB |
Output is correct |
60 |
Correct |
22 ms |
6736 KB |
Output is correct |
61 |
Correct |
23 ms |
6748 KB |
Output is correct |
62 |
Correct |
85 ms |
6728 KB |
Output is correct |
63 |
Correct |
84 ms |
6828 KB |
Output is correct |
64 |
Correct |
121 ms |
6736 KB |
Output is correct |
65 |
Correct |
101 ms |
6216 KB |
Output is correct |
66 |
Correct |
90 ms |
6736 KB |
Output is correct |
67 |
Correct |
76 ms |
6736 KB |
Output is correct |
68 |
Correct |
46 ms |
6216 KB |
Output is correct |
69 |
Correct |
41 ms |
6688 KB |
Output is correct |
70 |
Correct |
40 ms |
6796 KB |
Output is correct |
71 |
Correct |
38 ms |
6736 KB |
Output is correct |
72 |
Incorrect |
36 ms |
6728 KB |
Output isn't correct |
73 |
Incorrect |
52 ms |
7004 KB |
Output isn't correct |
74 |
Correct |
45 ms |
6984 KB |
Output is correct |
75 |
Correct |
59 ms |
6984 KB |
Output is correct |
76 |
Correct |
46 ms |
6992 KB |
Output is correct |
77 |
Correct |
50 ms |
7008 KB |
Output is correct |
78 |
Correct |
52 ms |
6912 KB |
Output is correct |
79 |
Correct |
43 ms |
6992 KB |
Output is correct |
80 |
Correct |
47 ms |
6992 KB |
Output is correct |
81 |
Correct |
51 ms |
6648 KB |
Output is correct |
82 |
Correct |
42 ms |
7084 KB |
Output is correct |
83 |
Correct |
57 ms |
6992 KB |
Output is correct |
84 |
Correct |
50 ms |
6492 KB |
Output is correct |
85 |
Correct |
65 ms |
6984 KB |
Output is correct |
86 |
Correct |
56 ms |
6984 KB |
Output is correct |
87 |
Correct |
56 ms |
6992 KB |
Output is correct |
88 |
Correct |
56 ms |
6992 KB |
Output is correct |
89 |
Correct |
61 ms |
6984 KB |
Output is correct |
90 |
Correct |
63 ms |
6992 KB |
Output is correct |
91 |
Correct |
61 ms |
6992 KB |
Output is correct |
92 |
Correct |
58 ms |
6992 KB |
Output is correct |