#include "bits/stdc++.h"
using namespace std;
const int maxn = 805;
char s[maxn][maxn];
int n, S;
int vis[maxn][maxn], dis[maxn][maxn], dis1[maxn][maxn];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int sx, sy, fx, fy;
void precalc(){
using pii = pair<int, pair<int, int>>;
priority_queue<pii, vector<pii>, greater<pii>> q;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
dis[i][j] = n * n;
if(s[i][j] == 'H'){
q.push({0, {i, j}});
dis[i][j] = 0;
}
}
}
while(!q.empty()){
pii src = q.top();
q.pop();
for(int i = 0; i < 4; ++i){
int newx = src.second.first + dx[i], newy = src.second.second + dy[i];
if(newx <= n && newx > 0 && newy <= n && newy > 0){
if(dis[src.second.first][src.second.second] + 1 < dis[newx][newy] && s[newx][newy] != 'T' && s[newx][newy] != 'D'){
dis[newx][newy] = dis[src.second.first][src.second.second] + 1;
q.push({dis[newx][newy], {newx, newy}});
}
}
}
}
}
bool can(int x){
queue<pair<int, int>> q;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
dis1[i][j] = n * n;
if(s[i][j] == 'T')vis[i][j] = 1;
else if(s[i][j] == 'M'){
vis[i][j] = 1;
dis1[i][j] = 0;
q.push({i, j});
}else {
vis[i][j] = 0;
}
}
}
while(!q.empty()){
pair<int, int> src = q.front();
q.pop();
for(int i = 0; i < 4; ++i){
int newx = src.first + dx[i], newy = src.second + dy[i];
if(newx <= n && newx > 0 && newy <= n && newy > 0){
if(s[newx][newy] != 'T' && !vis[newx][newy]){
int cost = (dis1[src.first][src.second] + 1) / S + x;
if(cost < dis[newx][newy]){
vis[newx][newy] = 1;
dis1[newx][newy] = dis1[src.first][src.second] + 1;
q.push({newx, newy});
}
}
}
}
}
if(vis[fx][fy])return 1;
return 0;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> S;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> s[i][j];
if(s[i][j] == 'M'){
sx = i, sy = j;
}else if(s[i][j] == 'D'){
fx = i, fy = j;
}
}
}
precalc();
int lo = 0, hi = n * n, ans = -1;
while(lo <= hi){
int mid = (lo + hi) >> 1;
if(can(mid)){
ans = mid;
lo = mid + 1;
}else {
hi = mid - 1;
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
213 ms |
8716 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
876 KB |
Output isn't correct |
14 |
Correct |
2 ms |
1004 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
748 KB |
Output is correct |
22 |
Correct |
1 ms |
748 KB |
Output is correct |
23 |
Correct |
1 ms |
876 KB |
Output is correct |
24 |
Correct |
1 ms |
876 KB |
Output is correct |
25 |
Correct |
1 ms |
1004 KB |
Output is correct |
26 |
Correct |
2 ms |
1004 KB |
Output is correct |
27 |
Correct |
2 ms |
1004 KB |
Output is correct |
28 |
Correct |
2 ms |
1004 KB |
Output is correct |
29 |
Correct |
2 ms |
1132 KB |
Output is correct |
30 |
Correct |
2 ms |
1132 KB |
Output is correct |
31 |
Correct |
2 ms |
1132 KB |
Output is correct |
32 |
Correct |
2 ms |
1132 KB |
Output is correct |
33 |
Correct |
10 ms |
3948 KB |
Output is correct |
34 |
Correct |
11 ms |
3948 KB |
Output is correct |
35 |
Correct |
38 ms |
3948 KB |
Output is correct |
36 |
Correct |
12 ms |
4460 KB |
Output is correct |
37 |
Correct |
14 ms |
4460 KB |
Output is correct |
38 |
Correct |
49 ms |
4460 KB |
Output is correct |
39 |
Correct |
15 ms |
4972 KB |
Output is correct |
40 |
Correct |
17 ms |
4972 KB |
Output is correct |
41 |
Correct |
65 ms |
4972 KB |
Output is correct |
42 |
Correct |
18 ms |
5484 KB |
Output is correct |
43 |
Correct |
21 ms |
5484 KB |
Output is correct |
44 |
Correct |
83 ms |
5484 KB |
Output is correct |
45 |
Correct |
24 ms |
5996 KB |
Output is correct |
46 |
Correct |
32 ms |
6076 KB |
Output is correct |
47 |
Correct |
98 ms |
5996 KB |
Output is correct |
48 |
Correct |
26 ms |
6508 KB |
Output is correct |
49 |
Correct |
36 ms |
6508 KB |
Output is correct |
50 |
Correct |
121 ms |
6636 KB |
Output is correct |
51 |
Correct |
30 ms |
7020 KB |
Output is correct |
52 |
Correct |
33 ms |
7148 KB |
Output is correct |
53 |
Correct |
146 ms |
7020 KB |
Output is correct |
54 |
Correct |
34 ms |
7532 KB |
Output is correct |
55 |
Correct |
40 ms |
7532 KB |
Output is correct |
56 |
Correct |
170 ms |
7532 KB |
Output is correct |
57 |
Correct |
38 ms |
8044 KB |
Output is correct |
58 |
Correct |
44 ms |
8096 KB |
Output is correct |
59 |
Correct |
234 ms |
8044 KB |
Output is correct |
60 |
Correct |
43 ms |
8556 KB |
Output is correct |
61 |
Correct |
58 ms |
8684 KB |
Output is correct |
62 |
Correct |
218 ms |
8684 KB |
Output is correct |
63 |
Correct |
228 ms |
8556 KB |
Output is correct |
64 |
Correct |
336 ms |
8684 KB |
Output is correct |
65 |
Correct |
275 ms |
8684 KB |
Output is correct |
66 |
Correct |
246 ms |
8556 KB |
Output is correct |
67 |
Correct |
237 ms |
8556 KB |
Output is correct |
68 |
Correct |
176 ms |
8704 KB |
Output is correct |
69 |
Correct |
184 ms |
8684 KB |
Output is correct |
70 |
Correct |
157 ms |
8812 KB |
Output is correct |
71 |
Correct |
168 ms |
8764 KB |
Output is correct |
72 |
Incorrect |
150 ms |
8740 KB |
Output isn't correct |
73 |
Incorrect |
157 ms |
8668 KB |
Output isn't correct |
74 |
Correct |
180 ms |
8668 KB |
Output is correct |
75 |
Correct |
197 ms |
8800 KB |
Output is correct |
76 |
Correct |
181 ms |
8664 KB |
Output is correct |
77 |
Correct |
182 ms |
8800 KB |
Output is correct |
78 |
Correct |
203 ms |
8712 KB |
Output is correct |
79 |
Correct |
168 ms |
8720 KB |
Output is correct |
80 |
Correct |
185 ms |
8712 KB |
Output is correct |
81 |
Correct |
185 ms |
8684 KB |
Output is correct |
82 |
Correct |
180 ms |
8720 KB |
Output is correct |
83 |
Correct |
204 ms |
8936 KB |
Output is correct |
84 |
Correct |
190 ms |
8640 KB |
Output is correct |
85 |
Correct |
204 ms |
8864 KB |
Output is correct |
86 |
Correct |
204 ms |
8640 KB |
Output is correct |
87 |
Correct |
199 ms |
8808 KB |
Output is correct |
88 |
Correct |
205 ms |
8704 KB |
Output is correct |
89 |
Correct |
248 ms |
8704 KB |
Output is correct |
90 |
Correct |
210 ms |
8704 KB |
Output is correct |
91 |
Correct |
212 ms |
8704 KB |
Output is correct |
92 |
Correct |
230 ms |
8736 KB |
Output is correct |