// https://oj.uz/problem/view/IOI09_mecho
#include <cstdio>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
// binary search + BFS
// n <= 800, s <= 1000
int n, s;
char m[805][805];
int mr,mc,dr,dc;
struct E {
int r,c,d;
};
queue<E> q;
const int INF = 1e9;
int beetime[805][805]; // time for bees to reach here
int steps[805][805]; // min steps for mecho to reach here
// init q before calling this
void bfs() {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
beetime[i][j] = -1;
while (!q.empty()) {
E e = q.front();
q.pop();
if (e.r < 0 || e.r >= n || e.c < 0 || e.c >= n) continue;
if (m[e.r][e.c] == 'T' || m[e.r][e.c] == 'D' || beetime[e.r][e.c] != -1) continue;
beetime[e.r][e.c] = e.d;
q.push({e.r+1, e.c, e.d+1});
q.push({e.r-1, e.c, e.d+1});
q.push({e.r, e.c+1, e.d+1});
q.push({e.r, e.c-1, e.d+1});
}
}
// check if eating honey for t minutes is ok
bool check(int t) {
q.push({mr, mc, t*s});
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
steps[i][j] = -1;
while (!q.empty()) {
E e = q.front();
q.pop();
if (e.r < 0 || e.r >= n || e.c < 0 || e.c >= n) continue;
if (m[e.r][e.c] == 'T' || steps[e.r][e.c] != -1) continue;
if (beetime[e.r][e.c] != -1 && e.d / s >= beetime[e.r][e.c]) continue; // stinged by bees
steps[e.r][e.c] = e.d;
q.push({e.r+1, e.c, e.d+1});
q.push({e.r-1, e.c, e.d+1});
q.push({e.r, e.c+1, e.d+1});
q.push({e.r, e.c-1, e.d+1});
}
return steps[dr][dc] != -1;
}
int main() {
scanf("%d %d", &n, &s);
for (int i = 0; i < n; i++) {
scanf("%s", m[i]);
for (int j = 0; j < n; j++)
if (m[i][j] == 'M') mr = i, mc = j;
else if (m[i][j] == 'D') dr = i, dc = j;
else if (m[i][j] == 'H') q.push({i,j,0});
}
bfs(); // calc beetime[][]
int lo = 0, hi = 800*800;
while (lo < hi) {
int mid = (lo+hi+1) / 2;
if (check(mid))
lo = mid;
else
hi = mid-1;
}
if (lo == 0 && !check(0))
printf("-1");
else
printf("%d", lo);
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%d %d", &n, &s);
| ~~~~~^~~~~~~~~~~~~~~~~
mecho.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%s", m[i]);
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
168 ms |
6584 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
0 ms |
480 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
5 ms |
2772 KB |
Output is correct |
34 |
Correct |
6 ms |
2704 KB |
Output is correct |
35 |
Correct |
36 ms |
2772 KB |
Output is correct |
36 |
Correct |
7 ms |
3028 KB |
Output is correct |
37 |
Correct |
9 ms |
3028 KB |
Output is correct |
38 |
Correct |
53 ms |
3156 KB |
Output is correct |
39 |
Correct |
8 ms |
3412 KB |
Output is correct |
40 |
Correct |
10 ms |
3428 KB |
Output is correct |
41 |
Correct |
56 ms |
3492 KB |
Output is correct |
42 |
Correct |
10 ms |
3796 KB |
Output is correct |
43 |
Correct |
10 ms |
3796 KB |
Output is correct |
44 |
Correct |
76 ms |
3852 KB |
Output is correct |
45 |
Correct |
13 ms |
4180 KB |
Output is correct |
46 |
Correct |
17 ms |
4128 KB |
Output is correct |
47 |
Correct |
87 ms |
4212 KB |
Output is correct |
48 |
Correct |
13 ms |
4460 KB |
Output is correct |
49 |
Correct |
15 ms |
4444 KB |
Output is correct |
50 |
Correct |
98 ms |
4564 KB |
Output is correct |
51 |
Correct |
17 ms |
4912 KB |
Output is correct |
52 |
Correct |
19 ms |
4904 KB |
Output is correct |
53 |
Correct |
145 ms |
4820 KB |
Output is correct |
54 |
Correct |
22 ms |
5236 KB |
Output is correct |
55 |
Correct |
20 ms |
5260 KB |
Output is correct |
56 |
Correct |
155 ms |
5288 KB |
Output is correct |
57 |
Correct |
24 ms |
5560 KB |
Output is correct |
58 |
Correct |
26 ms |
5556 KB |
Output is correct |
59 |
Correct |
163 ms |
5596 KB |
Output is correct |
60 |
Correct |
29 ms |
5952 KB |
Output is correct |
61 |
Correct |
34 ms |
5884 KB |
Output is correct |
62 |
Correct |
213 ms |
6000 KB |
Output is correct |
63 |
Correct |
184 ms |
5888 KB |
Output is correct |
64 |
Correct |
258 ms |
5972 KB |
Output is correct |
65 |
Correct |
201 ms |
5964 KB |
Output is correct |
66 |
Correct |
158 ms |
5972 KB |
Output is correct |
67 |
Correct |
159 ms |
5960 KB |
Output is correct |
68 |
Correct |
71 ms |
6132 KB |
Output is correct |
69 |
Correct |
52 ms |
6092 KB |
Output is correct |
70 |
Correct |
51 ms |
6092 KB |
Output is correct |
71 |
Correct |
48 ms |
6060 KB |
Output is correct |
72 |
Correct |
38 ms |
6064 KB |
Output is correct |
73 |
Correct |
53 ms |
7512 KB |
Output is correct |
74 |
Correct |
83 ms |
7500 KB |
Output is correct |
75 |
Correct |
117 ms |
7532 KB |
Output is correct |
76 |
Correct |
110 ms |
7580 KB |
Output is correct |
77 |
Correct |
104 ms |
7508 KB |
Output is correct |
78 |
Correct |
136 ms |
7360 KB |
Output is correct |
79 |
Correct |
71 ms |
7368 KB |
Output is correct |
80 |
Correct |
94 ms |
7436 KB |
Output is correct |
81 |
Correct |
106 ms |
7428 KB |
Output is correct |
82 |
Correct |
87 ms |
7436 KB |
Output is correct |
83 |
Correct |
129 ms |
7112 KB |
Output is correct |
84 |
Correct |
108 ms |
7088 KB |
Output is correct |
85 |
Correct |
113 ms |
7116 KB |
Output is correct |
86 |
Correct |
131 ms |
6992 KB |
Output is correct |
87 |
Correct |
109 ms |
7140 KB |
Output is correct |
88 |
Correct |
141 ms |
6836 KB |
Output is correct |
89 |
Correct |
134 ms |
6860 KB |
Output is correct |
90 |
Correct |
137 ms |
6892 KB |
Output is correct |
91 |
Correct |
131 ms |
6844 KB |
Output is correct |
92 |
Correct |
125 ms |
6860 KB |
Output is correct |