제출 #972931

#제출 시각아이디문제언어결과실행 시간메모리
972931jadai007Mecho (IOI09_mecho)C++17
100 / 100
154 ms7048 KiB
#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 timeMemoryGrader output
Fetching results...