#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*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;
if(arr[newx][newy]=='G'||arr[newx][newy]=='M'){
dist[newx][newy]=dist[cur.first][cur.second]+1;
q.push({newx,newy});
}
}
}
while(l<=r){
mid=(l+r)/2;
int dist2[n][n];
memset(dist2,-1,sizeof(dist2));
dist2[start.first][start.second]=k*mid;
if(mid<dist[start.first][start.second])q.push({start.first,start.second});
bool visited[n+5][n+5];
memset(visited,0,sizeof(visited));
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;
if(dist[newx][newy]<(dist2[cur.first][cur.second]+k)/k) continue;
if(arr[newx][newy]=='M'||arr[newx][newy]=='G'){
dist2[newx][newy]=dist2[cur.first][cur.second]+1;
q.push({newx,newy});
}
}
}
bool amos=false;
for(auto it:dir){
int newx=done.first+it.first;
int newy=done.second+it.second;
if(newx<0||newy<0||newx>=n||newy>=n) continue;
if(arr[newx][newy]=='G'||arr[newx][newy]=='M'){
if(dist2[newx][newy]!=-1) amos=true;
}
}
if(amos){
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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
61 ms |
11868 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Incorrect |
4 ms |
2652 KB |
Output isn't correct |
34 |
Correct |
4 ms |
2652 KB |
Output is correct |
35 |
Correct |
15 ms |
2652 KB |
Output is correct |
36 |
Incorrect |
6 ms |
3164 KB |
Output isn't correct |
37 |
Correct |
5 ms |
3324 KB |
Output is correct |
38 |
Correct |
24 ms |
3336 KB |
Output is correct |
39 |
Incorrect |
6 ms |
3932 KB |
Output isn't correct |
40 |
Correct |
7 ms |
3932 KB |
Output is correct |
41 |
Correct |
28 ms |
4076 KB |
Output is correct |
42 |
Incorrect |
7 ms |
4820 KB |
Output isn't correct |
43 |
Correct |
8 ms |
4700 KB |
Output is correct |
44 |
Correct |
32 ms |
4944 KB |
Output is correct |
45 |
Incorrect |
8 ms |
5724 KB |
Output isn't correct |
46 |
Correct |
10 ms |
5828 KB |
Output is correct |
47 |
Correct |
39 ms |
5844 KB |
Output is correct |
48 |
Incorrect |
11 ms |
6744 KB |
Output isn't correct |
49 |
Correct |
14 ms |
6744 KB |
Output is correct |
50 |
Correct |
45 ms |
6748 KB |
Output is correct |
51 |
Incorrect |
12 ms |
7768 KB |
Output isn't correct |
52 |
Correct |
14 ms |
7856 KB |
Output is correct |
53 |
Correct |
59 ms |
7864 KB |
Output is correct |
54 |
Incorrect |
13 ms |
9052 KB |
Output isn't correct |
55 |
Correct |
18 ms |
9204 KB |
Output is correct |
56 |
Correct |
100 ms |
9188 KB |
Output is correct |
57 |
Incorrect |
16 ms |
10328 KB |
Output isn't correct |
58 |
Correct |
19 ms |
10332 KB |
Output is correct |
59 |
Correct |
83 ms |
10484 KB |
Output is correct |
60 |
Incorrect |
18 ms |
11612 KB |
Output isn't correct |
61 |
Correct |
24 ms |
11612 KB |
Output is correct |
62 |
Correct |
100 ms |
11868 KB |
Output is correct |
63 |
Correct |
96 ms |
11864 KB |
Output is correct |
64 |
Correct |
172 ms |
11864 KB |
Output is correct |
65 |
Correct |
135 ms |
11844 KB |
Output is correct |
66 |
Incorrect |
120 ms |
11868 KB |
Output isn't correct |
67 |
Correct |
109 ms |
11864 KB |
Output is correct |
68 |
Correct |
41 ms |
12156 KB |
Output is correct |
69 |
Correct |
39 ms |
11864 KB |
Output is correct |
70 |
Correct |
31 ms |
11856 KB |
Output is correct |
71 |
Correct |
34 ms |
11868 KB |
Output is correct |
72 |
Correct |
25 ms |
11868 KB |
Output is correct |
73 |
Correct |
29 ms |
12628 KB |
Output is correct |
74 |
Correct |
41 ms |
12352 KB |
Output is correct |
75 |
Correct |
50 ms |
12392 KB |
Output is correct |
76 |
Correct |
45 ms |
12384 KB |
Output is correct |
77 |
Correct |
46 ms |
12364 KB |
Output is correct |
78 |
Correct |
66 ms |
12336 KB |
Output is correct |
79 |
Correct |
35 ms |
12124 KB |
Output is correct |
80 |
Correct |
42 ms |
12336 KB |
Output is correct |
81 |
Correct |
54 ms |
12364 KB |
Output is correct |
82 |
Correct |
42 ms |
12120 KB |
Output is correct |
83 |
Correct |
53 ms |
12120 KB |
Output is correct |
84 |
Correct |
47 ms |
12124 KB |
Output is correct |
85 |
Correct |
54 ms |
12260 KB |
Output is correct |
86 |
Correct |
57 ms |
12124 KB |
Output is correct |
87 |
Correct |
67 ms |
12368 KB |
Output is correct |
88 |
Correct |
72 ms |
12112 KB |
Output is correct |
89 |
Correct |
54 ms |
12124 KB |
Output is correct |
90 |
Correct |
56 ms |
12120 KB |
Output is correct |
91 |
Correct |
58 ms |
12124 KB |
Output is correct |
92 |
Correct |
59 ms |
12124 KB |
Output is correct |