# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
550474 | Jomnoi | Mecho (IOI09_mecho) | C++17 | 182 ms | 6332 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 DEBUG false
using namespace std;
const int MAX_N = 810;
const int INF = 1e9 + 7;
const int d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
char s[MAX_N][MAX_N];
int db[MAX_N][MAX_N], dm[MAX_N][MAX_N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, S;
cin >> N >> S;
for(int i = 1; i <= N; i++) {
cin >> (s[i] + 1);
}
int sx, sy, ex, ey;
queue <pair <int, int>> q;
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
db[i][j] = INF;
if(s[i][j] == 'H') {
db[i][j] = 0;
q.emplace(i, j);
}
else if(s[i][j] == 'M') {
sx = i, sy = j;
}
else if(s[i][j] == 'D') {
ex = i, ey = j;
}
}
}
while(!q.empty()) {
auto [ux, uy] = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
int vx = ux + d[i][0], vy = uy + d[i][1];
if(vx < 1 or vx > N or vy < 1 or vy > N or s[vx][vy] == 'T' or s[vx][vy] == 'D' or db[vx][vy] != INF) {
continue;
}
db[vx][vy] = db[ux][uy] + 1;
q.emplace(vx, vy);
}
}
int l = 0, r = N * N, ans = -1;
while(l <= r) {
int mid = (l + r) / 2;
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
dm[i][j] = INF;
}
}
queue <pair <int, int>> q;
if(db[sx][sy] > mid) {
q.emplace(sx, sy);
dm[sx][sy] = 0;
}
while(!q.empty()) {
auto [ux, uy] = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
int vx = ux + d[i][0], vy = uy + d[i][1];
if(vx < 1 or vx > N or vy < 1 or vy > N or s[vx][vy] == 'T' or dm[vx][vy] != INF) {
continue;
}
if((dm[ux][uy] + 1) / S >= db[vx][vy] - mid) {
continue;
}
dm[vx][vy] = dm[ux][uy] + 1;
q.emplace(vx, vy);
}
}
if(dm[ex][ey] == INF) {
r = mid - 1;
}
else {
l = mid + 1;
ans = mid;
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |