Submission #1079773

# Submission time Handle Problem Language Result Execution time Memory
1079773 2024-08-29T01:12:15 Z juicy Mecho (IOI09_mecho) C++17
100 / 100
154 ms 6840 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
 
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
 
int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);  
 
  int n, k; cin >> n >> k;
  vector a(n, vector<char>(n));
  vector d(n, vector<int>(n, -1));
  queue<array<int, 2>> q;
  array<int, 2> s{}, t{};
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'H') {
        q.push({i, j});
        d[i][j] = 0;
      } else if (a[i][j] == 'M') {
        s = {i, j};
      } else if (a[i][j] == 'D') {
        t = {i, j};
      }
    }
  }
  auto inside = [&](int i, int j) {
    return 0 <= i && i < n && 0 <= j && j < n;
  };
  while (q.size()) {
    auto [i, j] = q.front(); q.pop();
    for (int dr = 0; dr < 4; ++dr) {
      int u = i + dx[dr], v = j + dy[dr];
      if (inside(u, v) && a[u][v] != 'T' && a[u][v] != 'D' && d[u][v] == -1) {
        d[u][v] = d[i][j] + 1;
        q.push({u, v});
      }
    }
  } 
  auto check = [&](int md) -> bool {
    vector c(n, vector<int>(n, -1));
    queue<array<int, 2>>().swap(q);
    q.push(s);
    c[s[0]][s[1]] = 0;
    while (q.size()) {
      auto [i, j] = q.front(); q.pop();
      if (tie(i, j) == tie(t[0], t[1])) {
        return 1;
      }
      if (md + c[i][j] / k >= d[i][j]) {
        continue;
      }
      for (int dr = 0; dr < 4; ++dr) {
        int u = i + dx[dr], v = j + dy[dr];
        if (inside(u, v) && a[u][v] != 'T' && c[u][v] == -1) {
          c[u][v] = c[i][j] + 1; 
          q.push({u, v});
        }
      }
    }
    return 0;
  };
  int l = 0, r = n * n, res = -1;
  while (l <= r) {
    int md = (l + r) / 2;
    if (check(md)) {
      res = md;
      l = md + 1;
    } else {
      r = md - 1;
    }
  } 
  cout << res;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 452 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 94 ms 6228 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 5 ms 1680 KB Output is correct
34 Correct 5 ms 1612 KB Output is correct
35 Correct 15 ms 1572 KB Output is correct
36 Correct 7 ms 1948 KB Output is correct
37 Correct 6 ms 1884 KB Output is correct
38 Correct 25 ms 1924 KB Output is correct
39 Correct 9 ms 2520 KB Output is correct
40 Correct 8 ms 2252 KB Output is correct
41 Correct 26 ms 2268 KB Output is correct
42 Correct 10 ms 2956 KB Output is correct
43 Correct 10 ms 2688 KB Output is correct
44 Correct 35 ms 2700 KB Output is correct
45 Correct 13 ms 3516 KB Output is correct
46 Correct 15 ms 3204 KB Output is correct
47 Correct 39 ms 3164 KB Output is correct
48 Correct 14 ms 4040 KB Output is correct
49 Correct 14 ms 3756 KB Output is correct
50 Correct 46 ms 3700 KB Output is correct
51 Correct 17 ms 4668 KB Output is correct
52 Correct 21 ms 4304 KB Output is correct
53 Correct 58 ms 4232 KB Output is correct
54 Correct 21 ms 5380 KB Output is correct
55 Correct 19 ms 4900 KB Output is correct
56 Correct 67 ms 4908 KB Output is correct
57 Correct 26 ms 6076 KB Output is correct
58 Correct 22 ms 5524 KB Output is correct
59 Correct 85 ms 5496 KB Output is correct
60 Correct 27 ms 6840 KB Output is correct
61 Correct 24 ms 6216 KB Output is correct
62 Correct 90 ms 6180 KB Output is correct
63 Correct 93 ms 6180 KB Output is correct
64 Correct 154 ms 6180 KB Output is correct
65 Correct 120 ms 6208 KB Output is correct
66 Correct 102 ms 6216 KB Output is correct
67 Correct 95 ms 6180 KB Output is correct
68 Correct 50 ms 6220 KB Output is correct
69 Correct 56 ms 6252 KB Output is correct
70 Correct 39 ms 6220 KB Output is correct
71 Correct 43 ms 6204 KB Output is correct
72 Correct 39 ms 6200 KB Output is correct
73 Correct 35 ms 6212 KB Output is correct
74 Correct 52 ms 6256 KB Output is correct
75 Correct 59 ms 6236 KB Output is correct
76 Correct 54 ms 6232 KB Output is correct
77 Correct 51 ms 6240 KB Output is correct
78 Correct 70 ms 6232 KB Output is correct
79 Correct 52 ms 6248 KB Output is correct
80 Correct 54 ms 6196 KB Output is correct
81 Correct 58 ms 6224 KB Output is correct
82 Correct 54 ms 6200 KB Output is correct
83 Correct 63 ms 6268 KB Output is correct
84 Correct 61 ms 6292 KB Output is correct
85 Correct 66 ms 6460 KB Output is correct
86 Correct 60 ms 6220 KB Output is correct
87 Correct 62 ms 6228 KB Output is correct
88 Correct 67 ms 6224 KB Output is correct
89 Correct 65 ms 6156 KB Output is correct
90 Correct 74 ms 6228 KB Output is correct
91 Correct 66 ms 6220 KB Output is correct
92 Correct 72 ms 6220 KB Output is correct