# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
848104 | uped | Mecho (IOI09_mecho) | C++14 | 176 ms | 6424 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;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
int n, s;
cin >> n >> s;
vector<vector<char>> f(n, vector<char>(n));
queue<pair<int, int>> q;
vector<vector<int>> mt(n, vector<int>(n, INT32_MAX));
pair<int, int> start;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> f[i][j];
if (f[i][j] == 'M') {
start = {i, j};
} else if (f[i][j] == 'H') {
q.emplace(i, j);
mt[i][j] = 0;
}
}
}
auto inside = [&](int i, int j) {
return i >= 0 && i < n && j >= 0 && j < n && f[i][j] != 'T';
};
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int ni = i + dx[k];
int nj = j + dy[k];
if (!inside(ni, nj)) continue;
if (mt[i][j] + 1 < mt[ni][nj]) {
mt[ni][nj] = mt[i][j] + 1;
q.emplace(ni, nj);
}
}
}
bool fl = false;
int l = 0, r = 16000;
while (r > l + 1) {
while (!q.empty()) q.pop();
int mid = (l + r) / 2;
vector<vector<int>> ms(n, vector<int>(n, INT32_MAX));
ms[start.first][start.second] = 0;
bool good = false;
q.emplace(start.first, start.second);
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int ni = i + dx[k];
int nj = j + dy[k];
if (!inside(ni, nj)) continue;
if (f[ni][nj] == 'D') {
good = true;
fl = true;
break;
}
int steps = ms[i][j] + 1;
bool possible = mid + (steps / s) < mt[ni][nj];
if (possible && steps < ms[ni][nj]) {
ms[ni][nj] = steps;
q.emplace(ni, nj);
}
}
if (good) break;
}
if (good) {
l = mid;
} else {
r = mid;
}
}
if (fl) {
cout << l;
} else {
cout << -1;
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |