# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
972931 | jadai007 | Mecho (IOI09_mecho) | C++17 | 154 ms | 7048 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 810, INF = 1e9 + 7, d[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
char s[MAX_N][MAX_N];
int bee[MAX_N][MAX_N], bear[MAX_N][MAX_N], sx, sy, ex, ey;
queue<pair<int, int>> q;
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);
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= N; j++) {
bee[i][j] = INF;
if(s[i][j] == 'H') {
bee[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()) {
int ux = q.front().first, uy = q.front().second; q.pop();
for(int i = 0; i < 4; i++) {
int vx = ux + d[i][0], vy = uy + d[i][1];
if(vx < 1 || vx > N || vy < 1 || vy > N || s[vx][vy] == 'T' || s[vx][vy] == 'D' || bee[vx][vy] != INF) continue;
bee[vx][vy] = bee[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++) bear[i][j] = INF;
if(bee[sx][sy] > mid) {
q.emplace(sx, sy);
bear[sx][sy] = 0;
}
while(!q.empty()) {
int ux = q.front().first, uy = q.front().second; q.pop();
for(int i = 0; i < 4; i++) {
int vx = ux + d[i][0], vy = uy + d[i][1];
if(vx < 1 || vx > N || vy < 1 || vy > N || s[vx][vy] == 'T' || bear[vx][vy] != INF) continue;
if((bear[ux][uy] + 1) / S >= bee[vx][vy] - mid) continue;
bear[vx][vy] = bear[ux][uy] + 1;
q.emplace(vx, vy);
}
}
if(bear[ex][ey] == INF) r = mid - 1;
else{
l = mid + 1;
ans = mid;
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |