Submission #1012641

#TimeUsernameProblemLanguageResultExecution timeMemory
1012641toast12Mecho (IOI09_mecho)C++14
100 / 100
157 ms6464 KiB
#include <bits/stdc++.h> using namespace std; const int MAXN = 800; int n, s; vector<vector<char>> grid; vector<vector<int>> dist; int honey; bool reachable(int mecho, int bee) { return (((mecho-honey)/s < bee-honey) && mecho != -1); } void bfs(queue<pair<int, int>> &q, vector<vector<int>> &d) { while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); if (x < 1 || y < 1 || x > n || y > n) continue; if (d[x+1][y] == -1 && grid[x+1][y] == '.' && reachable(d[x][y]+1, dist[x+1][y])) { q.push({x+1, y}); d[x+1][y] = d[x][y]+1; } if (d[x-1][y] == -1 && grid[x-1][y] == '.' && reachable(d[x][y]+1, dist[x-1][y])) { q.push({x-1, y}); d[x-1][y] = d[x][y]+1; } if (d[x][y+1] == -1 && grid[x][y+1] == '.' && reachable(d[x][y]+1, dist[x][y+1])) { q.push({x, y+1}); d[x][y+1] = d[x][y]+1; } if (d[x][y-1] == -1 && grid[x][y-1] == '.' && reachable(d[x][y]+1, dist[x][y-1])) { q.push({x, y-1}); d[x][y-1] = d[x][y]+1; } } } int main() { // ios_base::sync_with_stdio(0); // cin.tie(NULL); cin >> n >> s; grid.resize(n+2, vector<char>(n+2, '.')); pair<int, int> start, end; queue<pair<int, int>> q; dist.resize(n+2, vector<int>(n+2, -1)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> grid[i][j]; if (grid[i][j] == 'G') grid[i][j] = '.'; else if (grid[i][j] == 'T') grid[i][j] = 'X'; else if (grid[i][j] == 'M') { start = {i, j}; grid[i][j] = '.'; } else if (grid[i][j] == 'D') { end = {i, j}; grid[i][j] = 'X'; } else { q.push({i, j}); dist[i][j] = 0; } } } // distance for bees while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); if (x < 1 || y < 1 || x > n || y > n) continue; if (dist[x+1][y] == -1 && grid[x+1][y] == '.') { q.push({x+1, y}); dist[x+1][y] = dist[x][y]+1; } if (dist[x-1][y] == -1 && grid[x-1][y] == '.') { q.push({x-1, y}); dist[x-1][y] = dist[x][y]+1; } if (dist[x][y+1] == -1 && grid[x][y+1] == '.') { q.push({x, y+1}); dist[x][y+1] = dist[x][y]+1; } if (dist[x][y-1] == -1 && grid[x][y-1] == '.') { q.push({x, y-1}); dist[x][y-1] = dist[x][y]+1; } } int lo = 0, hi = n*n; int ans = -1; while (lo <= hi) { int mid = (lo+hi)/2; // cout << mid << '\n'; vector<vector<int>> d(n+2, vector<int>(n+2, -1)); d[start.first][start.second] = mid; honey = mid; q.push(start); if (dist[start.first][start.second] <= mid) q.pop(); bfs(q, d); bool poss = reachable(d[end.first-1][end.second], dist[end.first-1][end.second]); poss |= reachable(d[end.first+1][end.second], dist[end.first+1][end.second]); poss |= reachable(d[end.first][end.second-1], dist[end.first][end.second-1]); poss |= reachable(d[end.first][end.second+1], dist[end.first][end.second+1]); if (poss) { lo = mid+1; ans = mid; } else { hi = mid-1; } } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...