#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();
| ^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
2 ms |
1004 KB |
Output is correct |
3 |
Correct |
1 ms |
1004 KB |
Output is correct |
4 |
Correct |
1 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 |
131 ms |
11244 KB |
Output is correct |
8 |
Correct |
2 ms |
1004 KB |
Output is correct |
9 |
Correct |
2 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1004 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 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 |
1388 KB |
Output is correct |
15 |
Correct |
1 ms |
1004 KB |
Output is correct |
16 |
Correct |
1 ms |
1152 KB |
Output is correct |
17 |
Correct |
2 ms |
1132 KB |
Output is correct |
18 |
Correct |
2 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
1132 KB |
Output is correct |
20 |
Correct |
2 ms |
1260 KB |
Output is correct |
21 |
Correct |
2 ms |
1132 KB |
Output is correct |
22 |
Correct |
2 ms |
1132 KB |
Output is correct |
23 |
Correct |
2 ms |
1132 KB |
Output is correct |
24 |
Correct |
2 ms |
1260 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 |
1388 KB |
Output is correct |
33 |
Correct |
11 ms |
2924 KB |
Output is correct |
34 |
Correct |
11 ms |
2924 KB |
Output is correct |
35 |
Correct |
31 ms |
4460 KB |
Output is correct |
36 |
Correct |
14 ms |
3308 KB |
Output is correct |
37 |
Correct |
14 ms |
3436 KB |
Output is correct |
38 |
Correct |
42 ms |
4844 KB |
Output is correct |
39 |
Correct |
17 ms |
3692 KB |
Output is correct |
40 |
Correct |
17 ms |
3820 KB |
Output is correct |
41 |
Correct |
50 ms |
5484 KB |
Output is correct |
42 |
Correct |
20 ms |
4204 KB |
Output is correct |
43 |
Correct |
25 ms |
4204 KB |
Output is correct |
44 |
Correct |
68 ms |
6380 KB |
Output is correct |
45 |
Correct |
24 ms |
4588 KB |
Output is correct |
46 |
Correct |
25 ms |
4588 KB |
Output is correct |
47 |
Correct |
76 ms |
6892 KB |
Output is correct |
48 |
Correct |
28 ms |
5120 KB |
Output is correct |
49 |
Correct |
28 ms |
5100 KB |
Output is correct |
50 |
Correct |
95 ms |
7808 KB |
Output is correct |
51 |
Correct |
36 ms |
5612 KB |
Output is correct |
52 |
Correct |
33 ms |
5612 KB |
Output is correct |
53 |
Correct |
113 ms |
8428 KB |
Output is correct |
54 |
Correct |
38 ms |
6124 KB |
Output is correct |
55 |
Correct |
38 ms |
6124 KB |
Output is correct |
56 |
Correct |
134 ms |
9324 KB |
Output is correct |
57 |
Correct |
43 ms |
6636 KB |
Output is correct |
58 |
Correct |
43 ms |
6764 KB |
Output is correct |
59 |
Correct |
153 ms |
10092 KB |
Output is correct |
60 |
Correct |
58 ms |
7276 KB |
Output is correct |
61 |
Correct |
49 ms |
7276 KB |
Output is correct |
62 |
Correct |
173 ms |
10988 KB |
Output is correct |
63 |
Correct |
158 ms |
10220 KB |
Output is correct |
64 |
Correct |
264 ms |
10348 KB |
Output is correct |
65 |
Correct |
213 ms |
10220 KB |
Output is correct |
66 |
Correct |
175 ms |
10220 KB |
Output is correct |
67 |
Correct |
183 ms |
10476 KB |
Output is correct |
68 |
Correct |
89 ms |
10348 KB |
Output is correct |
69 |
Correct |
89 ms |
10220 KB |
Output is correct |
70 |
Correct |
76 ms |
10220 KB |
Output is correct |
71 |
Correct |
91 ms |
10220 KB |
Output is correct |
72 |
Correct |
69 ms |
8428 KB |
Output is correct |
73 |
Correct |
75 ms |
12396 KB |
Output is correct |
74 |
Correct |
104 ms |
12268 KB |
Output is correct |
75 |
Correct |
118 ms |
12268 KB |
Output is correct |
76 |
Correct |
109 ms |
12268 KB |
Output is correct |
77 |
Correct |
106 ms |
12268 KB |
Output is correct |
78 |
Correct |
119 ms |
10988 KB |
Output is correct |
79 |
Correct |
86 ms |
10732 KB |
Output is correct |
80 |
Correct |
103 ms |
10720 KB |
Output is correct |
81 |
Correct |
110 ms |
10732 KB |
Output is correct |
82 |
Correct |
104 ms |
10732 KB |
Output is correct |
83 |
Correct |
124 ms |
10988 KB |
Output is correct |
84 |
Correct |
113 ms |
11008 KB |
Output is correct |
85 |
Correct |
124 ms |
10860 KB |
Output is correct |
86 |
Correct |
120 ms |
10860 KB |
Output is correct |
87 |
Correct |
123 ms |
10860 KB |
Output is correct |
88 |
Correct |
126 ms |
11244 KB |
Output is correct |
89 |
Correct |
130 ms |
10988 KB |
Output is correct |
90 |
Correct |
131 ms |
10988 KB |
Output is correct |
91 |
Correct |
128 ms |
11148 KB |
Output is correct |
92 |
Correct |
140 ms |
11152 KB |
Output is correct |