#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
char tab[802][802];
int xcor[4]={0,1,0,-1};
int ycor[4]={1,0,-1,0};
int dist[802][802];
int oo=1e9;
int n,s;
void prep(){
queue<pii> q;
for (int i = 1; i<=n; i++){
for (int j = 1; j<=n; j++){
dist[i][j]=oo;
if (tab[i][j]=='H'){
q.push({i,j});
dist[i][j]=0;
}
}
}
while(q.size()){
auto [x,y]=q.front();
q.pop();
for (int i = 0; i<4; i++){
int u=x+xcor[i];
int v=y+ycor[i];
if (tab[u][v]=='G' && dist[u][v]>dist[x][y]+1){
dist[u][v]=dist[x][y]+1;
q.push({u,v});
}
}
}
}
int vis[802][802];
int iter=0;
pii st,nd;
bool bfs(int waiting){
queue<pair<pii,int>> q;
if (dist[st.first][st.second]<=waiting)return false;
q.push({st,0});
while(q.size()){
auto [pol,d]=q.front();
if (pol==nd)return true;
q.pop();
for (int i = 0; i<4; i++){
int u=pol.first+xcor[i];
int v=pol.second+ycor[i];
if ((tab[u][v]=='G'||tab[u][v]=='D') && vis[u][v]!=iter && (dist[u][v]>waiting+(d+1)/s)){
vis[u][v]=iter;
q.push({{u,v},d+1});
}
}
}
return false;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for (int i = 1; i<=n; i++){
for (int j = 1; j<=n; j++){
cin >> tab[i][j];
if (tab[i][j]=='M')st={i,j};
if (tab[i][j]=='D')nd={i,j};
}
}
prep();
int l=-1,p=1e4,md;
while(l<p){
md=(l+p+1)/2;
iter++;
if (bfs(md))l=md;
else p=md-1;
}
cout << l << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
89 ms |
5716 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
0 ms |
600 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
0 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
612 KB |
Output is correct |
32 |
Correct |
1 ms |
608 KB |
Output is correct |
33 |
Incorrect |
3 ms |
1916 KB |
Output isn't correct |
34 |
Incorrect |
3 ms |
1892 KB |
Output isn't correct |
35 |
Correct |
15 ms |
3012 KB |
Output is correct |
36 |
Incorrect |
4 ms |
2148 KB |
Output isn't correct |
37 |
Incorrect |
4 ms |
2148 KB |
Output isn't correct |
38 |
Correct |
17 ms |
3428 KB |
Output is correct |
39 |
Incorrect |
4 ms |
2404 KB |
Output isn't correct |
40 |
Incorrect |
5 ms |
2404 KB |
Output isn't correct |
41 |
Correct |
23 ms |
3708 KB |
Output is correct |
42 |
Incorrect |
5 ms |
2648 KB |
Output isn't correct |
43 |
Incorrect |
5 ms |
2652 KB |
Output isn't correct |
44 |
Correct |
25 ms |
4256 KB |
Output is correct |
45 |
Incorrect |
6 ms |
2908 KB |
Output isn't correct |
46 |
Incorrect |
6 ms |
2908 KB |
Output isn't correct |
47 |
Correct |
29 ms |
4444 KB |
Output is correct |
48 |
Incorrect |
7 ms |
3160 KB |
Output isn't correct |
49 |
Incorrect |
7 ms |
3064 KB |
Output isn't correct |
50 |
Correct |
35 ms |
5056 KB |
Output is correct |
51 |
Incorrect |
8 ms |
3420 KB |
Output isn't correct |
52 |
Incorrect |
7 ms |
3420 KB |
Output isn't correct |
53 |
Correct |
43 ms |
5244 KB |
Output is correct |
54 |
Incorrect |
9 ms |
3676 KB |
Output isn't correct |
55 |
Incorrect |
11 ms |
3672 KB |
Output isn't correct |
56 |
Correct |
52 ms |
5696 KB |
Output is correct |
57 |
Incorrect |
11 ms |
3928 KB |
Output isn't correct |
58 |
Incorrect |
17 ms |
3932 KB |
Output isn't correct |
59 |
Correct |
58 ms |
6312 KB |
Output is correct |
60 |
Incorrect |
17 ms |
4188 KB |
Output isn't correct |
61 |
Incorrect |
11 ms |
4184 KB |
Output isn't correct |
62 |
Correct |
76 ms |
6748 KB |
Output is correct |
63 |
Correct |
70 ms |
6748 KB |
Output is correct |
64 |
Correct |
134 ms |
6484 KB |
Output is correct |
65 |
Correct |
112 ms |
6748 KB |
Output is correct |
66 |
Correct |
82 ms |
6736 KB |
Output is correct |
67 |
Correct |
77 ms |
6736 KB |
Output is correct |
68 |
Correct |
34 ms |
6744 KB |
Output is correct |
69 |
Correct |
33 ms |
6740 KB |
Output is correct |
70 |
Correct |
25 ms |
6736 KB |
Output is correct |
71 |
Correct |
30 ms |
6736 KB |
Output is correct |
72 |
Incorrect |
22 ms |
4692 KB |
Output isn't correct |
73 |
Incorrect |
42 ms |
7004 KB |
Output isn't correct |
74 |
Correct |
37 ms |
7000 KB |
Output is correct |
75 |
Correct |
45 ms |
7000 KB |
Output is correct |
76 |
Correct |
40 ms |
7008 KB |
Output is correct |
77 |
Correct |
42 ms |
6996 KB |
Output is correct |
78 |
Correct |
48 ms |
5696 KB |
Output is correct |
79 |
Correct |
31 ms |
5200 KB |
Output is correct |
80 |
Correct |
44 ms |
5208 KB |
Output is correct |
81 |
Correct |
43 ms |
5380 KB |
Output is correct |
82 |
Correct |
37 ms |
5408 KB |
Output is correct |
83 |
Correct |
48 ms |
5624 KB |
Output is correct |
84 |
Correct |
45 ms |
5468 KB |
Output is correct |
85 |
Correct |
46 ms |
5464 KB |
Output is correct |
86 |
Correct |
54 ms |
5468 KB |
Output is correct |
87 |
Correct |
45 ms |
5452 KB |
Output is correct |
88 |
Correct |
48 ms |
5716 KB |
Output is correct |
89 |
Correct |
51 ms |
5468 KB |
Output is correct |
90 |
Correct |
52 ms |
5460 KB |
Output is correct |
91 |
Correct |
77 ms |
5712 KB |
Output is correct |
92 |
Correct |
62 ms |
5712 KB |
Output is correct |