#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long int
#define pi pair<int,int>
#define sz(x) (int)x.size()
#define all(x) begin(x),end(x)
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n,s;cin >> n >> s;
vector<vector<char>> a(n + 1,vector<char>(n + 1));
queue<pair<int,int>> qu;
vector<vector<int>> bdist(n + 1,vector<int>(n + 1,1e9));
int si = -1,sj = -1,ei = -1,ej = -1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin >> a[i][j];
if(a[i][j] == 'H'){
bdist[i][j] = 0;
qu.push({i,j});
}
if(a[i][j] == 'M'){
si = i;
sj = j;
}
if(a[i][j] == 'D'){
ei = i;
ej = j;
}
}
}
while(sz(qu)){
int ni = qu.front().first,nj = qu.front().second;
qu.pop();
for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++){
if(abs(i) == abs(j)) continue;
int ii = ni + i,jj = nj + j;
if(ii < 1 || ii > n || jj < 1 || jj > n) continue;
if(bdist[ii][jj] != 1e9 || a[ii][jj] == 'T' || a[ii][jj] == 'D') continue;
bdist[ii][jj] = bdist[ni][nj] + 1;
qu.push({ii,jj});
}
}
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout << (bdist[i][j] == 1e9 ? -1 : bdist[i][j]) << ' ';
}
cout << '\n';
}
cout << "---------------------------------------------\n";
*/
int ans = -1;
int l = 0,r = n * n;
while(l <= r){
int mid = (l + r) >> 1;
//cout << mid << '\n';
vector<vector<int>> dist(n + 1,vector<int>(n + 1,1e9));
queue<pair<int,int>> q;
q.push({si,sj});
dist[si][sj] = 0;
bool ok = false;
while(sz(q)){
int ni = q.front().first,nj = q.front().second;
q.pop();
for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++){
if(abs(i) == abs(j)) continue;
int ii = ni + i,jj = nj + j;
if(ii < 1 || ii > n || jj < 1 || jj > n) continue;
if(dist[ii][jj] != 1e9) continue;
if(bdist[ii][jj] <= mid + (dist[ni][nj] + 1) / s) continue;
if(a[ii][jj] == 'T' || a[ii][jj] == 'H') continue;
//cout << ni << ' ' << nj << ' ' << (dist[ni][nj] + s - 1) / s << '\n';
dist[ii][jj] = dist[ni][nj] + 1;
q.push({ii,jj});
if(a[ii][jj] == 'D') ok = true;
}
}
/*
for(int i=1;i<=n;i++) {
for(int j=1;j<=n;j++) {
cout << dist[i][j] << ' ';
}
cout << '\n';
}
*/
if(ok){
l = mid + 1;
ans = mid;
}
else r = mid - 1;
}
cout << ans;
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout << bdist[i][j] << ' ';
}
cout << '\n';
}
*/
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:17:25: warning: variable 'ei' set but not used [-Wunused-but-set-variable]
17 | int si = -1,sj = -1,ei = -1,ej = -1;
| ^~
mecho.cpp:17:33: warning: variable 'ej' set but not used [-Wunused-but-set-variable]
17 | int si = -1,sj = -1,ei = -1,ej = -1;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
106 ms |
6244 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
508 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
8 ms |
1572 KB |
Output is correct |
34 |
Correct |
9 ms |
1596 KB |
Output is correct |
35 |
Correct |
24 ms |
1584 KB |
Output is correct |
36 |
Correct |
10 ms |
1872 KB |
Output is correct |
37 |
Correct |
13 ms |
1872 KB |
Output is correct |
38 |
Correct |
31 ms |
1872 KB |
Output is correct |
39 |
Correct |
15 ms |
2304 KB |
Output is correct |
40 |
Correct |
15 ms |
2304 KB |
Output is correct |
41 |
Correct |
43 ms |
2352 KB |
Output is correct |
42 |
Correct |
17 ms |
2728 KB |
Output is correct |
43 |
Correct |
19 ms |
2728 KB |
Output is correct |
44 |
Correct |
54 ms |
2728 KB |
Output is correct |
45 |
Correct |
21 ms |
3216 KB |
Output is correct |
46 |
Correct |
21 ms |
3216 KB |
Output is correct |
47 |
Correct |
61 ms |
3436 KB |
Output is correct |
48 |
Correct |
25 ms |
3700 KB |
Output is correct |
49 |
Correct |
27 ms |
3924 KB |
Output is correct |
50 |
Correct |
73 ms |
3724 KB |
Output is correct |
51 |
Correct |
32 ms |
4328 KB |
Output is correct |
52 |
Correct |
33 ms |
4344 KB |
Output is correct |
53 |
Correct |
91 ms |
4292 KB |
Output is correct |
54 |
Correct |
32 ms |
4944 KB |
Output is correct |
55 |
Correct |
34 ms |
4892 KB |
Output is correct |
56 |
Correct |
106 ms |
4888 KB |
Output is correct |
57 |
Correct |
38 ms |
5584 KB |
Output is correct |
58 |
Correct |
40 ms |
5532 KB |
Output is correct |
59 |
Correct |
123 ms |
5564 KB |
Output is correct |
60 |
Correct |
43 ms |
6248 KB |
Output is correct |
61 |
Correct |
46 ms |
6240 KB |
Output is correct |
62 |
Correct |
138 ms |
6216 KB |
Output is correct |
63 |
Correct |
118 ms |
6204 KB |
Output is correct |
64 |
Correct |
194 ms |
6200 KB |
Output is correct |
65 |
Correct |
151 ms |
6196 KB |
Output is correct |
66 |
Correct |
128 ms |
6204 KB |
Output is correct |
67 |
Correct |
132 ms |
6196 KB |
Output is correct |
68 |
Correct |
65 ms |
6240 KB |
Output is correct |
69 |
Correct |
70 ms |
6248 KB |
Output is correct |
70 |
Correct |
57 ms |
6224 KB |
Output is correct |
71 |
Correct |
63 ms |
6304 KB |
Output is correct |
72 |
Incorrect |
51 ms |
6256 KB |
Output isn't correct |
73 |
Incorrect |
61 ms |
6280 KB |
Output isn't correct |
74 |
Correct |
81 ms |
6240 KB |
Output is correct |
75 |
Correct |
90 ms |
6252 KB |
Output is correct |
76 |
Correct |
80 ms |
6280 KB |
Output is correct |
77 |
Correct |
78 ms |
6228 KB |
Output is correct |
78 |
Correct |
90 ms |
6256 KB |
Output is correct |
79 |
Correct |
78 ms |
6288 KB |
Output is correct |
80 |
Correct |
78 ms |
6268 KB |
Output is correct |
81 |
Correct |
85 ms |
6292 KB |
Output is correct |
82 |
Correct |
79 ms |
6252 KB |
Output is correct |
83 |
Correct |
96 ms |
6252 KB |
Output is correct |
84 |
Correct |
89 ms |
6252 KB |
Output is correct |
85 |
Correct |
103 ms |
6268 KB |
Output is correct |
86 |
Correct |
92 ms |
6260 KB |
Output is correct |
87 |
Correct |
94 ms |
6256 KB |
Output is correct |
88 |
Correct |
103 ms |
6252 KB |
Output is correct |
89 |
Correct |
99 ms |
6280 KB |
Output is correct |
90 |
Correct |
118 ms |
6224 KB |
Output is correct |
91 |
Correct |
99 ms |
6216 KB |
Output is correct |
92 |
Correct |
106 ms |
6252 KB |
Output is correct |