# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
722770 | TAhmed33 | Mecho (IOI09_mecho) | C++98 | 211 ms | 8928 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, s, beginx, beginy, endx, endy;
char arr[801][801];
queue <pair <int, int>> cur;
int distbee[801][801] = {}; bool vis[801][801];
int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0};
inline bool okbee (int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= n && (arr[x][y] == 'G' || arr[x][y] == 'M' || arr[x][y] == 'H')); }
void beefs () {
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) distbee[i][j] = 1e9;
memset(vis, false, sizeof(vis)); int cnt = 0; while (!cur.empty()) {
int u = cur.size(); while (u--) {
auto k = cur.front(); cur.pop(); if (vis[k.first][k.second]) continue; vis[k.first][k.second] = 1; distbee[k.first][k.second] = cnt;
for (int i = 0; i < 4; i++) {
if (okbee(k.first + dx[i], k.second + dy[i]) && !vis[k.first + dx[i]][k.second + dy[i]]) cur.push({k.first + dx[i], k.second + dy[i]});
}
}
cnt++;
}
}
inline bool okmecho (int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= n && (arr[x][y] == 'G' || arr[x][y] == 'M' || arr[x][y] == 'D')); }
bool check (int x) {
if (x * s >= distbee[beginx][beginy]) return false;
queue <pair <int, int>> cur; bool vis[n + 1][n + 1]; memset(vis, false, sizeof(vis)); cur.push({beginx, beginy});
int cnt = x * s;
while (!cur.empty()) {
auto u = cur.size(); while (u--) {
auto k = cur.front(); cur.pop(); if (vis[k.first][k.second]) continue; vis[k.first][k.second] = 1;
if (cnt >= distbee[k.first][k.second]) continue;
if (k.first == endx && k.second == endy) return true;
for (int i = 0; i < 4; i++) {
if (okmecho(k.first + dx[i], k.second + dy[i]) && !vis[k.first + dx[i]][k.second + dy[i]]) cur.push({k.first + dx[i], k.second + dy[i]});
}
}
cnt++;
}
return false;
}
signed main () {
cin >> n >> s; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> arr[i][j];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (arr[i][j] == 'M') {
beginx = i; beginy = j;
} else if (arr[i][j] == 'D') {
endx = i; endy = j;
}
}
}
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (arr[i][j] == 'H') cur.push({i, j});
beefs();
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) distbee[i][j] *= s;
int l = 0, r = 1e9, mid, ans = -1; while (l <= r) {
mid = (l + r) >> 1; if (check(mid)) {
l = mid + 1;
ans = mid;
} else r = mid - 1;
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |