Submission #728720

# Submission time Handle Problem Language Result Execution time Memory
728720 2023-04-23T00:53:56 Z asdfgrace Mecho (IOI09_mecho) C++17
100 / 100
198 ms 4156 KB
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) //x
#define A(x) DEBUG(assert(x))
#define PRINT(x) DEBUG(cerr << x)
#define PV(x) DEBUG(cerr << #x << " = " << x << endl)
#define PAR(x) DEBUG(PRINT(#x << " = { "); for (auto y : x) PRINT(y << ' '); PRINT("}\n");)
#define PAR2(x) DEBUG(PRINT(#x << " = { "); for (auto [y, z] : x) PRINT(y << ',' << z << "  "); PRINT("}\n");)
using i64 = int64_t;
const int INF = 1000000007; //998244353;

struct S {
  int n, k;
  vector<string> a;
  vector<pair<int, int>> hives;
  pair<int, int> origin, dest;
  vector<vector<int>> bdist;
  const array<int, 4> dx = {1, -1, 0, 0};
  const array<int, 4> dy = {0, 0, 1, -1};

  void run() {
    cin >> n >> k;
    a.resize(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      for (int j = 0; j < n; ++j) {
        if (a[i][j] == 'H') {
          hives.push_back({i, j});
        } else if (a[i][j] == 'M') {
          origin = {i, j};
        } else if (a[i][j] == 'D') {
          dest = {i, j};
        }
      }
    }
  }
  
  void bfs1() {
    queue<array<int, 3>> q;
    vector<vector<bool>> visited(n, vector<bool>(n, false));
    bdist.resize(n, vector<int>(n, INF));
    for (auto p : hives) {
      q.push({p.first, p.second, 0});
      bdist[p.first][p.second] = 0;
      visited[p.first][p.second] = true;
    }
    while (!q.empty()) {
      int x = q.front()[0];
      int y = q.front()[1];
      int d = q.front()[2];
      q.pop();
      for (int i = 0; i < 4; ++i) {
        int x2 = x + dx[i];
        int y2 = y + dy[i];
        if (ok(x2, y2) && !visited[x2][y2] && a[x2][y2] != 'D') {
          q.push({x2, y2, d + 1});
          visited[x2][y2] = true;
          bdist[x2][y2] = d + 1;
        }
      }
    }
    DEBUG(
    for (auto arr : bdist) {
      PAR(arr);
    }
    )
    PRINT('\n');
  }
  
  void bs() {
    if (!safe(0)) {
      cout << -1 << '\n';
      return;
    }
    int h = n * n, l = 0;
    while (h - 1 > l) {
      int m = (h + l) / 2;
      if (safe(m)) l = m;
      else h = m - 1;
    }
    cout << (safe(h) ? h : l) << '\n';
  }
  
  bool safe(int t) {
    PV(t);
    queue<array<int, 3>> q;
    vector<vector<bool>> visited(n, vector<bool>(n, false));
    q.push({origin.first, origin.second, 0});
    visited[origin.first][origin.second] = true;
    if (bdist[origin.first][origin.second] <= t) return false;
    while (!q.empty()) {
      int x = q.front()[0];
      int y = q.front()[1];
      int d = q.front()[2];
      q.pop();
      for (int i = 0; i < 4; ++i) {
        int x2 = x + dx[i];
        int y2 = y + dy[i];
        if (ok(x2, y2) && !visited[x2][y2] && a[x2][y2] != 'H'
          && (d + 1) / k + t < bdist[x2][y2]) {
          q.push({x2, y2, d + 1});
          visited[x2][y2] = true;
        }
      }
    }
    DEBUG(
    for (auto arr : visited) {
      PAR(arr);
    }
    )
    PRINT('\n');
    return visited[dest.first][dest.second];
  }
  
  bool ok(int x, int y) {
    return x >= 0 && y >= 0 && x < n && y < n && a[x][y] != 'T';
  }
};

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  auto sol = make_unique<S>();
  sol->run();
  sol->bfs1();
  sol->bs();
}


# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 107 ms 3820 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 6 ms 980 KB Output is correct
34 Correct 8 ms 980 KB Output is correct
35 Correct 28 ms 980 KB Output is correct
36 Correct 6 ms 1108 KB Output is correct
37 Correct 7 ms 1108 KB Output is correct
38 Correct 30 ms 1108 KB Output is correct
39 Correct 7 ms 1364 KB Output is correct
40 Correct 9 ms 1364 KB Output is correct
41 Correct 43 ms 1388 KB Output is correct
42 Correct 10 ms 1656 KB Output is correct
43 Correct 12 ms 1648 KB Output is correct
44 Correct 58 ms 1620 KB Output is correct
45 Correct 10 ms 1844 KB Output is correct
46 Correct 14 ms 1920 KB Output is correct
47 Correct 56 ms 1876 KB Output is correct
48 Correct 12 ms 2232 KB Output is correct
49 Correct 15 ms 2132 KB Output is correct
50 Correct 69 ms 2132 KB Output is correct
51 Correct 18 ms 2516 KB Output is correct
52 Correct 24 ms 2516 KB Output is correct
53 Correct 96 ms 2552 KB Output is correct
54 Correct 19 ms 2772 KB Output is correct
55 Correct 22 ms 2896 KB Output is correct
56 Correct 110 ms 2900 KB Output is correct
57 Correct 18 ms 3156 KB Output is correct
58 Correct 25 ms 3156 KB Output is correct
59 Correct 129 ms 3284 KB Output is correct
60 Correct 21 ms 3668 KB Output is correct
61 Correct 31 ms 3668 KB Output is correct
62 Correct 139 ms 3668 KB Output is correct
63 Correct 114 ms 3668 KB Output is correct
64 Correct 171 ms 3668 KB Output is correct
65 Correct 198 ms 3668 KB Output is correct
66 Correct 148 ms 3676 KB Output is correct
67 Correct 30 ms 3668 KB Output is correct
68 Correct 51 ms 3668 KB Output is correct
69 Correct 58 ms 3668 KB Output is correct
70 Correct 43 ms 3632 KB Output is correct
71 Correct 41 ms 3708 KB Output is correct
72 Correct 43 ms 3692 KB Output is correct
73 Correct 47 ms 4052 KB Output is correct
74 Correct 75 ms 4052 KB Output is correct
75 Correct 71 ms 4096 KB Output is correct
76 Correct 69 ms 4112 KB Output is correct
77 Correct 63 ms 4052 KB Output is correct
78 Correct 28 ms 4052 KB Output is correct
79 Correct 67 ms 4052 KB Output is correct
80 Correct 80 ms 4052 KB Output is correct
81 Correct 81 ms 4052 KB Output is correct
82 Correct 75 ms 4156 KB Output is correct
83 Correct 83 ms 4000 KB Output is correct
84 Correct 88 ms 4052 KB Output is correct
85 Correct 83 ms 4008 KB Output is correct
86 Correct 85 ms 3980 KB Output is correct
87 Correct 120 ms 3980 KB Output is correct
88 Correct 84 ms 3904 KB Output is correct
89 Correct 97 ms 3912 KB Output is correct
90 Correct 94 ms 3796 KB Output is correct
91 Correct 118 ms 3840 KB Output is correct
92 Correct 96 ms 3796 KB Output is correct