#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int n, sx, sy, fx, fy, dB[810][810], d[810][810], S;
char a[810][810];
bool f(int bound){
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++)
d[i][j] = 1e9;
}
d[sx][sy] = 0;
queue <pair<int, int> > q;
q.push({sx, sy});
while(!q.empty()){
int x = q.front().first, y = q.front().second;
q.pop();
int curTime = bound + max(0, (d[x][y]))/S;
if (curTime >= dB[x][y])
continue;
for (int i = 0; i < 4; i++){
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || yy < 1 || xx > n || yy > n || a[xx][yy] == 'T' || curTime >= dB[xx][yy])
continue;
if (d[x][y] + 1 < d[xx][yy]){
d[xx][yy] = d[x][y] + 1;
q.push({xx, yy});
}
}
}
return d[fx][fy] != 1e9;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> S;
queue <pair<int, int> > q;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
cin >> a[i][j];
dB[i][j] = 1e9;
if (a[i][j] == 'H'){
q.push({i, j});
dB[i][j] = 0;
}
if (a[i][j] == 'M'){
sx = i; sy = j;
}
if (a[i][j] == 'D'){
fx = i; fy = j;
}
}
}
while(!q.empty()){
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++){
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || yy < 1 || xx > n || yy > n || a[xx][yy] == 'T' || a[xx][yy] == 'D')
continue;
if (dB[x][y] + 1 < dB[xx][yy]){
dB[xx][yy] = dB[x][y] + 1;
q.push({xx, yy});
}
}
}
int l = 0, r = 2000000;
while(l < r){
int mid = (l+r+1)>>1;
if (f(mid))
l = mid; else
r = mid-1;
}
if (f(l) == 0)
l = -1;
cout << l;
}
/**
4 3
TTDT
THGT
TTGT
TTGM
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
452 KB |
Output is correct |
3 |
Correct |
4 ms |
552 KB |
Output is correct |
4 |
Correct |
2 ms |
552 KB |
Output is correct |
5 |
Correct |
4 ms |
636 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
202 ms |
7052 KB |
Output is correct |
8 |
Correct |
4 ms |
7052 KB |
Output is correct |
9 |
Correct |
3 ms |
7052 KB |
Output is correct |
10 |
Correct |
3 ms |
7052 KB |
Output is correct |
11 |
Correct |
3 ms |
7052 KB |
Output is correct |
12 |
Correct |
4 ms |
7052 KB |
Output is correct |
13 |
Correct |
4 ms |
7052 KB |
Output is correct |
14 |
Correct |
4 ms |
7052 KB |
Output is correct |
15 |
Correct |
3 ms |
7052 KB |
Output is correct |
16 |
Correct |
3 ms |
7052 KB |
Output is correct |
17 |
Correct |
4 ms |
7052 KB |
Output is correct |
18 |
Correct |
3 ms |
7052 KB |
Output is correct |
19 |
Correct |
3 ms |
7052 KB |
Output is correct |
20 |
Correct |
3 ms |
7052 KB |
Output is correct |
21 |
Correct |
3 ms |
7052 KB |
Output is correct |
22 |
Correct |
3 ms |
7052 KB |
Output is correct |
23 |
Correct |
3 ms |
7052 KB |
Output is correct |
24 |
Correct |
3 ms |
7052 KB |
Output is correct |
25 |
Correct |
3 ms |
7052 KB |
Output is correct |
26 |
Correct |
3 ms |
7052 KB |
Output is correct |
27 |
Correct |
2 ms |
7052 KB |
Output is correct |
28 |
Correct |
3 ms |
7052 KB |
Output is correct |
29 |
Correct |
3 ms |
7052 KB |
Output is correct |
30 |
Correct |
3 ms |
7052 KB |
Output is correct |
31 |
Correct |
4 ms |
7052 KB |
Output is correct |
32 |
Correct |
2 ms |
7052 KB |
Output is correct |
33 |
Correct |
13 ms |
7052 KB |
Output is correct |
34 |
Correct |
15 ms |
7052 KB |
Output is correct |
35 |
Correct |
50 ms |
7052 KB |
Output is correct |
36 |
Correct |
11 ms |
7052 KB |
Output is correct |
37 |
Correct |
12 ms |
7052 KB |
Output is correct |
38 |
Correct |
66 ms |
7052 KB |
Output is correct |
39 |
Correct |
17 ms |
7052 KB |
Output is correct |
40 |
Correct |
14 ms |
7052 KB |
Output is correct |
41 |
Correct |
84 ms |
7052 KB |
Output is correct |
42 |
Correct |
19 ms |
7052 KB |
Output is correct |
43 |
Correct |
22 ms |
7052 KB |
Output is correct |
44 |
Correct |
91 ms |
7052 KB |
Output is correct |
45 |
Correct |
17 ms |
7052 KB |
Output is correct |
46 |
Correct |
20 ms |
7052 KB |
Output is correct |
47 |
Correct |
134 ms |
7052 KB |
Output is correct |
48 |
Correct |
21 ms |
7052 KB |
Output is correct |
49 |
Correct |
24 ms |
7052 KB |
Output is correct |
50 |
Correct |
143 ms |
7052 KB |
Output is correct |
51 |
Correct |
25 ms |
7052 KB |
Output is correct |
52 |
Correct |
25 ms |
7052 KB |
Output is correct |
53 |
Correct |
190 ms |
7052 KB |
Output is correct |
54 |
Correct |
30 ms |
7052 KB |
Output is correct |
55 |
Correct |
30 ms |
7052 KB |
Output is correct |
56 |
Correct |
188 ms |
7052 KB |
Output is correct |
57 |
Correct |
33 ms |
7052 KB |
Output is correct |
58 |
Correct |
30 ms |
7052 KB |
Output is correct |
59 |
Correct |
223 ms |
7052 KB |
Output is correct |
60 |
Correct |
43 ms |
7068 KB |
Output is correct |
61 |
Correct |
35 ms |
7068 KB |
Output is correct |
62 |
Correct |
282 ms |
7068 KB |
Output is correct |
63 |
Correct |
150 ms |
7168 KB |
Output is correct |
64 |
Correct |
242 ms |
7196 KB |
Output is correct |
65 |
Correct |
262 ms |
7196 KB |
Output is correct |
66 |
Correct |
243 ms |
7196 KB |
Output is correct |
67 |
Correct |
162 ms |
7196 KB |
Output is correct |
68 |
Correct |
71 ms |
7196 KB |
Output is correct |
69 |
Correct |
68 ms |
7196 KB |
Output is correct |
70 |
Correct |
60 ms |
7196 KB |
Output is correct |
71 |
Correct |
66 ms |
7196 KB |
Output is correct |
72 |
Correct |
45 ms |
7196 KB |
Output is correct |
73 |
Correct |
66 ms |
7324 KB |
Output is correct |
74 |
Correct |
109 ms |
7452 KB |
Output is correct |
75 |
Correct |
139 ms |
7452 KB |
Output is correct |
76 |
Correct |
134 ms |
7452 KB |
Output is correct |
77 |
Correct |
137 ms |
7452 KB |
Output is correct |
78 |
Correct |
115 ms |
7452 KB |
Output is correct |
79 |
Correct |
100 ms |
7452 KB |
Output is correct |
80 |
Correct |
98 ms |
7452 KB |
Output is correct |
81 |
Correct |
112 ms |
7452 KB |
Output is correct |
82 |
Correct |
111 ms |
7452 KB |
Output is correct |
83 |
Correct |
160 ms |
7452 KB |
Output is correct |
84 |
Correct |
120 ms |
7452 KB |
Output is correct |
85 |
Correct |
130 ms |
7452 KB |
Output is correct |
86 |
Correct |
150 ms |
7452 KB |
Output is correct |
87 |
Correct |
153 ms |
7452 KB |
Output is correct |
88 |
Correct |
154 ms |
7452 KB |
Output is correct |
89 |
Correct |
170 ms |
7452 KB |
Output is correct |
90 |
Correct |
206 ms |
7452 KB |
Output is correct |
91 |
Correct |
234 ms |
7452 KB |
Output is correct |
92 |
Correct |
155 ms |
7452 KB |
Output is correct |