Submission #943512

#TimeUsernameProblemLanguageResultExecution timeMemory
943512myst6Mecho (IOI09_mecho)C++17
100 / 100
166 ms6664 KiB
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; int di[4] = {1, -1, 0, 0}; int dj[4] = {0, 0, 1, -1}; const int maxn = 800; char grid[maxn][maxn]; int dist[maxn][maxn]; int dist2[maxn][maxn]; int main() { cin.tie(0)->sync_with_stdio(0); int N, S; cin >> N >> S; for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { cin >> grid[i][j]; dist[i][j] = inf; } } queue<int> Q; for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { if (grid[i][j] == 'H') { dist[i][j] = 0; Q.push(i*N+j); } } } while (Q.size()) { int x = Q.front(); Q.pop(); int i = x/N, j = x%N; for (int k=0; k<4; k++) { int i2 = i + di[k]; int j2 = j + dj[k]; if (i2 < 0 || j2 < 0 || i2 >= N || j2 >= N) continue; if (grid[i2][j2] != 'G' && grid[i2][j2] != 'M') continue; if (dist[i2][j2] <= dist[i][j] + 1) continue; dist[i2][j2] = dist[i][j] + 1; Q.push(i2*N+j2); } } pair<int,int> begin, end; for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { if (grid[i][j] == 'M') { begin = {i, j}; } else if (grid[i][j] == 'D') { end = {i, j}; } } } int lo = 0, hi = dist[begin.first][begin.second] - 1, ans = -1; while (lo <= hi) { int mid = (lo + hi) / 2; // mid = number of steps the bees have progressed already for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { dist2[i][j] = inf; } } dist2[begin.first][begin.second] = 0; Q.push(begin.first*N+begin.second); while (Q.size()) { auto x = Q.front(); Q.pop(); int i = x/N, j = x%N; for (int k=0; k<4; k++) { int i2 = i + di[k]; int j2 = j + dj[k]; if (i2 < 0 || j2 < 0 || i2 >= N || j2 >= N) continue; if (grid[i2][j2] != 'G' && grid[i2][j2] != 'M' && grid[i2][j2] != 'D') continue; if (dist2[i2][j2] <= dist2[i][j] + 1) continue; int T = mid + (dist2[i][j] + 1) / S; if (dist[i2][j2] <= T) continue; dist2[i2][j2] = dist2[i][j] + 1; Q.push(i2*N+j2); } } if (dist2[end.first][end.second] == inf) { hi = mid - 1; } else { ans = mid; lo = mid + 1; } } cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...