제출 #722770

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