#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>> q;
vector<vector<int>> bdist(n + 1,vector<int>(n + 1,1e9));
int si = -1,sj = -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;
q.push({i,j});
}
if(a[i][j] == 'M'){
si = i;
sj = j;
}
}
}
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(bdist[ii][jj] != 1e9 || a[ii][jj] == 'T' || a[ii][jj] == 'D') continue;
bdist[ii][jj] = bdist[ni][nj] + 1;
q.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 l = 0,r = bdist[si][sj];
while(l < r){
int mid = (l + r + 1) >> 1;
//cout << mid << '\n';
vector<vector<int>> dist(n + 1,vector<int>(n + 1,-1));
q.push({si,sj});
dist[si][sj] = mid * s;
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] != -1) continue;
if(bdist[ii][jj] < (dist[ni][nj] + s) / s) continue;
if(a[ii][jj] == 'T') 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;
}
else r = mid - 1;
}
cout << l;
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout << bdist[i][j] << ' ';
}
cout << '\n';
}
*/
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
87 ms |
6828 KB |
Output is correct |
8 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Incorrect |
8 ms |
1716 KB |
Output isn't correct |
34 |
Correct |
12 ms |
1700 KB |
Output is correct |
35 |
Correct |
28 ms |
1876 KB |
Output is correct |
36 |
Incorrect |
13 ms |
2120 KB |
Output isn't correct |
37 |
Correct |
12 ms |
2108 KB |
Output is correct |
38 |
Correct |
31 ms |
2088 KB |
Output is correct |
39 |
Incorrect |
15 ms |
2500 KB |
Output isn't correct |
40 |
Correct |
16 ms |
2516 KB |
Output is correct |
41 |
Correct |
39 ms |
2468 KB |
Output is correct |
42 |
Incorrect |
18 ms |
2980 KB |
Output isn't correct |
43 |
Correct |
19 ms |
2952 KB |
Output is correct |
44 |
Correct |
56 ms |
2984 KB |
Output is correct |
45 |
Incorrect |
23 ms |
3488 KB |
Output isn't correct |
46 |
Correct |
22 ms |
3536 KB |
Output is correct |
47 |
Correct |
56 ms |
3500 KB |
Output is correct |
48 |
Incorrect |
25 ms |
4036 KB |
Output isn't correct |
49 |
Correct |
36 ms |
4108 KB |
Output is correct |
50 |
Correct |
64 ms |
4084 KB |
Output is correct |
51 |
Incorrect |
30 ms |
4736 KB |
Output isn't correct |
52 |
Correct |
34 ms |
4864 KB |
Output is correct |
53 |
Correct |
87 ms |
4704 KB |
Output is correct |
54 |
Incorrect |
33 ms |
5380 KB |
Output isn't correct |
55 |
Correct |
35 ms |
5544 KB |
Output is correct |
56 |
Correct |
101 ms |
5336 KB |
Output is correct |
57 |
Incorrect |
44 ms |
6132 KB |
Output isn't correct |
58 |
Correct |
52 ms |
6092 KB |
Output is correct |
59 |
Correct |
121 ms |
6088 KB |
Output is correct |
60 |
Incorrect |
45 ms |
6868 KB |
Output isn't correct |
61 |
Correct |
48 ms |
6860 KB |
Output is correct |
62 |
Correct |
135 ms |
6820 KB |
Output is correct |
63 |
Correct |
97 ms |
6828 KB |
Output is correct |
64 |
Correct |
152 ms |
6824 KB |
Output is correct |
65 |
Correct |
145 ms |
6840 KB |
Output is correct |
66 |
Incorrect |
119 ms |
6812 KB |
Output isn't correct |
67 |
Incorrect |
111 ms |
6832 KB |
Output isn't correct |
68 |
Correct |
50 ms |
6852 KB |
Output is correct |
69 |
Correct |
42 ms |
6860 KB |
Output is correct |
70 |
Correct |
44 ms |
6864 KB |
Output is correct |
71 |
Correct |
42 ms |
6824 KB |
Output is correct |
72 |
Incorrect |
39 ms |
6852 KB |
Output isn't correct |
73 |
Incorrect |
40 ms |
6800 KB |
Output isn't correct |
74 |
Correct |
66 ms |
6876 KB |
Output is correct |
75 |
Correct |
67 ms |
6884 KB |
Output is correct |
76 |
Correct |
69 ms |
6852 KB |
Output is correct |
77 |
Correct |
67 ms |
6856 KB |
Output is correct |
78 |
Incorrect |
76 ms |
6836 KB |
Output isn't correct |
79 |
Correct |
59 ms |
6844 KB |
Output is correct |
80 |
Correct |
59 ms |
6884 KB |
Output is correct |
81 |
Correct |
78 ms |
6868 KB |
Output is correct |
82 |
Correct |
73 ms |
6848 KB |
Output is correct |
83 |
Correct |
82 ms |
6836 KB |
Output is correct |
84 |
Correct |
105 ms |
6844 KB |
Output is correct |
85 |
Correct |
79 ms |
6808 KB |
Output is correct |
86 |
Correct |
86 ms |
6876 KB |
Output is correct |
87 |
Correct |
99 ms |
6860 KB |
Output is correct |
88 |
Correct |
85 ms |
6864 KB |
Output is correct |
89 |
Correct |
90 ms |
6752 KB |
Output is correct |
90 |
Correct |
120 ms |
6852 KB |
Output is correct |
91 |
Correct |
120 ms |
6840 KB |
Output is correct |
92 |
Correct |
99 ms |
6856 KB |
Output is correct |