#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 = hd[sx][sy]/m,mid;
while(abs(l-r)>1){
mid = (l+r)/2;
vector dist(n,vector<int>(n,inf)); dist[sx][sy] = mid*m,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 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
81 ms |
11356 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
360 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
8 ms |
2540 KB |
Output is correct |
34 |
Correct |
8 ms |
2552 KB |
Output is correct |
35 |
Correct |
16 ms |
2528 KB |
Output is correct |
36 |
Correct |
11 ms |
3216 KB |
Output is correct |
37 |
Correct |
11 ms |
3168 KB |
Output is correct |
38 |
Correct |
27 ms |
3188 KB |
Output is correct |
39 |
Correct |
14 ms |
3864 KB |
Output is correct |
40 |
Correct |
13 ms |
3896 KB |
Output is correct |
41 |
Correct |
29 ms |
3856 KB |
Output is correct |
42 |
Correct |
18 ms |
4940 KB |
Output is correct |
43 |
Correct |
18 ms |
4908 KB |
Output is correct |
44 |
Correct |
36 ms |
4892 KB |
Output is correct |
45 |
Correct |
21 ms |
5784 KB |
Output is correct |
46 |
Correct |
21 ms |
5840 KB |
Output is correct |
47 |
Correct |
44 ms |
5764 KB |
Output is correct |
48 |
Correct |
26 ms |
6748 KB |
Output is correct |
49 |
Correct |
24 ms |
6712 KB |
Output is correct |
50 |
Correct |
54 ms |
6724 KB |
Output is correct |
51 |
Correct |
33 ms |
7824 KB |
Output is correct |
52 |
Correct |
28 ms |
7848 KB |
Output is correct |
53 |
Correct |
86 ms |
7744 KB |
Output is correct |
54 |
Correct |
38 ms |
8976 KB |
Output is correct |
55 |
Correct |
33 ms |
8908 KB |
Output is correct |
56 |
Correct |
74 ms |
8848 KB |
Output is correct |
57 |
Correct |
51 ms |
10108 KB |
Output is correct |
58 |
Correct |
40 ms |
10124 KB |
Output is correct |
59 |
Correct |
98 ms |
10008 KB |
Output is correct |
60 |
Correct |
47 ms |
11420 KB |
Output is correct |
61 |
Correct |
44 ms |
11400 KB |
Output is correct |
62 |
Correct |
115 ms |
11300 KB |
Output is correct |
63 |
Correct |
100 ms |
11312 KB |
Output is correct |
64 |
Correct |
155 ms |
11312 KB |
Output is correct |
65 |
Correct |
151 ms |
11264 KB |
Output is correct |
66 |
Correct |
112 ms |
11312 KB |
Output is correct |
67 |
Correct |
106 ms |
11336 KB |
Output is correct |
68 |
Correct |
52 ms |
11316 KB |
Output is correct |
69 |
Correct |
55 ms |
11524 KB |
Output is correct |
70 |
Correct |
49 ms |
11332 KB |
Output is correct |
71 |
Correct |
40 ms |
11328 KB |
Output is correct |
72 |
Correct |
48 ms |
11404 KB |
Output is correct |
73 |
Correct |
41 ms |
11356 KB |
Output is correct |
74 |
Correct |
63 ms |
11324 KB |
Output is correct |
75 |
Correct |
61 ms |
11356 KB |
Output is correct |
76 |
Correct |
61 ms |
11376 KB |
Output is correct |
77 |
Correct |
63 ms |
11372 KB |
Output is correct |
78 |
Correct |
65 ms |
11348 KB |
Output is correct |
79 |
Correct |
60 ms |
11304 KB |
Output is correct |
80 |
Correct |
61 ms |
11352 KB |
Output is correct |
81 |
Correct |
62 ms |
11324 KB |
Output is correct |
82 |
Correct |
59 ms |
11364 KB |
Output is correct |
83 |
Correct |
93 ms |
11364 KB |
Output is correct |
84 |
Correct |
70 ms |
11356 KB |
Output is correct |
85 |
Correct |
64 ms |
11352 KB |
Output is correct |
86 |
Correct |
75 ms |
11320 KB |
Output is correct |
87 |
Correct |
73 ms |
11356 KB |
Output is correct |
88 |
Correct |
79 ms |
11336 KB |
Output is correct |
89 |
Correct |
77 ms |
11304 KB |
Output is correct |
90 |
Correct |
81 ms |
11336 KB |
Output is correct |
91 |
Correct |
82 ms |
11312 KB |
Output is correct |
92 |
Correct |
82 ms |
11360 KB |
Output is correct |