#include<bits/stdc++.h>
using namespace std;
const int maxn = 805, INF = 0x3f3f3f3f;
#define pii pair<int, int>
#define ff first
#define ss second
int dist[maxn][maxn];
bool valid1[maxn][maxn];
pii ini, fim;
vector<pii> hives;
int px[4] = {1, -1, 0, 0}, py[4] = {0, 0, 1, -1};
int n, s;
bool solve(int T){
bool valid[maxn][maxn];
for(int i = 0; i < maxn; i++){
for(int j = 0; j < maxn; j++) valid[i][j] = valid1[i][j];
}
memset(dist, 0, sizeof dist);
queue<pii> qabel, qurso;
for(pii x : hives)
qabel.push(x);
bool found = false, fuck = false;
while(!qabel.empty()){
int x, y;
tie(x, y) = qabel.front();
if(dist[x][y]==T) break;
qabel.pop();
for(int i = 0; i < 4; i++){
int vx = x + px[i];
int vy = y + py[i];
if(make_pair(vx, vy) == fim) {fuck = true; break;}
if(valid[vx][vy]) dist[vx][vy] = dist[x][y]+1, valid[vx][vy]=false, qabel.push({vx, vy});
}
if(fuck) break;
}
if(fuck) return false;
if(valid[ini.ff][ini.ss]) qurso.push(ini);
int cnt = 0;
while(true){
while(!qurso.empty()){// bfs urso
int x, y;
tie(x, y) = qurso.front();
if(make_pair(x, y)==fim) {found = true; break;}
int distu = dist[x][y];
if(distu%s==0 && distu!=cnt*s) break;
qurso.pop();
for(int i = 0; i < 4; i++){
int vx = x + px[i];
int vy = y + py[i];
if(valid[vx][vy]) dist[vx][vy] = dist[x][y]+1, valid[vx][vy] = false, qurso.push({vx, vy});
}
}
cnt++;
if(qurso.empty() || found) break;
while(!qabel.empty()){// bfs abelhas
int x, y;
tie(x, y) = qabel.front();
if(dist[x][y]==cnt+T) break;
qabel.pop();
for(int i = 0; i < 4; i++){
int vx = x + px[i];
int vy = y + py[i];
if(make_pair(vx, vy) == fim) {fuck = true; break;}
if(valid[vx][vy]) dist[vx][vy] = dist[x][y]+1, valid[vx][vy]=false, qabel.push({vx, vy});
}
if(fuck) break;
}
if(fuck) break;
}
if(fuck) {cout << "fuck\n"; return false;}
else return found;
}
int main(){
cin >> n >> s;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
char c; cin >> c;
if(c=='H') hives.push_back({i, j});
if(c=='M') ini = {i, j};
if(c=='D') fim = {i, j};
if(c=='M' || c=='G' || c=='D') valid1[i][j] = true;
}
}
int l=0, r = n*n, ans = INF;
while(l<=r){
int mid = (l+r)>>1;
if(solve(mid)) l = mid+1, ans = mid;
else r = mid-1;
}
cout << (ans==INF? -1 : ans) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
3 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
7 |
Incorrect |
195 ms |
4208 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
9 |
Correct |
2 ms |
3412 KB |
Output is correct |
10 |
Correct |
3 ms |
3412 KB |
Output is correct |
11 |
Correct |
3 ms |
3412 KB |
Output is correct |
12 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
13 |
Correct |
4 ms |
3412 KB |
Output is correct |
14 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
15 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
16 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
17 |
Incorrect |
4 ms |
3412 KB |
Output isn't correct |
18 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
19 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
20 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
21 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
22 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
23 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
24 |
Incorrect |
4 ms |
3412 KB |
Output isn't correct |
25 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
26 |
Incorrect |
5 ms |
3412 KB |
Output isn't correct |
27 |
Incorrect |
4 ms |
3412 KB |
Output isn't correct |
28 |
Incorrect |
4 ms |
3412 KB |
Output isn't correct |
29 |
Incorrect |
3 ms |
3412 KB |
Output isn't correct |
30 |
Incorrect |
4 ms |
3412 KB |
Output isn't correct |
31 |
Incorrect |
4 ms |
3412 KB |
Output isn't correct |
32 |
Incorrect |
6 ms |
3412 KB |
Output isn't correct |
33 |
Incorrect |
32 ms |
3728 KB |
Output isn't correct |
34 |
Incorrect |
27 ms |
3712 KB |
Output isn't correct |
35 |
Incorrect |
14 ms |
3636 KB |
Output isn't correct |
36 |
Incorrect |
40 ms |
3760 KB |
Output isn't correct |
37 |
Incorrect |
36 ms |
3668 KB |
Output isn't correct |
38 |
Incorrect |
16 ms |
3796 KB |
Output isn't correct |
39 |
Incorrect |
52 ms |
3692 KB |
Output isn't correct |
40 |
Incorrect |
49 ms |
3796 KB |
Output isn't correct |
41 |
Incorrect |
26 ms |
3820 KB |
Output isn't correct |
42 |
Incorrect |
70 ms |
3836 KB |
Output isn't correct |
43 |
Incorrect |
52 ms |
3864 KB |
Output isn't correct |
44 |
Incorrect |
26 ms |
3796 KB |
Output isn't correct |
45 |
Incorrect |
73 ms |
3896 KB |
Output isn't correct |
46 |
Incorrect |
73 ms |
3796 KB |
Output isn't correct |
47 |
Incorrect |
29 ms |
3884 KB |
Output isn't correct |
48 |
Incorrect |
90 ms |
3924 KB |
Output isn't correct |
49 |
Incorrect |
78 ms |
3920 KB |
Output isn't correct |
50 |
Incorrect |
32 ms |
3840 KB |
Output isn't correct |
51 |
Incorrect |
104 ms |
3964 KB |
Output isn't correct |
52 |
Incorrect |
101 ms |
4028 KB |
Output isn't correct |
53 |
Incorrect |
44 ms |
3968 KB |
Output isn't correct |
54 |
Incorrect |
116 ms |
3996 KB |
Output isn't correct |
55 |
Incorrect |
102 ms |
4044 KB |
Output isn't correct |
56 |
Incorrect |
50 ms |
4000 KB |
Output isn't correct |
57 |
Incorrect |
136 ms |
4040 KB |
Output isn't correct |
58 |
Incorrect |
118 ms |
4044 KB |
Output isn't correct |
59 |
Incorrect |
52 ms |
3968 KB |
Output isn't correct |
60 |
Incorrect |
158 ms |
4184 KB |
Output isn't correct |
61 |
Incorrect |
147 ms |
4084 KB |
Output isn't correct |
62 |
Incorrect |
55 ms |
4052 KB |
Output isn't correct |
63 |
Correct |
213 ms |
4088 KB |
Output is correct |
64 |
Correct |
250 ms |
4104 KB |
Output is correct |
65 |
Correct |
220 ms |
4016 KB |
Output is correct |
66 |
Incorrect |
249 ms |
4088 KB |
Output isn't correct |
67 |
Correct |
238 ms |
4088 KB |
Output is correct |
68 |
Correct |
258 ms |
4120 KB |
Output is correct |
69 |
Correct |
247 ms |
4116 KB |
Output is correct |
70 |
Correct |
249 ms |
4196 KB |
Output is correct |
71 |
Correct |
256 ms |
4112 KB |
Output is correct |
72 |
Correct |
316 ms |
4100 KB |
Output is correct |
73 |
Correct |
203 ms |
4428 KB |
Output is correct |
74 |
Correct |
275 ms |
4392 KB |
Output is correct |
75 |
Incorrect |
271 ms |
4424 KB |
Output isn't correct |
76 |
Incorrect |
272 ms |
4320 KB |
Output isn't correct |
77 |
Incorrect |
269 ms |
4292 KB |
Output isn't correct |
78 |
Incorrect |
226 ms |
4356 KB |
Output isn't correct |
79 |
Incorrect |
251 ms |
4352 KB |
Output isn't correct |
80 |
Incorrect |
264 ms |
4360 KB |
Output isn't correct |
81 |
Incorrect |
251 ms |
4416 KB |
Output isn't correct |
82 |
Incorrect |
251 ms |
4352 KB |
Output isn't correct |
83 |
Incorrect |
228 ms |
4292 KB |
Output isn't correct |
84 |
Incorrect |
229 ms |
4320 KB |
Output isn't correct |
85 |
Incorrect |
275 ms |
4212 KB |
Output isn't correct |
86 |
Incorrect |
228 ms |
4440 KB |
Output isn't correct |
87 |
Incorrect |
224 ms |
4288 KB |
Output isn't correct |
88 |
Incorrect |
211 ms |
4264 KB |
Output isn't correct |
89 |
Incorrect |
208 ms |
4252 KB |
Output isn't correct |
90 |
Incorrect |
202 ms |
4376 KB |
Output isn't correct |
91 |
Incorrect |
198 ms |
4256 KB |
Output isn't correct |
92 |
Incorrect |
214 ms |
4384 KB |
Output isn't correct |