# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
491400 |
2021-12-02T00:00:01 Z |
nehasane |
Mecho (IOI09_mecho) |
C++14 |
|
513 ms |
7748 KB |
//for an editorial and code in C++, visit my blog - https://nehasane.blogspot.com/2021/11/ioi-2009-mecho.html
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 800;
vector <string> field(MAX_N);
bool bees_visited[MAX_N][MAX_N], mecho_visited[MAX_N][MAX_N];
//mecho_time records the time taken for mecho to reach node[x][y].
//bees_time does the same for the bees
int mecho_time[MAX_N][MAX_N], bees_time[MAX_N][MAX_N];
queue <pair <int, int>> q;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n, s;
bool valid_sq(int x, int y){
if (x >= 0 && x < n && y >= 0 && y < n && (field[x][y] == 'G' || field[x][y] == 'M'))
return true;
return false;
}
int main()
{
cin >> n >> s;
for (int i = 0; i < n; i++)
cin >> field[i];
vector <pair <int, int>> hives;
int mechox, mechoy, home_x, home_y;
//find x and y coordinates for for Mecho, the bees and the cave
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (field[i][j] == 'M'){
mechox = i;
mechoy = j;
}else if (field[i][j] == 'H'){
hives.push_back({i, j});
}else if (field[i][j] == 'D'){
home_x = i;
home_y = j;
}
}
}
//binary search on waiting time
int l = 0, r = 3*n;
while (l <= r){
int eating_time = (l + r) / 2;
memset(bees_visited, false, sizeof(bees_visited));
memset(mecho_visited, false, sizeof(mecho_visited));
memset(bees_time, 0, sizeof(bees_time));
memset(mecho_time, 0, sizeof(mecho_time));
//move bees
for (auto i : hives){
q.push({i.first, i.second});
bees_visited[i.first][i.second] = true;
}
while (!q.empty()){
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if (valid_sq(nx, ny) && !bees_visited[nx][ny]){
bees_time[nx][ny] = bees_time[x][y] + 1;
q.push({nx, ny});
bees_visited[nx][ny] = true;
}
}
}
//move Mecho
q.push({mechox, mechoy});
mecho_visited[mechox][mechoy] = true;
if (bees_time[mechox][mechoy] <= eating_time)
q.pop();
while (!q.empty()){
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
//check if mecho reaces node[x][y] before the bees
/*divide the time mecho takes to reach a node by s, since mecho
walks s steps at a time.
substract the eating time from the time taken for the bees to
reach the node, because that time was used by mecho for eating
*/
if (valid_sq(nx, ny) && !mecho_visited[nx][ny] &&
(mecho_time[x][y] + 1) / s < bees_time[nx][ny] - eating_time){
mecho_visited[nx][ny] = true;
q.push({nx, ny});
mecho_time[nx][ny] = mecho_time[x][y] + 1;
}
}
}
//check if mecho reached a node surrounding his cave before the bees
bool mecho_reached = false;
for (int i = 0; i < 4; i++){
int nx = home_x+ dx[i], ny = home_y + dy[i];
if (valid_sq(nx, ny) && (mecho_time[nx][ny] / s) < bees_time[nx][ny] - eating_time
&& mecho_visited[nx][ny])
mecho_reached = true;
}
if (mecho_reached)
l = eating_time + 1;
else
r = eating_time - 1;
}
cout << l - 1 << '\n';
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:68:37: warning: 'mechoy' may be used uninitialized in this function [-Wmaybe-uninitialized]
68 | if (bees_time[mechox][mechoy] <= eating_time)
| ~~~~~~~~~~~~~~~~~~~~~~~~^
mecho.cpp:68:37: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
mecho.cpp:92:37: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
92 | int nx = home_x+ dx[i], ny = home_y + dy[i];
| ^~
mecho.cpp:92:17: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
92 | int nx = home_x+ dx[i], ny = home_y + dy[i];
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6476 KB |
Output is correct |
2 |
Correct |
4 ms |
6476 KB |
Output is correct |
3 |
Correct |
4 ms |
6476 KB |
Output is correct |
4 |
Correct |
5 ms |
6476 KB |
Output is correct |
5 |
Correct |
5 ms |
6584 KB |
Output is correct |
6 |
Correct |
5 ms |
6476 KB |
Output is correct |
7 |
Correct |
376 ms |
7508 KB |
Output is correct |
8 |
Correct |
4 ms |
6476 KB |
Output is correct |
9 |
Correct |
4 ms |
6476 KB |
Output is correct |
10 |
Correct |
4 ms |
6504 KB |
Output is correct |
11 |
Correct |
5 ms |
6476 KB |
Output is correct |
12 |
Incorrect |
7 ms |
6572 KB |
Output isn't correct |
13 |
Correct |
9 ms |
6476 KB |
Output is correct |
14 |
Correct |
8 ms |
6476 KB |
Output is correct |
15 |
Incorrect |
5 ms |
6524 KB |
Output isn't correct |
16 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
17 |
Incorrect |
8 ms |
6476 KB |
Output isn't correct |
18 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
19 |
Incorrect |
4 ms |
6476 KB |
Output isn't correct |
20 |
Incorrect |
5 ms |
6584 KB |
Output isn't correct |
21 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
22 |
Incorrect |
7 ms |
6476 KB |
Output isn't correct |
23 |
Incorrect |
5 ms |
6476 KB |
Output isn't correct |
24 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
25 |
Incorrect |
6 ms |
6476 KB |
Output isn't correct |
26 |
Incorrect |
6 ms |
6500 KB |
Output isn't correct |
27 |
Incorrect |
7 ms |
6588 KB |
Output isn't correct |
28 |
Incorrect |
7 ms |
6588 KB |
Output isn't correct |
29 |
Incorrect |
7 ms |
6476 KB |
Output isn't correct |
30 |
Incorrect |
7 ms |
6476 KB |
Output isn't correct |
31 |
Incorrect |
7 ms |
6476 KB |
Output isn't correct |
32 |
Incorrect |
8 ms |
6588 KB |
Output isn't correct |
33 |
Incorrect |
38 ms |
6732 KB |
Output isn't correct |
34 |
Incorrect |
45 ms |
6732 KB |
Output isn't correct |
35 |
Correct |
55 ms |
6752 KB |
Output is correct |
36 |
Incorrect |
48 ms |
6768 KB |
Output isn't correct |
37 |
Incorrect |
58 ms |
6756 KB |
Output isn't correct |
38 |
Correct |
86 ms |
6756 KB |
Output is correct |
39 |
Incorrect |
61 ms |
6784 KB |
Output isn't correct |
40 |
Incorrect |
70 ms |
6732 KB |
Output isn't correct |
41 |
Correct |
90 ms |
6796 KB |
Output is correct |
42 |
Incorrect |
73 ms |
7040 KB |
Output isn't correct |
43 |
Incorrect |
113 ms |
6988 KB |
Output isn't correct |
44 |
Correct |
123 ms |
7048 KB |
Output is correct |
45 |
Incorrect |
89 ms |
7096 KB |
Output isn't correct |
46 |
Incorrect |
106 ms |
6988 KB |
Output isn't correct |
47 |
Correct |
170 ms |
7104 KB |
Output is correct |
48 |
Incorrect |
107 ms |
7236 KB |
Output isn't correct |
49 |
Incorrect |
125 ms |
7136 KB |
Output isn't correct |
50 |
Correct |
192 ms |
7140 KB |
Output is correct |
51 |
Incorrect |
133 ms |
7184 KB |
Output isn't correct |
52 |
Incorrect |
149 ms |
7188 KB |
Output isn't correct |
53 |
Correct |
206 ms |
7192 KB |
Output is correct |
54 |
Incorrect |
158 ms |
7248 KB |
Output isn't correct |
55 |
Incorrect |
190 ms |
7244 KB |
Output isn't correct |
56 |
Correct |
267 ms |
7244 KB |
Output is correct |
57 |
Incorrect |
172 ms |
7244 KB |
Output isn't correct |
58 |
Incorrect |
211 ms |
7272 KB |
Output isn't correct |
59 |
Correct |
290 ms |
7280 KB |
Output is correct |
60 |
Incorrect |
181 ms |
7364 KB |
Output isn't correct |
61 |
Incorrect |
245 ms |
7332 KB |
Output isn't correct |
62 |
Correct |
311 ms |
7336 KB |
Output is correct |
63 |
Correct |
418 ms |
7328 KB |
Output is correct |
64 |
Correct |
450 ms |
7332 KB |
Output is correct |
65 |
Correct |
513 ms |
7332 KB |
Output is correct |
66 |
Correct |
423 ms |
7244 KB |
Output is correct |
67 |
Correct |
383 ms |
7332 KB |
Output is correct |
68 |
Correct |
340 ms |
7492 KB |
Output is correct |
69 |
Correct |
340 ms |
7364 KB |
Output is correct |
70 |
Correct |
334 ms |
7368 KB |
Output is correct |
71 |
Correct |
358 ms |
7380 KB |
Output is correct |
72 |
Correct |
341 ms |
7372 KB |
Output is correct |
73 |
Correct |
319 ms |
7636 KB |
Output is correct |
74 |
Correct |
319 ms |
7632 KB |
Output is correct |
75 |
Correct |
370 ms |
7748 KB |
Output is correct |
76 |
Correct |
348 ms |
7628 KB |
Output is correct |
77 |
Correct |
338 ms |
7684 KB |
Output is correct |
78 |
Correct |
365 ms |
7592 KB |
Output is correct |
79 |
Correct |
361 ms |
7508 KB |
Output is correct |
80 |
Correct |
333 ms |
7620 KB |
Output is correct |
81 |
Correct |
411 ms |
7596 KB |
Output is correct |
82 |
Correct |
347 ms |
7600 KB |
Output is correct |
83 |
Correct |
361 ms |
7556 KB |
Output is correct |
84 |
Correct |
421 ms |
7552 KB |
Output is correct |
85 |
Correct |
335 ms |
7620 KB |
Output is correct |
86 |
Correct |
386 ms |
7556 KB |
Output is correct |
87 |
Correct |
415 ms |
7452 KB |
Output is correct |
88 |
Correct |
377 ms |
7504 KB |
Output is correct |
89 |
Correct |
352 ms |
7588 KB |
Output is correct |
90 |
Correct |
354 ms |
7504 KB |
Output is correct |
91 |
Correct |
397 ms |
7504 KB |
Output is correct |
92 |
Correct |
369 ms |
7500 KB |
Output is correct |