# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
789665 | phongcd | Mecho (IOI09_mecho) | C++14 | 145 ms | 9416 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 ll long long
#define ld long double
#define ull unsigned long long
#define ii pair <int, int>
#define ill pair <ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define file "test"
using namespace std;
const ll N = 8e2 + 2;
const ll MOD = 1e9;
const ll INF = 1e18;
const ll base = 311;
const int BLOCK_SIZE = 2000;
int n, s;
ii start, target, d[N][N];
bool vis[N][N];
char a[N][N];
int mtime[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
void addstep(ii &a) {
a.se += 1;
if (a.se == s) a.fi ++, a.se = 0;
}
bool cmp2(ii a, int b) {
if (a.fi != b) return a.fi < b;
return a.se < s;
}
bool check(int x) {
if (x >= mtime[start.fi][start.se]) return 0;
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++) d[i][j] = {2e9, 0};
queue <ii> h;
h.push(start);
d[start.fi][start.se] = {x + 1, 0};
vis[start.fi][start.se] = 1;
while (!h.empty()) {
ii e = h.front(); h.pop();
int x = e.fi, y = e.se;
ii res = d[x][y];
addstep(res);
for (int j = 0; j <= 3; j ++) {
int u = x + dx[j];
int v = y + dy[j];
if (a[u][v] == 'G' && cmp2(res, mtime[u][v]) && !vis[u][v]) {
h.push({u, v});
d[u][v] = res;
vis[u][v] = 1;
}
}
}
return d[target.fi][target.se].fi != 2e9;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s;
queue <ii> bee;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++) {
cin >> a[i][j];
mtime[i][j] = 2e9;
if (a[i][j] == 'H')
bee.push({i, j}), mtime[i][j] = 0;
if (a[i][j] == 'M')
a[i][j] = 'G', start = {i, j};
if (a[i][j] == 'D')
target = {i, j};
}
while (!bee.empty()) {
ii e = bee.front(); bee.pop();
int x = e.fi, y = e.se;
int res = mtime[x][y] + 1;
for (int j = 0; j <= 3; j ++) {
int u = x + dx[j];
int v = y + dy[j];
if (a[u][v] == 'G' && mtime[u][v] > res) {
bee.push({u, v});
mtime[u][v] = res;
}
}
}
a[target.fi][target.se] = 'G';
int l = 0, r = 1e9;
while (l < r) {
int mid = l + (r - l + 1) / 2;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << (check(l) ? l : -1) << '\n';
}
/*
/\_/\ zzZ
(= -_-)
/ >u >u
*/
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |