제출 #652762

#제출 시각아이디문제언어결과실행 시간메모리
652762chanhchuong123Mecho (IOI09_mecho)C++14
40 / 100
1089 ms12644 KiB
#include <bits/stdc++.h> using namespace std; #define task "C" #define fi first #define se second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template <typename T1, typename T2> bool mini(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maxi(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } const int dx[] = {-1, 0, 0, +1}; const int dy[] = {0, -1, +1, 0}; const int N = 802; int n, s; char a[N][N]; pair<int, int> st, en; pair<int, int> d[N][N]; pair<int, int> e[N][N]; deque<pair<int, int>> dq; void FirstBfs() { queue<pair<int, int>> q; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (a[i][j] == 'H') { d[i][j] = {0, 0}; q.push({i, j}); } } } while (q.size()) { auto [u, v] = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int x = u + dx[i], y = v + dy[i]; if (x < 1 || x > n) continue; if (y < 1 || y > n) continue; if (a[x][y] != 'T') { if (d[x][y].fi == 1e9) { d[x][y].fi = d[u][v].fi + 1; d[x][y].se = d[u][v].se; q.push({x, y}); } } } } } bool check(int time) { if (time >= d[st.fi][st.se].fi) return false; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) e[i][j] = d[i][j]; } while (dq.size()) dq.pop_back(); e[st.fi][st.se] = {time, s}; dq.push_back(st); while (dq.size()) { auto [u, v] = dq.front(); int cnt = e[u][v].se; int du = e[u][v].fi; dq.pop_front(); int w, _c; if (cnt == s) w = 1, _c = 1; else w = 0, _c = cnt + 1; for (int i = 0; i < 4; i++) { int x = u + dx[i], y = v + dy[i]; if (x < 1 || x > n) continue; if (y < 1 || y > n) continue; if (a[x][y] != 'T') { if (e[x][y].fi > e[u][v].fi + w) { e[x][y].fi = e[u][v].fi + w; e[x][y].se = _c; if (w) dq.push_back({x, y}); else dq.push_front({x, y}); } else if (e[x][y].fi == e[u][v].fi + w) { if (_c == s && e[x][y].se == 0) continue; if (e[x][y].se == 0) { e[x][y].se = _c; if (w) dq.push_back({x, y}); else dq.push_front({x, y}); } else { if (e[x][y].se > _c) { e[x][y].se = _c; if (w) dq.push_back({x, y}); else dq.push_front({x, y}); } } } } } } return e[en.fi][en.se].se > 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(task".inp","r")) { freopen(task".inp","r",stdin); freopen(task".out","w",stdout); } cin >> n >> s; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; if (a[i][j] == 'M') st = make_pair(i, j); if (a[i][j] == 'D') en = make_pair(i, j); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { d[i][j] = {1e9, -1}; } } FirstBfs(); int l = -1, r = 1e9; while (r - l > 1) { int mid = l + r >> 1; if (check(mid)) l = mid; else r = mid; } cout << l; }

컴파일 시 표준 에러 (stderr) 메시지

mecho.cpp: In function 'void FirstBfs()':
mecho.cpp:38:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   38 |     auto [u, v] = q.front();
      |          ^
mecho.cpp: In function 'bool check(int)':
mecho.cpp:65:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   65 |     auto [u, v] = dq.front();
      |          ^
mecho.cpp:67:9: warning: unused variable 'du' [-Wunused-variable]
   67 |     int du = e[u][v].fi;
      |         ^~
mecho.cpp: In function 'int main()':
mecho.cpp:131:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  131 |     int mid = l + r >> 1;
      |               ~~^~~
mecho.cpp:109:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:110:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |     freopen(task".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...