# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
491400 | nehasane | Mecho (IOI09_mecho) | C++14 | 513 ms | 7748 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |