Submission #1301617

#TimeUsernameProblemLanguageResultExecution timeMemory
1301617ericl23302Mecho (IOI09_mecho)C++20
38 / 100
138 ms9004 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; int main() { ios_base::sync_with_stdio(0); cin.tie(0); vector<pii> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int n, s; cin >> n >> s; vector<string> grid(n); for (auto &i : grid) cin >> i; pii start, target; vector<pii> hives, trees; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 'T') trees.emplace_back(i, j); else if (grid[i][j] == 'M') start = {i, j}; else if (grid[i][j] == 'D') target = {i, j}; else if (grid[i][j] == 'H') hives.emplace_back(i, j); } } vector<vector<int>> dist(n, vector<int>(n, 1e9)); queue<pii> q; for (auto &i : hives) { dist[i.first][i.second] = 0; q.push(i); } while (!q.empty()) { pii cur = q.front(); q.pop(); for (pii direction : directions) { pii nxt = {cur.first + direction.first, cur.second + direction.second}; if (nxt.first < 0 || nxt.first >= n || nxt.second < 0 || nxt.second >= n) continue; char nxtC = grid[nxt.first][nxt.second]; if (nxtC == 'T' || nxtC == 'D') continue; if (dist[nxt.first][nxt.second] < 1e9) continue; dist[nxt.first][nxt.second] = dist[cur.first][cur.second] + 1; q.push(nxt); } } auto possible = [&](int shift) { vector<vector<int>> minSteps(n, vector<int>(n, 1e9)); queue<pair<pii, int>> q2; q2.emplace(start, 0); minSteps[start.first][start.second] = 0; while (!q2.empty()) { auto [cur, steps] = q2.front(); q2.pop(); for (pii direction : directions) { pii nxt = {cur.first + direction.first, cur.second + direction.second}; if (nxt.first < 0 || nxt.first >= n || nxt.second < 0 || nxt.second >= n) continue; char nxtC = grid[nxt.first][nxt.second]; if (nxtC == 'T') continue; if (minSteps[nxt.first][nxt.second] < 1e9) continue; if (dist[nxt.first][nxt.second] - shift - steps / s <= 0) continue; minSteps[nxt.first][nxt.second] = steps + 1; q2.emplace(nxt, steps + 1); } } return (minSteps[target.first][target.second] < 1e9); }; int least = 0, most = 1e6, best = -1; while (least <= most) { int mid = (least + most) / 2; if (possible(mid)) best = max(best, mid), least = mid + 1; else most = mid - 1; } cout << best << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...