#include <bits/stdc++.h>
#include <iostream>
#include<queue>
#include<vector>
#include<utility>
using namespace std;
typedef long long ll;
const int sz=1005;
int n,s;
int d1[sz][sz];
int d0[sz][sz];
bool vis1[sz][sz];
bool vis0[sz][sz];
vector<pair<int,int>>hiv;
int px,py;
int dx,dy;
ll step[sz][sz];
int movex[4]={0,0,1,-1};
int movey[4]={-1,1,0,0};
char c[sz][sz];
void bfs(vector<pair<int,int>>&a,int d[sz][sz],bool visited[sz][sz]){
queue<pair<int,int>>q;
for (auto i:a){
q.push(i);
visited[i.first][i.second]=1;
}
while (!q.empty()){
int x1=q.front().first;
int y1=q.front().second;
q.pop();
for (int i=0;i<4;i++){
int x2=x1+movex[i];
int y2=y1+movey[i];
if (x2<0 or x2>=n) continue;
if (y2<0 or y2>=n) continue;
if (c[x2][y2]!='.' or visited[x2][y2]) continue;
visited[x2][y2]=1;
d[x2][y2]=d[x1][y1]+1;
q.push({x2,y2});
}
}
}
bool bsearch(int k){
for (int i=0;i<n;i++){
for(int j=0;j<n;j++){
vis1[i][j]=0;
d1[i][j]=0;
step[i][j]=LLONG_MAX;
}
}
queue<pair<int,int>>q;
q.push({px,py});
step[px][py]=k;
while (!q.empty()){
int sx=q.front().first;
int sy=q.front().second;
q.pop();
for (int i=0;i<4;i++){
int tx=sx+movex[i];
int ty=sy+movey[i];
if (ty<0 or tx>=n) continue;
if (ty<0 or ty>=n) continue;
if (c[tx][ty]!='.' or vis1[tx][ty]) continue;
d1[tx][ty]=d1[sx][sy]+1;
int moves=k+d1[tx][ty]%s;
if (moves==k) moves++;
if (moves>=d0[tx][ty]) continue;
step[tx][ty]=moves;
vis1[tx][ty]=1;
q.push({tx,ty});
}
} for (int i=0;i<4;i++){
int tx=dx+movex[i];
int ty=dy+movey[i];
if (tx<0 or tx>=n or ty<0 or ty>=n) continue;
if (step[tx][ty]<d0[tx][ty]) return 1;
} return 0;
}
int main(){
cin>>n>>s;
for (int i=0;i<n;i++){
for(int j=0;j<n;j++){
char y; cin>>y;
c[i][j]=y;
if (c[i][j]=='G'){
c[i][j]='.';
}
if (c[i][j]=='D'){
dx=i;dy=j;
c[i][j]='.';
}
if (c[i][j]=='M'){
px=i;py=j;
} if (c[i][j]=='H'){
hiv.push_back({i,j});
}
}
} bfs(hiv,d0,vis0);
int low=0;int high=1e6;
while (high>low){
int mid=low+(high-low+1)/2;
if (bsearch(mid)) low=mid;
else high=mid-1;
} if (low==1e6) cout <<-1;
else cout <<low;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
7 |
Incorrect |
175 ms |
15440 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Incorrect |
1 ms |
852 KB |
Output isn't correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Incorrect |
2 ms |
1236 KB |
Output isn't correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Incorrect |
1 ms |
1364 KB |
Output isn't correct |
31 |
Correct |
1 ms |
1364 KB |
Output is correct |
32 |
Incorrect |
1 ms |
1364 KB |
Output isn't correct |
33 |
Correct |
14 ms |
6468 KB |
Output is correct |
34 |
Incorrect |
15 ms |
6356 KB |
Output isn't correct |
35 |
Incorrect |
16 ms |
6396 KB |
Output isn't correct |
36 |
Correct |
17 ms |
7380 KB |
Output is correct |
37 |
Incorrect |
18 ms |
7436 KB |
Output isn't correct |
38 |
Incorrect |
19 ms |
7500 KB |
Output isn't correct |
39 |
Correct |
22 ms |
8472 KB |
Output is correct |
40 |
Incorrect |
21 ms |
8532 KB |
Output isn't correct |
41 |
Incorrect |
25 ms |
8532 KB |
Output isn't correct |
42 |
Correct |
28 ms |
9680 KB |
Output is correct |
43 |
Incorrect |
27 ms |
9668 KB |
Output isn't correct |
44 |
Incorrect |
30 ms |
9564 KB |
Output isn't correct |
45 |
Correct |
42 ms |
10580 KB |
Output is correct |
46 |
Incorrect |
36 ms |
10496 KB |
Output isn't correct |
47 |
Incorrect |
37 ms |
10572 KB |
Output isn't correct |
48 |
Correct |
37 ms |
11520 KB |
Output is correct |
49 |
Incorrect |
37 ms |
11520 KB |
Output isn't correct |
50 |
Incorrect |
47 ms |
11468 KB |
Output isn't correct |
51 |
Correct |
50 ms |
12364 KB |
Output is correct |
52 |
Incorrect |
45 ms |
12364 KB |
Output isn't correct |
53 |
Incorrect |
53 ms |
12364 KB |
Output isn't correct |
54 |
Correct |
58 ms |
13356 KB |
Output is correct |
55 |
Incorrect |
53 ms |
13388 KB |
Output isn't correct |
56 |
Incorrect |
59 ms |
13340 KB |
Output isn't correct |
57 |
Correct |
63 ms |
14316 KB |
Output is correct |
58 |
Incorrect |
64 ms |
14320 KB |
Output isn't correct |
59 |
Incorrect |
69 ms |
14284 KB |
Output isn't correct |
60 |
Correct |
68 ms |
15152 KB |
Output is correct |
61 |
Incorrect |
69 ms |
15180 KB |
Output isn't correct |
62 |
Incorrect |
87 ms |
15260 KB |
Output isn't correct |
63 |
Incorrect |
179 ms |
15288 KB |
Output isn't correct |
64 |
Incorrect |
162 ms |
15264 KB |
Output isn't correct |
65 |
Incorrect |
225 ms |
15160 KB |
Output isn't correct |
66 |
Incorrect |
171 ms |
15164 KB |
Output isn't correct |
67 |
Incorrect |
218 ms |
15344 KB |
Output isn't correct |
68 |
Correct |
84 ms |
15292 KB |
Output is correct |
69 |
Incorrect |
81 ms |
15168 KB |
Output isn't correct |
70 |
Incorrect |
94 ms |
15284 KB |
Output isn't correct |
71 |
Incorrect |
109 ms |
15344 KB |
Output isn't correct |
72 |
Incorrect |
96 ms |
15292 KB |
Output isn't correct |
73 |
Incorrect |
111 ms |
15556 KB |
Output isn't correct |
74 |
Incorrect |
87 ms |
15560 KB |
Output isn't correct |
75 |
Incorrect |
119 ms |
15552 KB |
Output isn't correct |
76 |
Incorrect |
106 ms |
15556 KB |
Output isn't correct |
77 |
Incorrect |
111 ms |
15468 KB |
Output isn't correct |
78 |
Incorrect |
131 ms |
15508 KB |
Output isn't correct |
79 |
Incorrect |
77 ms |
15504 KB |
Output isn't correct |
80 |
Incorrect |
99 ms |
15516 KB |
Output isn't correct |
81 |
Incorrect |
120 ms |
15516 KB |
Output isn't correct |
82 |
Incorrect |
130 ms |
15460 KB |
Output isn't correct |
83 |
Incorrect |
136 ms |
15436 KB |
Output isn't correct |
84 |
Incorrect |
82 ms |
15480 KB |
Output isn't correct |
85 |
Incorrect |
120 ms |
15412 KB |
Output isn't correct |
86 |
Incorrect |
131 ms |
15436 KB |
Output isn't correct |
87 |
Incorrect |
124 ms |
15356 KB |
Output isn't correct |
88 |
Incorrect |
149 ms |
15500 KB |
Output isn't correct |
89 |
Incorrect |
86 ms |
15368 KB |
Output isn't correct |
90 |
Incorrect |
155 ms |
15308 KB |
Output isn't correct |
91 |
Incorrect |
123 ms |
15416 KB |
Output isn't correct |
92 |
Incorrect |
118 ms |
15304 KB |
Output isn't correct |