#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]=='D' or c[x2][y2]=='T' or visited[x2][y2]) continue;
visited[x2][y2]=1;
d[x2][y2]=d[x1][y1]+1;
q.push({x2,y2});
}
}
}
bool bsearch(int k){
if (k==d0[px][py]) return 0;
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]=INT_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 (tx<0 or tx>=n) continue;
if (ty<0 or ty>=n) continue;
if (c[tx][ty]=='H' or c[tx][ty]=='T' or vis1[tx][ty]) continue;
d1[tx][ty]=d1[sx][sy]+1;
int moves=k+d1[tx][ty]/s;
if (moves>=d0[tx][ty]) continue;
step[tx][ty]=moves+1;
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 or c[tx][ty]=='T' or c[tx][ty]=='H') 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++){
cin>>c[i][j];
if (c[i][j]=='D'){
dx=i;dy=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=1e9;
while (high>low){
int mid=low+(high-low+1)/2;
if (bsearch(mid)) low=mid;
else high=mid-1;
}
if (!bsearch(low)) cout <<-1;
else cout <<low;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
149 ms |
15352 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
724 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
852 KB |
Output is correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
2 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
2 ms |
1364 KB |
Output is correct |
32 |
Correct |
2 ms |
1364 KB |
Output is correct |
33 |
Correct |
14 ms |
6484 KB |
Output is correct |
34 |
Correct |
15 ms |
6372 KB |
Output is correct |
35 |
Correct |
32 ms |
6476 KB |
Output is correct |
36 |
Correct |
18 ms |
7508 KB |
Output is correct |
37 |
Correct |
18 ms |
7508 KB |
Output is correct |
38 |
Correct |
41 ms |
7508 KB |
Output is correct |
39 |
Correct |
21 ms |
8532 KB |
Output is correct |
40 |
Correct |
22 ms |
8532 KB |
Output is correct |
41 |
Correct |
50 ms |
8472 KB |
Output is correct |
42 |
Correct |
25 ms |
9648 KB |
Output is correct |
43 |
Correct |
26 ms |
9592 KB |
Output is correct |
44 |
Correct |
68 ms |
9660 KB |
Output is correct |
45 |
Correct |
34 ms |
10612 KB |
Output is correct |
46 |
Correct |
32 ms |
10536 KB |
Output is correct |
47 |
Correct |
86 ms |
10596 KB |
Output is correct |
48 |
Correct |
40 ms |
11500 KB |
Output is correct |
49 |
Correct |
40 ms |
11476 KB |
Output is correct |
50 |
Correct |
131 ms |
11468 KB |
Output is correct |
51 |
Correct |
50 ms |
12376 KB |
Output is correct |
52 |
Correct |
51 ms |
12392 KB |
Output is correct |
53 |
Correct |
129 ms |
12456 KB |
Output is correct |
54 |
Correct |
65 ms |
13356 KB |
Output is correct |
55 |
Correct |
65 ms |
13328 KB |
Output is correct |
56 |
Correct |
144 ms |
13392 KB |
Output is correct |
57 |
Correct |
73 ms |
14320 KB |
Output is correct |
58 |
Correct |
72 ms |
14328 KB |
Output is correct |
59 |
Correct |
175 ms |
14304 KB |
Output is correct |
60 |
Correct |
82 ms |
15228 KB |
Output is correct |
61 |
Correct |
91 ms |
15256 KB |
Output is correct |
62 |
Correct |
204 ms |
15256 KB |
Output is correct |
63 |
Correct |
179 ms |
15260 KB |
Output is correct |
64 |
Correct |
263 ms |
15224 KB |
Output is correct |
65 |
Correct |
259 ms |
15348 KB |
Output is correct |
66 |
Correct |
200 ms |
15260 KB |
Output is correct |
67 |
Correct |
185 ms |
15260 KB |
Output is correct |
68 |
Correct |
107 ms |
15284 KB |
Output is correct |
69 |
Correct |
119 ms |
15232 KB |
Output is correct |
70 |
Correct |
107 ms |
15284 KB |
Output is correct |
71 |
Correct |
96 ms |
15236 KB |
Output is correct |
72 |
Correct |
92 ms |
15252 KB |
Output is correct |
73 |
Correct |
99 ms |
15552 KB |
Output is correct |
74 |
Correct |
129 ms |
15552 KB |
Output is correct |
75 |
Correct |
131 ms |
15500 KB |
Output is correct |
76 |
Correct |
125 ms |
15556 KB |
Output is correct |
77 |
Correct |
129 ms |
15552 KB |
Output is correct |
78 |
Correct |
136 ms |
15516 KB |
Output is correct |
79 |
Correct |
127 ms |
15568 KB |
Output is correct |
80 |
Correct |
128 ms |
15516 KB |
Output is correct |
81 |
Correct |
127 ms |
15520 KB |
Output is correct |
82 |
Correct |
121 ms |
15416 KB |
Output is correct |
83 |
Correct |
139 ms |
15476 KB |
Output is correct |
84 |
Correct |
136 ms |
15384 KB |
Output is correct |
85 |
Correct |
138 ms |
15436 KB |
Output is correct |
86 |
Correct |
155 ms |
15436 KB |
Output is correct |
87 |
Correct |
142 ms |
15476 KB |
Output is correct |
88 |
Correct |
142 ms |
15292 KB |
Output is correct |
89 |
Correct |
150 ms |
15396 KB |
Output is correct |
90 |
Correct |
151 ms |
15416 KB |
Output is correct |
91 |
Correct |
148 ms |
15432 KB |
Output is correct |
92 |
Correct |
160 ms |
15416 KB |
Output is correct |