# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
508812 | tabr | Mecho (IOI09_mecho) | C++17 | 173 ms | 7000 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;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
vector<vector<int>> a(n, vector<int>(n, -1));
queue<int> que;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'H') {
a[i][j] = 0;
que.emplace(i * n + j);
} else if (s[i][j] == 'D' || s[i][j] == 'T') {
a[i][j] = 0;
}
}
}
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
while (!que.empty()) {
int x = que.front() / n;
int y = que.front() % n;
que.pop();
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (min(nx, ny) < 0 || max(nx, ny) >= n || a[nx][ny] != -1) {
continue;
}
a[nx][ny] = a[x][y] + 1;
que.emplace(nx * n + ny);
}
}
int sx = -1;
int sy = -1;
int tx = -1;
int ty = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'M') {
sx = i;
sy = j;
} else if (s[i][j] == 'D') {
tx = i;
ty = j;
}
}
}
debug(a);
int lo = -1;
int hi = a[sx][sy];
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
vector<vector<int>> b(n, vector<int>(n, -1));
que.emplace(sx * n + sy);
b[sx][sy] = 0;
while (!que.empty()) {
int x = que.front() / n;
int y = que.front() % n;
que.pop();
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (nx == tx && ny == ty) {
b[tx][ty] = 0;
break;
}
if (min(nx, ny) < 0 || max(nx, ny) >= n || b[nx][ny] != -1 || b[x][y] + 1 >= m * (a[nx][ny] - mid)) {
continue;
}
b[nx][ny] = b[x][y] + 1;
que.emplace(nx * n + ny);
}
}
if (b[tx][ty] != -1) {
lo = mid;
} else {
hi = mid;
}
}
cout << lo << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |