Submission #815668

#TimeUsernameProblemLanguageResultExecution timeMemory
815668Mustela_ErmineaMecho (IOI09_mecho)C++14
29 / 100
153 ms6540 KiB
#include <iostream> #include <vector> #include <queue> #define pii pair<int, int> using namespace std; int N, S; int mR, mC, dR, dC; char grid[801][801]; int minHiveDist[801][801], minBearDist[801][801]; const int dx[4] = { -1, 1, 0, 0 }; const int dy[4] = { 0, 0, -1, 1 }; bool isValid(int r, int c) { return 0 <= r && r < N && 0 <= c && c < N; } void bfs(vector<pii> initialCoords, int minDist[801][801]) { queue<pair<pii, int>> q; vector<vector<bool>> visited(801, vector<bool>(801)); for (pii p : initialCoords) { q.push({ p, 0 }); visited[p.first][p.second] = true; } while (!q.empty()) { pii loc = q.front().first; int dist = q.front().second; q.pop(); minDist[loc.first][loc.second] = dist; for (int i = 0; i < 4; i++) { int r = loc.first + dx[i], c = loc.second + dy[i]; if (isValid(r, c) && !visited[r][c] && grid[r][c] != 'T') { visited[r][c] = true; q.push({ {r, c}, dist + 1 }); } } } } vector<vector<bool>> visited; void bfs2(int startR, int startC, int wait) { visited.clear(); visited.resize(N, vector<bool>(N)); queue<pair<pii, int>> q; q.push({ {startR, startC}, 0 }); visited[startR][startC] = true; while (!q.empty()) { pii loc = q.front().first; int dist = q.front().second; q.pop(); minBearDist[loc.first][loc.second] = dist; for (int i = 0; i < 4; i++) { int r = loc.first + dx[i], c = loc.second + dy[i]; if (isValid(r, c) && !visited[r][c] && grid[r][c] != 'T') { if (dist / S + wait < minHiveDist[r][c]) { visited[r][c] = true; q.push({ {r, c}, dist + 1 }); } } } } } bool works(int wait) { bfs2(mR, mC, wait); return visited[dR][dC]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> S; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) cin >> grid[i][j]; vector<pii> hives; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (grid[i][j] == 'H') hives.push_back({ i, j }); if (grid[i][j] == 'M') { mR = i; mC = j; } if (grid[i][j] == 'D') { dR = i; dC = j; } } } bfs(hives, minHiveDist); minHiveDist[dR][dC] = (int)1e9; int low = 0, high = N * N; while (low < high) { int mid = (low + high + 1) / 2; if (works(mid)) low = mid; else high = mid - 1; } cout << low << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...