Submission #1002839

#TimeUsernameProblemLanguageResultExecution timeMemory
1002839TobMecho (IOI09_mecho)C++14
100 / 100
90 ms7496 KiB
#include <bits/stdc++.h> #define ll long long #define F first #define S second #define all(x) x.begin(), x.end() #define pb push_back #define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) using namespace std; typedef pair <ll, ll> pii; const int N = 807, inf = 1e9; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int n, S, stx, sty, enx, eny; char mat[N][N]; int bee[N][N], bio[N][N]; int main () { FIO; cin >> n >> S; vector <pii> v; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> mat[i][j]; if (mat[i][j] == 'M') stx = i, sty = j; if (mat[i][j] == 'D') enx = i, eny = j; if (mat[i][j] == 'H') v.pb({i, j}); } } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) bee[i][j] = inf; for (int i = 0; i <= n+1; i++) bee[i][0] = bee[i][n+1] = bee[0][i] = bee[n+1][i] = -1; queue <pii> q; for (auto it : v) { q.push(it); bee[it.F][it.S] = 0; } while (!q.empty()) { pii fr = q.front(); q.pop(); int x = fr.F, y = fr.S; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (bee[nx][ny] != inf) continue; if (mat[nx][ny] == 'T' || mat[nx][ny] == 'D') continue; bee[nx][ny] = bee[x][y] + 1; q.push({nx, ny}); } } int lo = -1, hi = 1e8; while (!q.empty()) q.pop(); while (lo < hi) { int mid = (lo + hi + 1) / 2; if (mid >= bee[stx][sty]) { hi = mid-1; continue; } memset(bio, -1, sizeof bio); for (int i = 0; i <= n+1; i++) bio[i][0] = bio[i][n+1] = bio[0][i] = bio[n+1][i] = -2; bio[stx][sty] = 0; q.push({stx, sty}); while (!q.empty()) { pii fr = q.front(); q.pop(); int x = fr.F, y = fr.S; int d = bio[x][y]; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (bio[nx][ny] != -1) continue; if (mat[nx][ny] == 'T' || bee[nx][ny] <= mid+(d+1)/S) continue; bio[nx][ny] = d + 1; q.push({nx, ny}); } } if (bio[enx][eny] == -1) hi = mid-1; else lo = mid; } cout << lo << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...