#include <bits/stdc++.h>
using namespace std;
int N, S;
char arr[810][810];
int sx, sy, ex, ey;
typedef pair<int, int> pii;
#define f first
#define s second
queue<pair<pii, int> > q1;
queue<pair<pii, int> > q2;
int vis1[810][810];
int vis2[810][810];
const int INF = 0x3f3f3f3f;
int xd[4] = {1, -1, 0, 0};
int yd[4] = {0, 0, 1, -1};
void bfs(int t){
while (!q2.empty()){
int x = q2.front().f.f;
int y = q2.front().f.s;
int w = q2.front().s;
if (w == t)
break;
q2.pop();
for (int k = 0; k < 4; k++){
int nx = x+xd[k];
int ny = y+yd[k];
if (nx >= 1 && nx <= N && ny >= 1 && ny <= N &&
arr[nx][ny] != 'T' && arr[nx][ny] != 'D'){
if (vis2[nx][ny] > vis2[x][y]+1){
vis2[nx][ny] = vis2[x][y]+1;
q2.push({{nx, ny}, vis2[nx][ny]});
}
}
}
}
}
bool valid(int t){
while (!q1.empty()) q1.pop();
while (!q2.empty()) q2.pop();
memset(vis1, INF, sizeof(vis1));
memset(vis2, INF, sizeof(vis2));
for (int i = 1; i <= N; i++){
for (int j = 1; j <= N; j++){
if (arr[i][j] == 'H'){
q2.push({{i, j}, 0});
vis2[i][j] = 0;
}
}
}
q1.push({{sx, sy}, 0});
vis1[sx][sy] = 0;
int curtime = 1;
while (curtime <= t){
bfs(curtime);
curtime++;
}
while (!q1.empty()){
int x = q1.front().f.f;
int y = q1.front().f.s;
int w = q1.front().s;
if (x == ex && y == ey){
return true;
}
if (vis1[x][y] == (curtime-t)*S){
bfs(curtime);
curtime++;
}
q1.pop();
if (vis2[x][y] != INF){
continue;
}
for (int k = 0; k < 4; k++){
int nx = x+xd[k];
int ny = y+yd[k];
if (nx >= 1 && nx <= N && ny >= 1 && ny <= N && vis2[nx][ny] == INF &&
(arr[nx][ny] == 'D' || arr[nx][ny] == 'G')){
if (vis1[nx][ny] > vis1[x][y]+1){
vis1[nx][ny] = vis1[x][y]+1;
q1.push({{nx, ny}, vis1[nx][ny]});
}
}
}
}
return false;
}
int main(){
cin >> N >> S;
for (int i = 1; i <= N; i++){
for (int j = 1; j <= N; j++){
cin >> arr[i][j];
if (arr[i][j] == 'M'){
sx = i;
sy = j;
}
if (arr[i][j] == 'D'){
ex = i;
ey = j;
}
}
}
if (!valid(0)){
cout << -1 << "\n";
return 0;
}
int l = 0, r = 640000;
while (l != r){
int mid = (l+r+1)/2;
if (valid(mid)){
l = mid;
} else {
r = mid-1;
}
}
cout << l << "\n";
}
Compilation message
mecho.cpp: In function 'bool valid(int)':
mecho.cpp:70:7: warning: unused variable 'w' [-Wunused-variable]
70 | int w = q1.front().s;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5324 KB |
Output is correct |
2 |
Correct |
11 ms |
5436 KB |
Output is correct |
3 |
Correct |
11 ms |
5324 KB |
Output is correct |
4 |
Correct |
10 ms |
5440 KB |
Output is correct |
5 |
Correct |
10 ms |
5324 KB |
Output is correct |
6 |
Correct |
10 ms |
5440 KB |
Output is correct |
7 |
Correct |
455 ms |
6348 KB |
Output is correct |
8 |
Correct |
3 ms |
5324 KB |
Output is correct |
9 |
Correct |
11 ms |
5444 KB |
Output is correct |
10 |
Correct |
12 ms |
5440 KB |
Output is correct |
11 |
Correct |
11 ms |
5324 KB |
Output is correct |
12 |
Correct |
12 ms |
5452 KB |
Output is correct |
13 |
Correct |
13 ms |
5480 KB |
Output is correct |
14 |
Correct |
3 ms |
5452 KB |
Output is correct |
15 |
Correct |
10 ms |
5452 KB |
Output is correct |
16 |
Correct |
13 ms |
5452 KB |
Output is correct |
17 |
Correct |
11 ms |
5448 KB |
Output is correct |
18 |
Correct |
11 ms |
5460 KB |
Output is correct |
19 |
Correct |
11 ms |
5452 KB |
Output is correct |
20 |
Correct |
11 ms |
5452 KB |
Output is correct |
21 |
Correct |
11 ms |
5464 KB |
Output is correct |
22 |
Correct |
11 ms |
5468 KB |
Output is correct |
23 |
Correct |
12 ms |
5468 KB |
Output is correct |
24 |
Correct |
11 ms |
5452 KB |
Output is correct |
25 |
Correct |
12 ms |
5452 KB |
Output is correct |
26 |
Correct |
12 ms |
5452 KB |
Output is correct |
27 |
Correct |
12 ms |
5488 KB |
Output is correct |
28 |
Correct |
12 ms |
5480 KB |
Output is correct |
29 |
Correct |
12 ms |
5492 KB |
Output is correct |
30 |
Correct |
13 ms |
5492 KB |
Output is correct |
31 |
Correct |
12 ms |
5452 KB |
Output is correct |
32 |
Correct |
12 ms |
5452 KB |
Output is correct |
33 |
Correct |
56 ms |
5708 KB |
Output is correct |
34 |
Correct |
53 ms |
5708 KB |
Output is correct |
35 |
Correct |
71 ms |
5708 KB |
Output is correct |
36 |
Correct |
65 ms |
5672 KB |
Output is correct |
37 |
Correct |
63 ms |
5708 KB |
Output is correct |
38 |
Correct |
89 ms |
5640 KB |
Output is correct |
39 |
Correct |
82 ms |
5768 KB |
Output is correct |
40 |
Correct |
78 ms |
5768 KB |
Output is correct |
41 |
Correct |
111 ms |
5668 KB |
Output is correct |
42 |
Correct |
107 ms |
5816 KB |
Output is correct |
43 |
Correct |
99 ms |
5816 KB |
Output is correct |
44 |
Correct |
131 ms |
5836 KB |
Output is correct |
45 |
Correct |
122 ms |
5848 KB |
Output is correct |
46 |
Correct |
109 ms |
5828 KB |
Output is correct |
47 |
Correct |
159 ms |
5876 KB |
Output is correct |
48 |
Correct |
149 ms |
5836 KB |
Output is correct |
49 |
Correct |
138 ms |
5956 KB |
Output is correct |
50 |
Correct |
185 ms |
5844 KB |
Output is correct |
51 |
Correct |
155 ms |
5836 KB |
Output is correct |
52 |
Correct |
150 ms |
5828 KB |
Output is correct |
53 |
Correct |
222 ms |
5828 KB |
Output is correct |
54 |
Correct |
181 ms |
6084 KB |
Output is correct |
55 |
Correct |
172 ms |
5912 KB |
Output is correct |
56 |
Correct |
257 ms |
6088 KB |
Output is correct |
57 |
Correct |
213 ms |
6028 KB |
Output is correct |
58 |
Correct |
195 ms |
5952 KB |
Output is correct |
59 |
Correct |
294 ms |
6024 KB |
Output is correct |
60 |
Correct |
226 ms |
6084 KB |
Output is correct |
61 |
Correct |
215 ms |
6084 KB |
Output is correct |
62 |
Correct |
337 ms |
5956 KB |
Output is correct |
63 |
Correct |
392 ms |
6084 KB |
Output is correct |
64 |
Correct |
429 ms |
6084 KB |
Output is correct |
65 |
Correct |
382 ms |
6172 KB |
Output is correct |
66 |
Correct |
423 ms |
6012 KB |
Output is correct |
67 |
Correct |
64 ms |
5952 KB |
Output is correct |
68 |
Correct |
437 ms |
6100 KB |
Output is correct |
69 |
Correct |
376 ms |
6144 KB |
Output is correct |
70 |
Correct |
448 ms |
6116 KB |
Output is correct |
71 |
Correct |
434 ms |
6088 KB |
Output is correct |
72 |
Correct |
450 ms |
6212 KB |
Output is correct |
73 |
Correct |
375 ms |
6340 KB |
Output is correct |
74 |
Correct |
440 ms |
6420 KB |
Output is correct |
75 |
Correct |
498 ms |
6472 KB |
Output is correct |
76 |
Correct |
491 ms |
6468 KB |
Output is correct |
77 |
Correct |
473 ms |
6560 KB |
Output is correct |
78 |
Correct |
65 ms |
6340 KB |
Output is correct |
79 |
Correct |
453 ms |
6492 KB |
Output is correct |
80 |
Correct |
454 ms |
6324 KB |
Output is correct |
81 |
Correct |
467 ms |
6548 KB |
Output is correct |
82 |
Correct |
448 ms |
6468 KB |
Output is correct |
83 |
Correct |
464 ms |
6464 KB |
Output is correct |
84 |
Correct |
472 ms |
6360 KB |
Output is correct |
85 |
Correct |
484 ms |
6388 KB |
Output is correct |
86 |
Correct |
485 ms |
6468 KB |
Output is correct |
87 |
Correct |
463 ms |
6360 KB |
Output is correct |
88 |
Correct |
456 ms |
6472 KB |
Output is correct |
89 |
Correct |
492 ms |
6384 KB |
Output is correct |
90 |
Correct |
482 ms |
6304 KB |
Output is correct |
91 |
Correct |
470 ms |
6344 KB |
Output is correct |
92 |
Correct |
465 ms |
6296 KB |
Output is correct |