# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
576345 | acatmeowmeow | Mecho (IOI09_mecho) | C++11 | 662 ms | 13036 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.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 8e2 + 5;
int n, s;
char grid[N][N];
pair<int, int> init, home;
vector<pair<int, int>> hive;
vector<int> dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
queue<pair<int, int>> qmecho, qbee;
bool vmecho[N][N], vbee[N][N];
int dmecho[N][N], dbee[N][N];
bool bee_inbound(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= n && !vbee[x][y] && (grid[x][y] == 'G' || grid[x][y] == 'M');
}
bool mecho_inbound(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= n && !vmecho[x][y] && (grid[x][y] == 'G' || grid[x][y] == 'D');
}
bool valid(int m) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
vmecho[i][j] = vbee[i][j] = false;
dmecho[i][j] = dbee[i][j] = 1e18;
}
}
for (auto&b : hive) {
int x = b.first, y = b.second;
dbee[x][y] = 0, vbee[x][y] = true, qbee.push(b);
}
while (qbee.size()) {
int x = qbee.front().first, y = qbee.front().second; qbee.pop();
for (int k = 0; k < 4; k++) {
int tox = x + dx[k], toy = y + dy[k];
if (!bee_inbound(tox, toy)) continue;
dbee[tox][toy] = dbee[x][y] + 1, vbee[tox][toy] = true, qbee.push({tox, toy});
}
}
int x = init.first, y = init.second;
dmecho[x][y] = 0, vmecho[x][y] = true, qmecho.push({x, y});
while (qmecho.size()) {
int x = qmecho.front().first, y = qmecho.front().second; qmecho.pop();
if (dmecho[x][y]/s >= dbee[x][y] - m) continue;
for (int k = 0; k < 4; k++) {
int tox = x + dx[k], toy = y + dy[k];
if (!mecho_inbound(tox, toy)) continue;
dmecho[tox][toy] = dmecho[x][y] + 1, vmecho[tox][toy] = true, qmecho.push({tox, toy});
}
}
x = home.first, y = home.second;
return vmecho[x][y] && dmecho[x][y]/s < dbee[x][y] - m;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> s;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'M') init = {i, j};
else if(grid[i][j] == 'D') home = {i, j};
else if(grid[i][j] == 'H') hive.push_back({i, j});
}
}
int l = 0, r = n*n, ans = -1;
while (l <= r) {
int m = (l + r)/2;
if (valid(m)) ans = m, l = m + 1;
else r = m - 1;
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |