#include <bits/stdc++.h>
#define x first
#define y second
#define mp make_pair
using namespace std;
int n, s;
pair<int, int> start, home;
int T, H;
int occ_time[805][805];
bool vis[805][805];
bool tree[805][805];
pair<int, int > Q[1000000];
int movex[] = {0, 0, -1, 1};
int movey[] = {-1, 1, 0, 0};
bool check(pair<int, int> v){
if(v.x >= 0 && v.x < n && v.y >= 0 && v.y < n && !tree[v.x][v.y] && !vis[v.x][v.y]) return 1;
return 0;
}
void init_bfs(){
pair<int, int> u, v;
while(H < T){
u = Q[H++];
if(u == home) continue;
//cout << u.x << ' ' << u.y << ' ' << occ_time[u.x][u.y] << endl;
for(int i = 0; i < 4; i++){
v = mp(u.x + movex[i], u.y + movey[i]);
if(!check(v)) continue;
occ_time[v.x][v.y] = occ_time[u.x][u.y] + s;
vis[v.x][v.y] = 1;
Q[T++] = v;
}
}
}
int dist[805][805];
bool solve(int st){
memset(vis, 0, sizeof(vis));
H = T = 0;
pair<int, int> u, v;
dist[start.x][start.y] = st;
Q[T++] = start;
while(H < T){
u = Q[H++];
if(u == home) return 1;
if(dist[u.x][u.y] >= occ_time[u.x][u.y]) continue;
for(int i = 0; i < 4; i++){
v = mp(u.x + movex[i], u.y + movey[i]);
if(!check(v)) continue;
dist[v.x][v.y] = dist[u.x][u.y] + 1;
vis[v.x][v.y] = 1;
Q[T++] = v;
}
}
return 0;
}
int search(){
int l = 0, r = 800 * 800 + 1;
int res = -1;
while(l <= r){
int mid = (l + r) / 2;
//cout << mid << endl;
if(solve(mid * s)){
l = mid + 1;
res = max(res, mid);
}
else r = mid - 1;
}
return res;
}
int main(){
cin >> n >> s;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++){
char ch;
cin >> ch;
if(ch == 'M') {
start = mp(i, j);
}
if(ch == 'D') {
home = mp(i, j);
}
if(ch == 'H') {
Q[T++] = make_pair(i, j);
vis[i][j] = 1;
occ_time[i][j] = 0;
}
if(ch == 'T') {
tree[i][j] = 1;
}
}
init_bfs();
cout << search() << endl;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:79:3: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
79 | for(int i = 0; i < n; i++)
| ^~~
mecho.cpp:98:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
98 | init_bfs();
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
2 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
132 ms |
10732 KB |
Output is correct |
8 |
Correct |
1 ms |
1004 KB |
Output is correct |
9 |
Correct |
1 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1004 KB |
Output is correct |
11 |
Correct |
1 ms |
1004 KB |
Output is correct |
12 |
Correct |
2 ms |
1260 KB |
Output is correct |
13 |
Correct |
2 ms |
1260 KB |
Output is correct |
14 |
Correct |
2 ms |
1408 KB |
Output is correct |
15 |
Correct |
1 ms |
1004 KB |
Output is correct |
16 |
Correct |
1 ms |
1144 KB |
Output is correct |
17 |
Correct |
1 ms |
1152 KB |
Output is correct |
18 |
Correct |
1 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
1132 KB |
Output is correct |
20 |
Correct |
1 ms |
1132 KB |
Output is correct |
21 |
Correct |
2 ms |
1132 KB |
Output is correct |
22 |
Correct |
2 ms |
1132 KB |
Output is correct |
23 |
Correct |
1 ms |
1132 KB |
Output is correct |
24 |
Correct |
2 ms |
1280 KB |
Output is correct |
25 |
Correct |
2 ms |
1260 KB |
Output is correct |
26 |
Correct |
2 ms |
1260 KB |
Output is correct |
27 |
Correct |
2 ms |
1260 KB |
Output is correct |
28 |
Correct |
2 ms |
1260 KB |
Output is correct |
29 |
Correct |
2 ms |
1260 KB |
Output is correct |
30 |
Correct |
2 ms |
1260 KB |
Output is correct |
31 |
Correct |
2 ms |
1260 KB |
Output is correct |
32 |
Correct |
2 ms |
1260 KB |
Output is correct |
33 |
Correct |
12 ms |
2796 KB |
Output is correct |
34 |
Correct |
11 ms |
2924 KB |
Output is correct |
35 |
Correct |
30 ms |
4204 KB |
Output is correct |
36 |
Correct |
13 ms |
3180 KB |
Output is correct |
37 |
Correct |
14 ms |
3180 KB |
Output is correct |
38 |
Correct |
42 ms |
4736 KB |
Output is correct |
39 |
Correct |
17 ms |
3564 KB |
Output is correct |
40 |
Correct |
17 ms |
3564 KB |
Output is correct |
41 |
Correct |
50 ms |
5356 KB |
Output is correct |
42 |
Correct |
20 ms |
3948 KB |
Output is correct |
43 |
Correct |
20 ms |
3948 KB |
Output is correct |
44 |
Correct |
62 ms |
5996 KB |
Output is correct |
45 |
Correct |
24 ms |
4332 KB |
Output is correct |
46 |
Correct |
26 ms |
4332 KB |
Output is correct |
47 |
Correct |
78 ms |
6636 KB |
Output is correct |
48 |
Correct |
28 ms |
4716 KB |
Output is correct |
49 |
Correct |
28 ms |
4716 KB |
Output is correct |
50 |
Correct |
96 ms |
7404 KB |
Output is correct |
51 |
Correct |
32 ms |
5228 KB |
Output is correct |
52 |
Correct |
34 ms |
5224 KB |
Output is correct |
53 |
Correct |
111 ms |
8044 KB |
Output is correct |
54 |
Correct |
37 ms |
5632 KB |
Output is correct |
55 |
Correct |
39 ms |
5612 KB |
Output is correct |
56 |
Correct |
132 ms |
8812 KB |
Output is correct |
57 |
Correct |
47 ms |
6124 KB |
Output is correct |
58 |
Correct |
43 ms |
6124 KB |
Output is correct |
59 |
Correct |
153 ms |
9580 KB |
Output is correct |
60 |
Correct |
48 ms |
6636 KB |
Output is correct |
61 |
Correct |
49 ms |
6636 KB |
Output is correct |
62 |
Correct |
171 ms |
10476 KB |
Output is correct |
63 |
Correct |
157 ms |
9580 KB |
Output is correct |
64 |
Correct |
264 ms |
9580 KB |
Output is correct |
65 |
Correct |
210 ms |
9580 KB |
Output is correct |
66 |
Correct |
177 ms |
9580 KB |
Output is correct |
67 |
Correct |
171 ms |
9708 KB |
Output is correct |
68 |
Correct |
91 ms |
9580 KB |
Output is correct |
69 |
Correct |
89 ms |
9580 KB |
Output is correct |
70 |
Correct |
77 ms |
9708 KB |
Output is correct |
71 |
Correct |
84 ms |
9580 KB |
Output is correct |
72 |
Correct |
68 ms |
7788 KB |
Output is correct |
73 |
Correct |
74 ms |
11628 KB |
Output is correct |
74 |
Correct |
100 ms |
11628 KB |
Output is correct |
75 |
Correct |
116 ms |
11628 KB |
Output is correct |
76 |
Correct |
105 ms |
11628 KB |
Output is correct |
77 |
Correct |
111 ms |
11628 KB |
Output is correct |
78 |
Correct |
119 ms |
10300 KB |
Output is correct |
79 |
Correct |
86 ms |
9964 KB |
Output is correct |
80 |
Correct |
104 ms |
10220 KB |
Output is correct |
81 |
Correct |
112 ms |
10240 KB |
Output is correct |
82 |
Correct |
99 ms |
10092 KB |
Output is correct |
83 |
Correct |
123 ms |
10348 KB |
Output is correct |
84 |
Correct |
115 ms |
10220 KB |
Output is correct |
85 |
Correct |
123 ms |
10348 KB |
Output is correct |
86 |
Correct |
123 ms |
10316 KB |
Output is correct |
87 |
Correct |
121 ms |
10348 KB |
Output is correct |
88 |
Correct |
125 ms |
10604 KB |
Output is correct |
89 |
Correct |
127 ms |
10348 KB |
Output is correct |
90 |
Correct |
129 ms |
10348 KB |
Output is correct |
91 |
Correct |
128 ms |
10476 KB |
Output is correct |
92 |
Correct |
137 ms |
10476 KB |
Output is correct |