#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
const int inf=1e9;
int n, s, Md, can;
array<int, 2>Str, End;
vector<string>A;
vector<vector<int>>Pri;
queue<array<int, 2>>q, qu;
int dx[]={0, 0, 1, -1};
int dy[]={1, -1, 0, 0};
void pre(){
A.clear(), Pri.clear();
A.resize(n), Pri.resize(n, vector<int>(n, inf));
}
bool valid(int i, int j){
return !(i<0 || i>=n || j<0 || j>=n);
}
void bfs(){
while(!q.empty()){
array<int, 2>u=q.front();
q.pop();
int i=u[0], j=u[1];
for(int f=0;f<4;f++){
int ii=i+dx[f], jj=j+dy[f];
if(valid(ii, jj) && A[ii][jj]!='D' && Pri[i][j]+1<Pri[ii][jj])
Pri[ii][jj]=Pri[i][j]+1, q.push({ii, jj});
}
}
}
void check(){
vector<vector<int>>Dis(n, vector<int>(n, inf));
Dis[Str[0]][Str[1]]=0;
while(!qu.empty()){
array<int, 2>u=qu.front();
qu.pop();
int i=u[0], j=u[1];
if(Md+Dis[i][j]/s>=Pri[i][j]){
continue;
}
if(i==End[0] && j==End[1]){
can=1;
continue;
}
for(int f=0;f<4;f++){
int ii=i+dx[f], jj=j+dy[f];
if(valid(ii, jj) && Dis[i][j]+1<Dis[ii][jj]){
Dis[ii][jj]=Dis[i][j]+1, qu.push({ii, jj});
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>s;
pre();
for(int i=0;i<n;i++){
cin>>A[i];
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(A[i][j]=='H')
q.push({i, j}), Pri[i][j]=0;
else if(A[i][j]=='M')
Str[0]=i, Str[1]=j;
else if(A[i][j]=='D')
End[0]=i, End[1]=j;
else if(A[i][j]=='T')
Pri[i][j]=0;
}
}
bfs();
int l=0, r=n*n, ans=-1;
while(l<=r){
int md=(l+r)/2;
Md=md;
can=0;
qu.push({Str[0], Str[1]});
check();
if(can)
ans=md, l=md+1;
else
r=md-1;
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
81 ms |
6420 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
600 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
420 KB |
Output is correct |
29 |
Correct |
0 ms |
600 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
5 ms |
1568 KB |
Output is correct |
34 |
Correct |
5 ms |
1572 KB |
Output is correct |
35 |
Correct |
20 ms |
1584 KB |
Output is correct |
36 |
Correct |
7 ms |
1884 KB |
Output is correct |
37 |
Correct |
7 ms |
1896 KB |
Output is correct |
38 |
Correct |
24 ms |
1884 KB |
Output is correct |
39 |
Correct |
8 ms |
2348 KB |
Output is correct |
40 |
Correct |
10 ms |
2268 KB |
Output is correct |
41 |
Correct |
37 ms |
2360 KB |
Output is correct |
42 |
Correct |
10 ms |
2780 KB |
Output is correct |
43 |
Correct |
11 ms |
2776 KB |
Output is correct |
44 |
Correct |
41 ms |
2788 KB |
Output is correct |
45 |
Correct |
12 ms |
3180 KB |
Output is correct |
46 |
Correct |
13 ms |
3192 KB |
Output is correct |
47 |
Correct |
52 ms |
3200 KB |
Output is correct |
48 |
Correct |
14 ms |
3780 KB |
Output is correct |
49 |
Correct |
17 ms |
3772 KB |
Output is correct |
50 |
Correct |
55 ms |
3740 KB |
Output is correct |
51 |
Correct |
23 ms |
4328 KB |
Output is correct |
52 |
Correct |
20 ms |
4188 KB |
Output is correct |
53 |
Correct |
75 ms |
4184 KB |
Output is correct |
54 |
Correct |
20 ms |
4912 KB |
Output is correct |
55 |
Correct |
22 ms |
4920 KB |
Output is correct |
56 |
Correct |
84 ms |
4904 KB |
Output is correct |
57 |
Correct |
22 ms |
5576 KB |
Output is correct |
58 |
Correct |
26 ms |
5572 KB |
Output is correct |
59 |
Correct |
96 ms |
5548 KB |
Output is correct |
60 |
Correct |
26 ms |
6268 KB |
Output is correct |
61 |
Correct |
33 ms |
6260 KB |
Output is correct |
62 |
Correct |
109 ms |
6236 KB |
Output is correct |
63 |
Correct |
103 ms |
6228 KB |
Output is correct |
64 |
Correct |
193 ms |
6232 KB |
Output is correct |
65 |
Correct |
146 ms |
6216 KB |
Output is correct |
66 |
Correct |
120 ms |
6224 KB |
Output is correct |
67 |
Correct |
117 ms |
6472 KB |
Output is correct |
68 |
Correct |
49 ms |
6300 KB |
Output is correct |
69 |
Correct |
50 ms |
6284 KB |
Output is correct |
70 |
Correct |
38 ms |
6260 KB |
Output is correct |
71 |
Correct |
45 ms |
6292 KB |
Output is correct |
72 |
Correct |
33 ms |
6288 KB |
Output is correct |
73 |
Correct |
36 ms |
6256 KB |
Output is correct |
74 |
Correct |
49 ms |
6276 KB |
Output is correct |
75 |
Correct |
62 ms |
6288 KB |
Output is correct |
76 |
Correct |
54 ms |
6292 KB |
Output is correct |
77 |
Correct |
60 ms |
6252 KB |
Output is correct |
78 |
Correct |
63 ms |
6260 KB |
Output is correct |
79 |
Correct |
48 ms |
6316 KB |
Output is correct |
80 |
Correct |
55 ms |
6552 KB |
Output is correct |
81 |
Correct |
57 ms |
6248 KB |
Output is correct |
82 |
Correct |
51 ms |
6292 KB |
Output is correct |
83 |
Correct |
63 ms |
6284 KB |
Output is correct |
84 |
Correct |
57 ms |
6264 KB |
Output is correct |
85 |
Correct |
69 ms |
6336 KB |
Output is correct |
86 |
Correct |
61 ms |
6308 KB |
Output is correct |
87 |
Correct |
61 ms |
6256 KB |
Output is correct |
88 |
Correct |
74 ms |
6268 KB |
Output is correct |
89 |
Correct |
84 ms |
6276 KB |
Output is correct |
90 |
Correct |
74 ms |
6380 KB |
Output is correct |
91 |
Correct |
66 ms |
6304 KB |
Output is correct |
92 |
Correct |
71 ms |
6292 KB |
Output is correct |