# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
661301 | chanhchuong123 | Mecho (IOI09_mecho) | C++17 | 187 ms | 6236 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;
#define task "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const int N = 800;
int n, S;
char a[N][N];
int bee[N][N];
int bear[N][N];
pair<int, int> st, en;
int ADD;
bool canMove(int dist, int i, int j) {
if (bee[i][j] == -1) return true;
return dist / S < bee[i][j] - ADD;
}
bool check() {
memset(bear, -1, sizeof(bear));
queue<pair<int, int>> q;
bear[st.fi][st.se] = 0;
q.push(st);
while (q.size()) {
int u = q.front().fi;
int v = q.front().se;
q.pop();
for (int i = 0; i < 4; i++) {
int x = u + dx[i], y = v + dy[i];
if (x < 0 || x >= n) continue;
if (y < 0 || y >= n) continue;
if (a[x][y] == 'T' || a[x][y] == 'H') continue;
if (bear[x][y] == -1 && canMove(bear[u][v] + 1, x, y)) {
bear[x][y] = bear[u][v] + 1; q.push(make_pair(x, y));
}
}
}
int u = en.fi, v = en.se;
for (int i = 0; i < 4; i++) {
int x = u + dx[i], y = v + dy[i];
if (x < 0 || x >= n) continue;
if (y < 0 || y >= n) continue;
if (bear[x][y] == -1) continue;
if (a[x][y] == 'T' || a[x][y] == 'H') continue;
if (canMove(bear[x][y], x, y)) return true;
}
return false;
}
main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> S;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
if (a[i][j] == 'M') st = make_pair(i, j);
if (a[i][j] == 'D') en = make_pair(i, j);
}
}
memset(bee, -1, sizeof(bee));
queue<pair<int, int>> q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 'H') {
bee[i][j] = 0;
q.push(make_pair(i, j));
}
}
}
while (q.size()) {
int u = q.front().fi;
int v = q.front().se;
q.pop();
for (int i = 0; i < 4; i++) {
int x = u + dx[i], y = v + dy[i];
if (x < 0 || x >= n) continue;
if (y < 0 || y >= n) continue;
if (a[x][y] == 'T' || a[x][y] == 'D') continue;
if (bee[x][y] == -1) {
q.push(make_pair(x, y));
bee[x][y] = bee[u][v] + 1;
}
}
}
ADD = 0;
if (check() == false) {
cout << "-1";
return 0;
}
int l = 0, r = n * n + n;
while (r - l > 1) {
ADD = l + r >> 1;
if (check()) l = ADD;
else r = ADD;
}
cout << l;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |