#include <bits/stdc++.h>
using namespace std;
int main(){
#define int long long
int n,m,inf=numeric_limits<int>::max(); cin >> n >> m; int sx,sy,gx,gy;
vector<string> A(n); for (auto& a:A) cin >> a;
queue<pair<int,int>> que; vector hd(n,vector<int>(n,inf));
for (int i(0);i < n;++i) for (int k(0);k < n;++k){
if (A[i][k]=='M') sx=i,sy=k;
if (A[i][k]=='D') gx=i,gy=k;
if (A[i][k]=='H') que.emplace(i,k),hd[i][k] = 0;
}
bool psf = true; auto can = [&psf,&A,&n](int x,int y){
if (x==-1||x==n||y==-1||y==n) return false;
if (A[x][y]=='T') return false;
if (psf&&A[x][y]=='D') return false;
return true;
};
while(!que.empty()){
auto [i,k] = que.front(); que.pop();
if (can(i+1,k)&&hd[i+1][k]==inf) hd[i+1][k]=hd[i][k]+m,que.emplace(i+1,k);
if (can(i-1,k)&&hd[i-1][k]==inf) hd[i-1][k]=hd[i][k]+m,que.emplace(i-1,k);
if (can(i,k+1)&&hd[i][k+1]==inf) hd[i][k+1]=hd[i][k]+m,que.emplace(i,k+1);
if (can(i,k-1)&&hd[i][k-1]==inf) hd[i][k-1]=hd[i][k]+m,que.emplace(i,k-1);
}
psf = false; int l = -1,r = 1e9,mid;
while(abs(l-r)>1){
mid = (l+r)/2;
vector dist(n,vector<int>(n,inf)); dist[sx][sy] = l,que.emplace(sx,sy);
while(!que.empty()){
auto [i,k] = que.front(); que.pop();
if (can(i+1,k)&&dist[i][k]+1<hd[i+1][k]&&dist[i+1][k]==inf) dist[i+1][k] = dist[i][k]+1,que.emplace(i+1,k);
if (can(i-1,k)&&dist[i][k]+1<hd[i-1][k]&&dist[i-1][k]==inf) dist[i-1][k] = dist[i][k]+1,que.emplace(i-1,k);
if (can(i,k+1)&&dist[i][k]+1<hd[i][k+1]&&dist[i][k+1]==inf) dist[i][k+1] = dist[i][k]+1,que.emplace(i,k+1);
if (can(i,k-1)&&dist[i][k]+1<hd[i][k-1]&&dist[i][k-1]==inf) dist[i][k-1] = dist[i][k]+1,que.emplace(i,k-1);
}
if (dist[gx][gy]==inf) r = mid;
else l = mid;
}
cout << l << endl;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:38:20: warning: 'gy' may be used uninitialized in this function [-Wmaybe-uninitialized]
38 | if (dist[gx][gy]==inf) r = mid;
| ^
mecho.cpp:38:16: warning: 'gx' may be used uninitialized in this function [-Wmaybe-uninitialized]
38 | if (dist[gx][gy]==inf) r = mid;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 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 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
69 ms |
11468 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
14 |
Correct |
2 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 |
1 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
1 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 |
348 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
448 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
12 ms |
2724 KB |
Output isn't correct |
34 |
Incorrect |
11 ms |
2664 KB |
Output isn't correct |
35 |
Incorrect |
13 ms |
2924 KB |
Output isn't correct |
36 |
Incorrect |
15 ms |
3288 KB |
Output isn't correct |
37 |
Incorrect |
18 ms |
3288 KB |
Output isn't correct |
38 |
Incorrect |
17 ms |
3360 KB |
Output isn't correct |
39 |
Incorrect |
18 ms |
4092 KB |
Output isn't correct |
40 |
Incorrect |
20 ms |
3996 KB |
Output isn't correct |
41 |
Incorrect |
21 ms |
4036 KB |
Output isn't correct |
42 |
Incorrect |
29 ms |
5128 KB |
Output isn't correct |
43 |
Incorrect |
26 ms |
5140 KB |
Output isn't correct |
44 |
Incorrect |
25 ms |
5128 KB |
Output isn't correct |
45 |
Incorrect |
28 ms |
6004 KB |
Output isn't correct |
46 |
Incorrect |
27 ms |
6012 KB |
Output isn't correct |
47 |
Incorrect |
34 ms |
6048 KB |
Output isn't correct |
48 |
Incorrect |
33 ms |
7140 KB |
Output isn't correct |
49 |
Incorrect |
33 ms |
7080 KB |
Output isn't correct |
50 |
Incorrect |
36 ms |
6988 KB |
Output isn't correct |
51 |
Incorrect |
39 ms |
8192 KB |
Output isn't correct |
52 |
Incorrect |
38 ms |
8060 KB |
Output isn't correct |
53 |
Incorrect |
44 ms |
8272 KB |
Output isn't correct |
54 |
Incorrect |
45 ms |
9180 KB |
Output isn't correct |
55 |
Incorrect |
45 ms |
9320 KB |
Output isn't correct |
56 |
Incorrect |
51 ms |
9304 KB |
Output isn't correct |
57 |
Incorrect |
51 ms |
10504 KB |
Output isn't correct |
58 |
Incorrect |
51 ms |
10516 KB |
Output isn't correct |
59 |
Incorrect |
57 ms |
10560 KB |
Output isn't correct |
60 |
Incorrect |
62 ms |
11888 KB |
Output isn't correct |
61 |
Incorrect |
61 ms |
11892 KB |
Output isn't correct |
62 |
Incorrect |
66 ms |
11940 KB |
Output isn't correct |
63 |
Incorrect |
72 ms |
11928 KB |
Output isn't correct |
64 |
Incorrect |
77 ms |
11844 KB |
Output isn't correct |
65 |
Incorrect |
75 ms |
11840 KB |
Output isn't correct |
66 |
Incorrect |
72 ms |
11852 KB |
Output isn't correct |
67 |
Correct |
300 ms |
11724 KB |
Output is correct |
68 |
Incorrect |
69 ms |
11956 KB |
Output isn't correct |
69 |
Incorrect |
69 ms |
12176 KB |
Output isn't correct |
70 |
Incorrect |
75 ms |
11888 KB |
Output isn't correct |
71 |
Incorrect |
71 ms |
11932 KB |
Output isn't correct |
72 |
Incorrect |
69 ms |
12008 KB |
Output isn't correct |
73 |
Incorrect |
64 ms |
11984 KB |
Output isn't correct |
74 |
Incorrect |
73 ms |
12156 KB |
Output isn't correct |
75 |
Incorrect |
63 ms |
11972 KB |
Output isn't correct |
76 |
Incorrect |
64 ms |
11852 KB |
Output isn't correct |
77 |
Incorrect |
65 ms |
11988 KB |
Output isn't correct |
78 |
Correct |
211 ms |
11808 KB |
Output is correct |
79 |
Incorrect |
71 ms |
11856 KB |
Output isn't correct |
80 |
Incorrect |
65 ms |
11832 KB |
Output isn't correct |
81 |
Incorrect |
63 ms |
11828 KB |
Output isn't correct |
82 |
Incorrect |
63 ms |
11924 KB |
Output isn't correct |
83 |
Incorrect |
68 ms |
11920 KB |
Output isn't correct |
84 |
Incorrect |
71 ms |
11744 KB |
Output isn't correct |
85 |
Incorrect |
66 ms |
11872 KB |
Output isn't correct |
86 |
Incorrect |
65 ms |
11876 KB |
Output isn't correct |
87 |
Incorrect |
67 ms |
11924 KB |
Output isn't correct |
88 |
Incorrect |
66 ms |
12060 KB |
Output isn't correct |
89 |
Incorrect |
76 ms |
11920 KB |
Output isn't correct |
90 |
Incorrect |
67 ms |
11936 KB |
Output isn't correct |
91 |
Incorrect |
70 ms |
11816 KB |
Output isn't correct |
92 |
Incorrect |
66 ms |
11956 KB |
Output isn't correct |