# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
518468 | Alex_tz307 | Mecho (IOI09_mecho) | C++17 | 188 ms | 6232 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>
#define INF 0x3f3f3f3f
using namespace std;
void testCase() {
int n, s;
cin >> n >> s;
vector<string> a(n);
queue<pair<int, int>> q;
vector<vector<int>> bee(n, vector<int>(n, INF));
int istart = -1, jstart = -1, istop = -1, jstop = -1;
for (int i = 0; i < n; ++i) {
cin >> a[i];
for (int j = 0; j < n; ++j) {
if (a[i][j] == 'H') {
bee[i][j] = 1;
q.emplace(i, j);
} else if (a[i][j] == 'M') {
istart = i, jstart = j;
} else if (a[i][j] == 'D') {
istop = i, jstop = j;
}
}
}
const int di[] = {-1, 0, 1, 0}, dj[] = {0, 1, 0, -1};
auto valid = [&](const int &i, const int &j) -> bool {
return 0 <= min(i, j) && max(i, j) < n && a[i][j] != 'T' && a[i][j] != 'H';
};
while (!q.empty()) {
int i, j;
tie(i, j) = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int iv = i + di[k], jv = j + dj[k];
if (valid(iv, jv) && a[iv][jv] != 'D' && bee[iv][jv] == INF) {
bee[iv][jv] = bee[i][j] + 1;
q.emplace(iv, jv);
}
}
}
auto checkDistance = [&](const int &m, const int &b) -> bool {
return m / s < b;
};
auto check = [&](const int &t) -> bool {
vector<vector<int>> dp(n, vector<int>(n, INF));
queue<pair<int, int>> q;
dp[istart][jstart] = t;
q.emplace(istart, jstart);
while (!q.empty()) {
int i, j;
tie(i, j) = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int iv = i + di[k], jv = j + dj[k];
if (valid(iv, jv) && dp[iv][jv] == INF && checkDistance(dp[i][j] + 1, bee[iv][jv] - t)) {
dp[iv][jv] = dp[i][j] + 1;
q.emplace(iv, jv);
}
}
}
if (dp[istop][jstop] == INF) {
return false;
}
return true;
};
int l = 0, r = bee[istart][jstart] - 1, ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |