#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int iini, ifin, jini, jfin, n, s;
bool way_home(int m, vector<vector<int> >&bemoment, vector<vector<char> >&grid){
queue<pair<int,int> >mecho;
vector<vector<pair<int,int> > >mechomoment(n, vector<pair<int,int> >(n, make_pair(1e9, 0)));
mecho.push(make_pair(iini, jini));
mechomoment[iini][jini]=make_pair(m, 1);
while (!mecho.empty()){
int i=mecho.front().first;
int j=mecho.front().second;
mecho.pop();
int minut=mechomoment[i][j].first, steps=mechomoment[i][j].second;
if (steps==s){
minut++;
steps=0;
}steps++;
if ((i!=0 && grid[i-1][j]=='D')||(j!=0 && grid[i][j-1]=='D')||(i!=n-1 && grid[i+1][j]=='D')||((j!=n-1 && grid[i][j+1]=='D'))){
return true;
}
if (i!=0 and bemoment[i-1][j]>minut and mechomoment[i-1][j].first==1e9){
mechomoment[i-1][j]=make_pair(minut, steps);
mecho.push(make_pair(i-1, j));
}if (j!=0 and bemoment[i][j-1]>minut and mechomoment[i][j-1].first==1e9){
mechomoment[i][j-1]=make_pair(minut, steps);
mecho.push(make_pair(i, j-1));
}if (i!=n-1 and bemoment[i+1][j]>minut and mechomoment[i+1][j].first==1e9){
mechomoment[i+1][j]=make_pair(minut, steps);
mecho.push(make_pair(i+1, j));
}if (j!=n-1 and bemoment[i][j+1]>minut and mechomoment[i][j+1].first==1e9){
mechomoment[i][j+1]=make_pair(minut, steps);
mecho.push(make_pair(i, j+1));
}
}return false;
}
int main(){
cin>>n>>s;
vector<vector<char> >grid(n, vector<char>(n));
vector<vector<int> >bemoment(n, vector<int>(n, 1e9));
queue<pair<int,int> >grassy;
for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
cin>>grid[i][j];
if (grid[i][j]=='H'){
grassy.push(make_pair(i, j));
bemoment[i][j]=0;
}else if (grid[i][j]=='M'){
iini=i;
jini=j;
}else if (grid[i][j]=='D'){
ifin=i;
jfin=j;
bemoment[i][j]=-1;
}else if (grid[i][j]=='T') bemoment[i][j]=-1;
}
}while (!grassy.empty()){
int i=grassy.front().first;
int j=grassy.front().second;
grassy.pop();
if (i!=0 and bemoment[i-1][j]==1e9){
bemoment[i-1][j]=bemoment[i][j]+1;
grassy.push(make_pair(i-1, j));
}if (j!=0 and bemoment[i][j-1]==1e9){
bemoment[i][j-1]=bemoment[i][j]+1;
grassy.push(make_pair(i, j-1));
}if (i!=n-1 and bemoment[i+1][j]==1e9){
bemoment[i+1][j]=bemoment[i][j]+1;
grassy.push(make_pair(i+1, j));
}if (j!=n-1 and bemoment[i][j+1]==1e9){
bemoment[i][j+1]=bemoment[i][j]+1;
grassy.push(make_pair(i, j+1));
}
}int ans=-1;
int l=0, r=bemoment[iini][jini]-1, m;
while (l<=r){
m=(r-l)/2+l;
if (l+1==r){
if (way_home(r, bemoment, grid)){
ans=r;
l+=2;
}else if (way_home(l, bemoment, grid)){
ans=l;
r-=2;
}else ans=-1;
break;
}else if (l==r){
if (way_home(r, bemoment, grid)){
ans=r;
l++;
}else ans=-1;
break;
}
if (way_home(m, bemoment, grid)) l=m;
else r=m-1;
}cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 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 |
604 KB |
Output is correct |
7 |
Correct |
81 ms |
9236 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
436 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
396 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
11 ms |
2156 KB |
Output is correct |
34 |
Correct |
10 ms |
2156 KB |
Output is correct |
35 |
Correct |
19 ms |
2180 KB |
Output is correct |
36 |
Correct |
14 ms |
2676 KB |
Output is correct |
37 |
Correct |
14 ms |
2728 KB |
Output is correct |
38 |
Correct |
24 ms |
2628 KB |
Output is correct |
39 |
Correct |
17 ms |
3292 KB |
Output is correct |
40 |
Correct |
18 ms |
3296 KB |
Output is correct |
41 |
Correct |
35 ms |
3252 KB |
Output is correct |
42 |
Correct |
21 ms |
3940 KB |
Output is correct |
43 |
Correct |
21 ms |
3972 KB |
Output is correct |
44 |
Correct |
43 ms |
3904 KB |
Output is correct |
45 |
Correct |
29 ms |
4668 KB |
Output is correct |
46 |
Correct |
26 ms |
4672 KB |
Output is correct |
47 |
Correct |
49 ms |
4640 KB |
Output is correct |
48 |
Correct |
38 ms |
5492 KB |
Output is correct |
49 |
Correct |
32 ms |
5496 KB |
Output is correct |
50 |
Correct |
55 ms |
5424 KB |
Output is correct |
51 |
Correct |
38 ms |
6328 KB |
Output is correct |
52 |
Correct |
36 ms |
6312 KB |
Output is correct |
53 |
Correct |
71 ms |
6292 KB |
Output is correct |
54 |
Correct |
43 ms |
7324 KB |
Output is correct |
55 |
Correct |
47 ms |
7584 KB |
Output is correct |
56 |
Correct |
82 ms |
7208 KB |
Output is correct |
57 |
Correct |
51 ms |
8328 KB |
Output is correct |
58 |
Correct |
50 ms |
8292 KB |
Output is correct |
59 |
Correct |
95 ms |
8180 KB |
Output is correct |
60 |
Correct |
63 ms |
9680 KB |
Output is correct |
61 |
Correct |
57 ms |
9284 KB |
Output is correct |
62 |
Correct |
108 ms |
9180 KB |
Output is correct |
63 |
Correct |
116 ms |
9180 KB |
Output is correct |
64 |
Correct |
163 ms |
9188 KB |
Output is correct |
65 |
Correct |
163 ms |
9180 KB |
Output is correct |
66 |
Correct |
144 ms |
9180 KB |
Output is correct |
67 |
Correct |
124 ms |
9196 KB |
Output is correct |
68 |
Correct |
63 ms |
9296 KB |
Output is correct |
69 |
Correct |
73 ms |
9464 KB |
Output is correct |
70 |
Correct |
60 ms |
9336 KB |
Output is correct |
71 |
Correct |
53 ms |
9316 KB |
Output is correct |
72 |
Correct |
58 ms |
9264 KB |
Output is correct |
73 |
Correct |
37 ms |
9128 KB |
Output is correct |
74 |
Correct |
81 ms |
9636 KB |
Output is correct |
75 |
Correct |
76 ms |
9344 KB |
Output is correct |
76 |
Correct |
70 ms |
9344 KB |
Output is correct |
77 |
Correct |
72 ms |
9360 KB |
Output is correct |
78 |
Correct |
80 ms |
9252 KB |
Output is correct |
79 |
Correct |
72 ms |
9208 KB |
Output is correct |
80 |
Correct |
70 ms |
9196 KB |
Output is correct |
81 |
Correct |
80 ms |
9212 KB |
Output is correct |
82 |
Correct |
71 ms |
9264 KB |
Output is correct |
83 |
Correct |
83 ms |
9180 KB |
Output is correct |
84 |
Correct |
84 ms |
9236 KB |
Output is correct |
85 |
Correct |
86 ms |
9220 KB |
Output is correct |
86 |
Correct |
95 ms |
9244 KB |
Output is correct |
87 |
Correct |
90 ms |
9204 KB |
Output is correct |
88 |
Correct |
85 ms |
9216 KB |
Output is correct |
89 |
Correct |
87 ms |
9232 KB |
Output is correct |
90 |
Correct |
98 ms |
9248 KB |
Output is correct |
91 |
Correct |
101 ms |
9224 KB |
Output is correct |
92 |
Correct |
99 ms |
9232 KB |
Output is correct |