Submission #446609

#TimeUsernameProblemLanguageResultExecution timeMemory
446609JerryLiu06Mecho (IOI09_mecho)C++17
100 / 100
200 ms6424 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using db = double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<str>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; #define mp make_pair #define f first #define s second #define sz(x) (int)(x).size() #define bg(x) begin(x) #define all(x) bg(x), end(x) #define sor(x) sort(all(x)) #define rsz resize #define ins insert #define ft front() #define bk back() #define pb push_back #define pf push_front #define lb lower_bound #define ub upper_bound #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define F0R(i, a) FOR(i, 0, a) #define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--) #define R0F(i, a) ROF(i, 0, a) #define EACH(a, x) for (auto& a : x) ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); } const int MOD = 1e9 + 7; const int MX = 810; const ll INF = 1e18; const int DIR[4][2] = { {1, 0}, {0, 1}, {-1, 0}, {0, -1} }; int N, S; char G[MX][MX]; int SX, SY, EX, EY; int distToHive[MX][MX], distToStart[MX][MX]; bool valid(int X, int Y) { return X >= 1 && X <= N && Y >= 1 && Y <= N; } bool valid(int tim) { memset(distToStart, -1, sizeof distToStart); distToStart[SX][SY] = 0; queue<pi> Q; Q.push({SX, SY}); while (!Q.empty()) { pi cur = Q.front(); Q.pop(); if (distToHive[cur.f][cur.s] != -1 && distToStart[cur.f][cur.s] / S + tim >= distToHive[cur.f][cur.s]) { continue; } if (cur.f == EX && cur.s == EY) return true; F0R(i, 4) { int nx = cur.f + DIR[i][0]; int ny = cur.s + DIR[i][1]; if (!valid(nx, ny)) continue; if (G[nx][ny] != 'T' && distToStart[nx][ny] == -1) { distToStart[nx][ny] = distToStart[cur.f][cur.s] + 1; Q.push({nx, ny}); } } } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> S; queue<pi> Q; memset(distToHive, -1, sizeof distToHive); FOR(i, 1, N + 1) FOR(j, 1, N + 1) { char C; cin >> C; if (C == 'M') { SX = i, SY = j; } if (C == 'D') { EX = i, EY = j; } if (C == 'H') { Q.push({i, j}); distToHive[i][j] = 0; } G[i][j] = C; } while (!Q.empty()) { pi cur = Q.front(); Q.pop(); F0R(i, 4) { int nx = cur.f + DIR[i][0]; int ny = cur.s + DIR[i][1]; if (!valid(nx, ny)) continue; if (distToHive[nx][ny] == -1 && G[nx][ny] != 'T' && G[nx][ny] != 'D') { distToHive[nx][ny] = distToHive[cur.f][cur.s] + 1; Q.push({nx, ny}); } } } int low = 0; int high = N * N; int res = -1; while (low <= high) { int mid = (low + high) / 2; if (valid(mid)) { low = mid + 1; res = mid; } else { high = mid - 1; } } cout << res; }
#Verdict Execution timeMemoryGrader output
Fetching results...