#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, s, disH[1010][1010], dis[1010][1010], xx[] = {0, 0, -1, 1}, xy[] = {-1, 1, 0, 0};
char mp[1010][1010];
queue<tuple<int, int, int>> q;
bool solve(int mid){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(mp[i][j] == 'M') q.emplace(i, j, mid*s), dis[i][j] = mid*s;
else dis[i][j] = 1e18;
}
}
while(!q.empty()){
int x = get<0>(q.front()), y = get<1>(q.front()), w = get<2>(q.front()); q.pop();
if(dis[x][y] < w || dis[x][y] >= disH[x][y]) continue;
if(mp[x][y] == 'D') return true;
for(int i = 0; i < 4; ++i){
int nx = x + xx[i];
int ny = y + xy[i];
if(nx < 1ll || nx > n || ny < 1ll || ny > n || mp[nx][ny] == 'T') continue;
if(dis[nx][ny] > w + 1ll){
dis[nx][ny] = w + 1ll;
q.emplace(nx, ny, w + 1ll);
}
}
}
return false;
}
int32_t main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> s;
for(int i = 1; i<=n; ++i) for(int j = 1; j <= n; ++j) cin >> mp[i][j];
for(int i = 1; i <= n; ++i){
for(int j = 1; j <=n; ++j){
if(mp[i][j] == 'H') q.emplace(i, j, 0), disH[i][j] = 0;
else disH[i][j] = 1e18;
}
}
while(!q.empty()){
int x = get<0>(q.front()), y = get<1>(q.front()), w = get<2>(q.front()); q.pop();
if(disH[x][y] < w) continue;
for(int i = 0; i < 4; ++i){
int nx = x + xx[i];
int ny = y + xy[i];
if(nx < 1ll || nx > n || ny < 1ll || ny > n || mp[nx][ny] == 'T') continue;
if(disH[nx][ny] > w + 1ll){
disH[nx][ny] = w + 1ll;
q.emplace(nx, ny, w + 1ll);
}
}
}
for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) disH[i][j] == 1e18 ? disH[i][j] *= 1ll : disH[i][j] *= s;
int l = 0, r = 1e18, ans = -1ll;
//for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) cout << i << ' ' << j << ' ' << disH[i][j] << '\n';
while(l <= r){
int mid = (l + r) >> 1ll;
if(solve(mid)) ans = mid, l = mid + 1ll;
else r = mid - 1ll;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
3 |
Correct |
1 ms |
4600 KB |
Output is correct |
4 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
7 |
Incorrect |
320 ms |
17348 KB |
Output isn't correct |
8 |
Correct |
1 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
4432 KB |
Output is correct |
11 |
Correct |
1 ms |
4432 KB |
Output is correct |
12 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
13 |
Correct |
1 ms |
4432 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4432 KB |
Output is correct |
16 |
Correct |
1 ms |
4432 KB |
Output is correct |
17 |
Correct |
1 ms |
4432 KB |
Output is correct |
18 |
Correct |
1 ms |
4432 KB |
Output is correct |
19 |
Correct |
1 ms |
4600 KB |
Output is correct |
20 |
Correct |
1 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
4432 KB |
Output is correct |
22 |
Incorrect |
1 ms |
4600 KB |
Output isn't correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
25 |
Correct |
1 ms |
4432 KB |
Output is correct |
26 |
Incorrect |
1 ms |
4432 KB |
Output isn't correct |
27 |
Correct |
2 ms |
4648 KB |
Output is correct |
28 |
Incorrect |
2 ms |
4604 KB |
Output isn't correct |
29 |
Correct |
2 ms |
4432 KB |
Output is correct |
30 |
Incorrect |
2 ms |
4432 KB |
Output isn't correct |
31 |
Correct |
2 ms |
4432 KB |
Output is correct |
32 |
Incorrect |
2 ms |
4432 KB |
Output isn't correct |
33 |
Correct |
9 ms |
8956 KB |
Output is correct |
34 |
Incorrect |
12 ms |
8852 KB |
Output isn't correct |
35 |
Incorrect |
34 ms |
8784 KB |
Output isn't correct |
36 |
Correct |
11 ms |
8784 KB |
Output is correct |
37 |
Incorrect |
11 ms |
8784 KB |
Output isn't correct |
38 |
Incorrect |
44 ms |
8784 KB |
Output isn't correct |
39 |
Correct |
18 ms |
12880 KB |
Output is correct |
40 |
Incorrect |
17 ms |
12880 KB |
Output isn't correct |
41 |
Incorrect |
77 ms |
12880 KB |
Output isn't correct |
42 |
Correct |
17 ms |
12880 KB |
Output is correct |
43 |
Incorrect |
17 ms |
12952 KB |
Output isn't correct |
44 |
Incorrect |
85 ms |
12880 KB |
Output isn't correct |
45 |
Correct |
19 ms |
12880 KB |
Output is correct |
46 |
Incorrect |
20 ms |
13048 KB |
Output isn't correct |
47 |
Incorrect |
101 ms |
12880 KB |
Output isn't correct |
48 |
Correct |
26 ms |
12880 KB |
Output is correct |
49 |
Incorrect |
23 ms |
12880 KB |
Output isn't correct |
50 |
Incorrect |
109 ms |
12880 KB |
Output isn't correct |
51 |
Correct |
26 ms |
12880 KB |
Output is correct |
52 |
Incorrect |
27 ms |
13048 KB |
Output isn't correct |
53 |
Incorrect |
126 ms |
12880 KB |
Output isn't correct |
54 |
Correct |
31 ms |
15096 KB |
Output is correct |
55 |
Incorrect |
31 ms |
14928 KB |
Output isn't correct |
56 |
Incorrect |
161 ms |
14928 KB |
Output isn't correct |
57 |
Correct |
35 ms |
16976 KB |
Output is correct |
58 |
Incorrect |
36 ms |
16976 KB |
Output isn't correct |
59 |
Incorrect |
180 ms |
17032 KB |
Output isn't correct |
60 |
Correct |
39 ms |
17052 KB |
Output is correct |
61 |
Incorrect |
48 ms |
17224 KB |
Output isn't correct |
62 |
Incorrect |
202 ms |
17224 KB |
Output isn't correct |
63 |
Correct |
107 ms |
16976 KB |
Output is correct |
64 |
Incorrect |
478 ms |
16976 KB |
Output isn't correct |
65 |
Correct |
137 ms |
16976 KB |
Output is correct |
66 |
Correct |
121 ms |
16968 KB |
Output is correct |
67 |
Correct |
100 ms |
16976 KB |
Output is correct |
68 |
Incorrect |
403 ms |
16976 KB |
Output isn't correct |
69 |
Incorrect |
318 ms |
17128 KB |
Output isn't correct |
70 |
Incorrect |
442 ms |
16968 KB |
Output isn't correct |
71 |
Correct |
60 ms |
16968 KB |
Output is correct |
72 |
Incorrect |
63 ms |
16976 KB |
Output isn't correct |
73 |
Correct |
55 ms |
17744 KB |
Output is correct |
74 |
Incorrect |
245 ms |
18048 KB |
Output isn't correct |
75 |
Correct |
69 ms |
17880 KB |
Output is correct |
76 |
Correct |
68 ms |
17864 KB |
Output is correct |
77 |
Incorrect |
254 ms |
17744 KB |
Output isn't correct |
78 |
Correct |
72 ms |
17744 KB |
Output is correct |
79 |
Incorrect |
269 ms |
17796 KB |
Output isn't correct |
80 |
Correct |
65 ms |
17744 KB |
Output is correct |
81 |
Correct |
86 ms |
17736 KB |
Output is correct |
82 |
Incorrect |
228 ms |
17744 KB |
Output isn't correct |
83 |
Correct |
76 ms |
17372 KB |
Output is correct |
84 |
Incorrect |
253 ms |
17668 KB |
Output isn't correct |
85 |
Incorrect |
249 ms |
17488 KB |
Output isn't correct |
86 |
Correct |
78 ms |
17488 KB |
Output is correct |
87 |
Incorrect |
274 ms |
17372 KB |
Output isn't correct |
88 |
Incorrect |
269 ms |
17488 KB |
Output isn't correct |
89 |
Incorrect |
275 ms |
17488 KB |
Output isn't correct |
90 |
Incorrect |
280 ms |
17532 KB |
Output isn't correct |
91 |
Incorrect |
316 ms |
17500 KB |
Output isn't correct |
92 |
Incorrect |
283 ms |
17488 KB |
Output isn't correct |