Submission #126532

#TimeUsernameProblemLanguageResultExecution timeMemory
126532khulegubMecho (IOI09_mecho)C++14
100 / 100
150 ms4600 KiB
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define xx first #define yy second using namespace std; typedef long long i64; typedef pair<int,int> pii; int n; int s; char gazar[810][810]; bool vis[810][810]; int beed[810][810]; int mei, mej; bool feasible (int x){ memset(vis, 0, sizeof vis); queue<pii> q; int depth = x * s; q.push(mp(mei, mej)); vis[mei][mej] = 1; while(!q.empty()){ int sz = q.size(); for (int cnt = 0; cnt < sz; cnt++){ pii nw = q.front(); if (gazar[nw.xx][nw.yy] == 'D') return 1; q.pop(); if (nw.xx < n && !vis[nw.xx + 1][nw.yy] && (gazar[nw.xx + 1][nw.yy] == 'D' || (gazar[nw.xx + 1][nw.yy] == 'G' && ((depth+1) / s) < beed[nw.xx + 1][nw.yy]) ) ) q.push(mp(nw.xx + 1, nw.yy)), vis[nw.xx + 1][nw.yy] = 1; if (nw.xx > 1 && !vis[nw.xx - 1][nw.yy] && (gazar[nw.xx - 1][nw.yy] == 'D' || (gazar[nw.xx - 1][nw.yy] == 'G' && ((depth+1) / s) < beed[nw.xx - 1][nw.yy]) ) ) q.push(mp(nw.xx - 1, nw.yy)), vis[nw.xx - 1][nw.yy] = 1; if (nw.yy < n && !vis[nw.xx][nw.yy + 1] && (gazar[nw.xx][nw.yy + 1] == 'D' || (gazar[nw.xx][nw.yy + 1] == 'G' && ((depth+1) / s) < beed[nw.xx][nw.yy + 1]) ) ) q.push(mp(nw.xx, nw.yy + 1)), vis[nw.xx][nw.yy + 1] = 1; if (nw.yy > 1 && !vis[nw.xx][nw.yy - 1] && (gazar[nw.xx][nw.yy - 1] == 'D' || (gazar[nw.xx][nw.yy - 1] == 'G' && ((depth+1) / s) < beed[nw.xx][nw.yy - 1]) ) ) q.push(mp(nw.xx, nw.yy - 1)), vis[nw.xx][nw.yy - 1] = 1; } depth ++; } return 0; } int main (){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); // freopen("in.txt", "r", stdin); cin >> n; cin >> s; for (int i = 1; i <= n; i++){ string tmp; cin >> tmp; // cout << tmp; for (int j = 1; j <= n; j++){ gazar[i][j] = tmp[j - 1]; if (tmp[j - 1] == 'M') mei = i, mej = j; } } queue<pii> q; for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ if (gazar[i][j] == 'H') q.push( mp(i, j) ), vis[i][j] = 1; } } int depth = 0; // cout << "QQQ"; while (!q.empty()){ int sz = q.size(); // cout << sz << ' '; for (int cnt = 0; cnt < sz; cnt++){ pii nw = q.front(); q.pop(); beed[nw.xx][nw.yy] = depth; if (nw.xx < n && !vis[nw.xx + 1][nw.yy] && (gazar[nw.xx + 1][nw.yy] == 'M' || gazar[nw.xx + 1][nw.yy] == 'G') ) q.push(mp(nw.xx + 1, nw.yy)), vis[nw.xx + 1][nw.yy] = 1; if (nw.xx > 1 && !vis[nw.xx - 1][nw.yy] && (gazar[nw.xx - 1][nw.yy] == 'M' || gazar[nw.xx - 1][nw.yy] == 'G') ) q.push(mp(nw.xx - 1, nw.yy)), vis[nw.xx - 1][nw.yy] = 1; if (nw.yy < n && !vis[nw.xx][nw.yy + 1] && (gazar[nw.xx][nw.yy + 1] == 'M' || gazar[nw.xx][nw.yy + 1] == 'G') ) q.push(mp(nw.xx, nw.yy + 1)), vis[nw.xx][nw.yy + 1] = 1; if (nw.yy > 1 && !vis[nw.xx][nw.yy - 1] && (gazar[nw.xx][nw.yy - 1] == 'M' || gazar[nw.xx][nw.yy - 1] == 'G') ) q.push(mp(nw.xx, nw.yy - 1)), vis[nw.xx][nw.yy - 1] = 1; } depth++; } int lo = 0, hi = beed[mei][mej] - 1; while (lo != hi){ int mid = (lo + hi + 1) >> 1; if (feasible(mid)) lo = mid; else hi = mid - 1; } if(feasible(lo)) cout << lo; else cout << -1; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...