#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
void solve(){
int n,k;
cin >> n >> k;
string arr[n];
for(int x=0;x<n;x++){
cin >> arr[x];
}
int l=0;
int r=n;
int best=-1;
int mid;
pii start;
pii done;
int dist[n+5][n+5];
memset(dist,-1,sizeof(dist));
queue<pii>q;
for(int x=0;x<n;x++){
for(int y=0;y<n;y++){
if(arr[x][y]=='H'){
q.push({x,y});
dist[x][y]=0;
}
else if(arr[x][y]=='M') start={x,y};
else if(arr[x][y]=='D') done={x,y};
}
}
pii dir[4]={
{0,1},
{0,-1},
{1,0},
{-1,0},
};
while(!q.empty()){
pii cur=q.front();
q.pop();
for(auto it:dir){
int newx=cur.first+it.first;
int newy=cur.second+it.second;
if(newx<0||newy<0||newx>=n||newy>=n) continue;
if(dist[newx][newy]!=-1) continue;
dist[newx][newy]=dist[cur.first][cur.second]+1;
q.push({newx,newy});
}
}
//for(int x=0;x<n;x++){
//for(int y=0;y<n;y++){
//cout << dist[x][y] << " ";
//}
//cout << "\n";
//}
while(l<=r){
mid=(l+r)/2;
int dist2[n][n];
memset(dist2,-1,sizeof(dist2));
dist2[start.first][start.second]=k*mid;
q.push({start.first,start.second});
while(!q.empty()){
pii cur=q.front();
q.pop();
for(auto it:dir){
int newx=cur.first+it.first;
int newy=cur.second+it.second;
if(newx<0||newy<0||newx>=n||newy>=n) continue;
if(dist2[newx][newy]!=-1) continue;
int hold=(dist2[cur.first][cur.second]+k)/k;
if(dist[newx][newy]<hold) continue;
dist2[newx][newy]=dist2[cur.first][cur.second]+1;
q.push({newx,newy});
}
}
if(dist2[done.first][done.second]!=-1){
best=mid;
l=mid+1;
}
else r=mid-1;
}
cout << best;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("out.txt", "w", stdout);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
47 ms |
11304 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
25 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
29 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
30 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
6 ms |
2344 KB |
Output isn't correct |
34 |
Incorrect |
10 ms |
2540 KB |
Output isn't correct |
35 |
Incorrect |
21 ms |
2396 KB |
Output isn't correct |
36 |
Incorrect |
5 ms |
3164 KB |
Output isn't correct |
37 |
Incorrect |
10 ms |
3172 KB |
Output isn't correct |
38 |
Incorrect |
24 ms |
3184 KB |
Output isn't correct |
39 |
Incorrect |
9 ms |
3672 KB |
Output isn't correct |
40 |
Incorrect |
14 ms |
3676 KB |
Output isn't correct |
41 |
Incorrect |
25 ms |
3896 KB |
Output isn't correct |
42 |
Incorrect |
8 ms |
4444 KB |
Output isn't correct |
43 |
Incorrect |
18 ms |
4444 KB |
Output isn't correct |
44 |
Incorrect |
35 ms |
4440 KB |
Output isn't correct |
45 |
Incorrect |
14 ms |
5468 KB |
Output isn't correct |
46 |
Incorrect |
21 ms |
5576 KB |
Output isn't correct |
47 |
Incorrect |
42 ms |
5464 KB |
Output isn't correct |
48 |
Incorrect |
12 ms |
6492 KB |
Output isn't correct |
49 |
Incorrect |
27 ms |
6540 KB |
Output isn't correct |
50 |
Incorrect |
56 ms |
6492 KB |
Output isn't correct |
51 |
Incorrect |
20 ms |
7516 KB |
Output isn't correct |
52 |
Incorrect |
31 ms |
7356 KB |
Output isn't correct |
53 |
Incorrect |
63 ms |
7580 KB |
Output isn't correct |
54 |
Incorrect |
16 ms |
8540 KB |
Output isn't correct |
55 |
Incorrect |
36 ms |
8540 KB |
Output isn't correct |
56 |
Incorrect |
75 ms |
8540 KB |
Output isn't correct |
57 |
Incorrect |
26 ms |
9816 KB |
Output isn't correct |
58 |
Incorrect |
41 ms |
9820 KB |
Output isn't correct |
59 |
Incorrect |
94 ms |
9940 KB |
Output isn't correct |
60 |
Incorrect |
20 ms |
11100 KB |
Output isn't correct |
61 |
Incorrect |
46 ms |
11100 KB |
Output isn't correct |
62 |
Incorrect |
108 ms |
11248 KB |
Output isn't correct |
63 |
Incorrect |
20 ms |
11096 KB |
Output isn't correct |
64 |
Incorrect |
51 ms |
11100 KB |
Output isn't correct |
65 |
Incorrect |
32 ms |
11216 KB |
Output isn't correct |
66 |
Incorrect |
27 ms |
11096 KB |
Output isn't correct |
67 |
Incorrect |
22 ms |
11100 KB |
Output isn't correct |
68 |
Incorrect |
19 ms |
11356 KB |
Output isn't correct |
69 |
Incorrect |
24 ms |
11524 KB |
Output isn't correct |
70 |
Incorrect |
21 ms |
11352 KB |
Output isn't correct |
71 |
Incorrect |
20 ms |
11352 KB |
Output isn't correct |
72 |
Incorrect |
20 ms |
11348 KB |
Output isn't correct |
73 |
Incorrect |
22 ms |
11648 KB |
Output isn't correct |
74 |
Incorrect |
34 ms |
11648 KB |
Output isn't correct |
75 |
Incorrect |
51 ms |
11772 KB |
Output isn't correct |
76 |
Incorrect |
39 ms |
11612 KB |
Output isn't correct |
77 |
Incorrect |
42 ms |
11608 KB |
Output isn't correct |
78 |
Correct |
43 ms |
11612 KB |
Output is correct |
79 |
Incorrect |
37 ms |
11608 KB |
Output isn't correct |
80 |
Incorrect |
48 ms |
11604 KB |
Output isn't correct |
81 |
Incorrect |
44 ms |
11612 KB |
Output isn't correct |
82 |
Incorrect |
38 ms |
11612 KB |
Output isn't correct |
83 |
Incorrect |
47 ms |
11660 KB |
Output isn't correct |
84 |
Incorrect |
42 ms |
11608 KB |
Output isn't correct |
85 |
Incorrect |
45 ms |
11812 KB |
Output isn't correct |
86 |
Incorrect |
43 ms |
11612 KB |
Output isn't correct |
87 |
Incorrect |
46 ms |
11612 KB |
Output isn't correct |
88 |
Incorrect |
53 ms |
11352 KB |
Output isn't correct |
89 |
Incorrect |
49 ms |
11536 KB |
Output isn't correct |
90 |
Incorrect |
46 ms |
11356 KB |
Output isn't correct |
91 |
Incorrect |
47 ms |
11352 KB |
Output isn't correct |
92 |
Incorrect |
50 ms |
11356 KB |
Output isn't correct |