#include <bits/stdc++.h>
using namespace std;
const int maxn = 805;
int n, s, b[maxn][maxn], m[maxn][maxn];
int dr[] = {0, 0, -1, 1};
int dc[] = {-1, 1, 0, 0};
char grid[maxn][maxn];
pair<int, int> st;
vector<pair<int, int>> hives;
bool check(int r, int c){
return 0 <= r && r < n && 0 <= c && c < n;
}
bool ok(int t){
memset(b, -1, sizeof b);
memset(m, -1, sizeof m);
queue<pair<int, int>> q;
for(auto x: hives){
q.push(x);
b[x.first][x.second] = 0;
}
while(!q.empty()){
auto[r, c] = q.front();
if(b[r][c]==t) break;
q.pop();
for(int i = 0; i < 4; i++){
int r2 = r + dr[i];
int c2 = c + dc[i];
if(check(r2, c2) && (grid[r2][c2] == 'M' || grid[r2][c2]=='G')&&b[r2][c2]==-1){
b[r2][c2] = b[r][c] + 1;
q.emplace(r2, c2);
}
}
}
queue<pair<int, int>> pos_loc;
pos_loc.push(st);
m[st.first][st.second] = 0;
int t2 = t;
while(!pos_loc.empty()){
t2++;
while(!pos_loc.empty()){
auto[r, c] = pos_loc.front();
if(m[r][c]==(t2-t)*s)
break;
pos_loc.pop();
if(~b[r][c]) continue;
for(int i = 0; i < 4; i++){
int r2 = r + dr[i];
int c2 = c + dc[i];
if(check(r2, c2) && b[r2][c2] == -1 && m[r2][c2] == -1 && (grid[r2][c2] == 'G' || grid[r2][c2] == 'D')){
if(grid[r2][c2]=='D') return true;
m[r2][c2] = m[r][c] + 1;
pos_loc.emplace(r2, c2);
}
}
}
while(!q.empty()){
auto[r, c] = q.front();
if(b[r][c] == t2)break;
q.pop();
for(int i = 0; i < 4; i++){
int r2 = r + dr[i];
int c2 = c + dc[i];
if(check(r2, c2) && b[r2][c2] == -1 && (grid[r2][c2] == 'G' || grid[r2][c2] == 'M')){
b[r2][c2] = b[r][c] + 1;
q.emplace(r2, c2);
}
}
}
}
return false;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> s;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++){
cin >> grid[i][j];
if(grid[i][j]=='M') st = {i, j};
else if(grid[i][j]=='H') hives.push_back(make_pair(i, j));
}
int ans = -1;
for(int u = int(1e6); u > 0; u /= 2) while(ans + u <= int(1e6) && ok(ans + u)) ans += u;
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5976 KB |
Output is correct |
2 |
Correct |
4 ms |
5980 KB |
Output is correct |
3 |
Correct |
4 ms |
5844 KB |
Output is correct |
4 |
Correct |
3 ms |
5992 KB |
Output is correct |
5 |
Correct |
5 ms |
5980 KB |
Output is correct |
6 |
Correct |
3 ms |
5976 KB |
Output is correct |
7 |
Correct |
255 ms |
6892 KB |
Output is correct |
8 |
Correct |
3 ms |
5980 KB |
Output is correct |
9 |
Correct |
4 ms |
5980 KB |
Output is correct |
10 |
Correct |
4 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
5980 KB |
Output is correct |
12 |
Correct |
5 ms |
5980 KB |
Output is correct |
13 |
Correct |
4 ms |
6044 KB |
Output is correct |
14 |
Correct |
4 ms |
5980 KB |
Output is correct |
15 |
Correct |
4 ms |
5980 KB |
Output is correct |
16 |
Correct |
4 ms |
6008 KB |
Output is correct |
17 |
Correct |
4 ms |
5980 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
4 ms |
5980 KB |
Output is correct |
20 |
Correct |
4 ms |
5980 KB |
Output is correct |
21 |
Correct |
4 ms |
5980 KB |
Output is correct |
22 |
Correct |
4 ms |
6024 KB |
Output is correct |
23 |
Correct |
5 ms |
5980 KB |
Output is correct |
24 |
Correct |
4 ms |
6032 KB |
Output is correct |
25 |
Correct |
4 ms |
5836 KB |
Output is correct |
26 |
Correct |
4 ms |
5976 KB |
Output is correct |
27 |
Correct |
5 ms |
5980 KB |
Output is correct |
28 |
Correct |
5 ms |
6048 KB |
Output is correct |
29 |
Correct |
5 ms |
6056 KB |
Output is correct |
30 |
Correct |
5 ms |
5980 KB |
Output is correct |
31 |
Correct |
5 ms |
5980 KB |
Output is correct |
32 |
Correct |
5 ms |
5980 KB |
Output is correct |
33 |
Correct |
29 ms |
6284 KB |
Output is correct |
34 |
Correct |
27 ms |
6236 KB |
Output is correct |
35 |
Correct |
35 ms |
6236 KB |
Output is correct |
36 |
Correct |
33 ms |
6236 KB |
Output is correct |
37 |
Correct |
30 ms |
6232 KB |
Output is correct |
38 |
Correct |
41 ms |
6240 KB |
Output is correct |
39 |
Correct |
43 ms |
6372 KB |
Output is correct |
40 |
Correct |
41 ms |
6236 KB |
Output is correct |
41 |
Correct |
62 ms |
6480 KB |
Output is correct |
42 |
Correct |
53 ms |
6236 KB |
Output is correct |
43 |
Correct |
55 ms |
6232 KB |
Output is correct |
44 |
Correct |
63 ms |
6232 KB |
Output is correct |
45 |
Correct |
55 ms |
6236 KB |
Output is correct |
46 |
Correct |
59 ms |
6448 KB |
Output is correct |
47 |
Correct |
83 ms |
6444 KB |
Output is correct |
48 |
Correct |
72 ms |
6492 KB |
Output is correct |
49 |
Correct |
69 ms |
6492 KB |
Output is correct |
50 |
Correct |
88 ms |
6500 KB |
Output is correct |
51 |
Correct |
84 ms |
6556 KB |
Output is correct |
52 |
Correct |
84 ms |
6552 KB |
Output is correct |
53 |
Correct |
109 ms |
6492 KB |
Output is correct |
54 |
Correct |
103 ms |
6492 KB |
Output is correct |
55 |
Correct |
101 ms |
6488 KB |
Output is correct |
56 |
Correct |
129 ms |
6488 KB |
Output is correct |
57 |
Correct |
109 ms |
6508 KB |
Output is correct |
58 |
Correct |
109 ms |
6488 KB |
Output is correct |
59 |
Correct |
137 ms |
6492 KB |
Output is correct |
60 |
Correct |
122 ms |
7000 KB |
Output is correct |
61 |
Correct |
110 ms |
6748 KB |
Output is correct |
62 |
Correct |
153 ms |
6780 KB |
Output is correct |
63 |
Correct |
240 ms |
7044 KB |
Output is correct |
64 |
Correct |
265 ms |
6744 KB |
Output is correct |
65 |
Correct |
266 ms |
6748 KB |
Output is correct |
66 |
Correct |
347 ms |
6748 KB |
Output is correct |
67 |
Correct |
236 ms |
6748 KB |
Output is correct |
68 |
Correct |
266 ms |
6744 KB |
Output is correct |
69 |
Correct |
250 ms |
6748 KB |
Output is correct |
70 |
Correct |
249 ms |
6748 KB |
Output is correct |
71 |
Correct |
252 ms |
6808 KB |
Output is correct |
72 |
Correct |
376 ms |
6948 KB |
Output is correct |
73 |
Correct |
216 ms |
7000 KB |
Output is correct |
74 |
Correct |
296 ms |
7340 KB |
Output is correct |
75 |
Correct |
278 ms |
7000 KB |
Output is correct |
76 |
Correct |
272 ms |
7004 KB |
Output is correct |
77 |
Correct |
289 ms |
7080 KB |
Output is correct |
78 |
Correct |
225 ms |
7000 KB |
Output is correct |
79 |
Correct |
272 ms |
7004 KB |
Output is correct |
80 |
Correct |
278 ms |
7004 KB |
Output is correct |
81 |
Correct |
246 ms |
7004 KB |
Output is correct |
82 |
Correct |
240 ms |
7004 KB |
Output is correct |
83 |
Correct |
237 ms |
7004 KB |
Output is correct |
84 |
Correct |
259 ms |
7000 KB |
Output is correct |
85 |
Correct |
270 ms |
7000 KB |
Output is correct |
86 |
Correct |
277 ms |
7000 KB |
Output is correct |
87 |
Correct |
263 ms |
7008 KB |
Output is correct |
88 |
Correct |
232 ms |
6748 KB |
Output is correct |
89 |
Correct |
250 ms |
6744 KB |
Output is correct |
90 |
Correct |
260 ms |
6748 KB |
Output is correct |
91 |
Correct |
261 ms |
6748 KB |
Output is correct |
92 |
Correct |
261 ms |
6948 KB |
Output is correct |