답안 #1079764

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1079764 2024-08-29T01:01:30 Z juicy Mecho (IOI09_mecho) C++17
95 / 100
213 ms 6920 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] != 'H' && 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' && a[u][v] != 'H' && 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 85 ms 6852 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 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 Incorrect 1 ms 348 KB Output isn't correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 464 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 5 ms 1560 KB Output is correct
34 Correct 6 ms 1688 KB Output is correct
35 Correct 20 ms 1700 KB Output is correct
36 Correct 9 ms 1884 KB Output is correct
37 Correct 10 ms 1892 KB Output is correct
38 Correct 20 ms 1884 KB Output is correct
39 Correct 11 ms 2268 KB Output is correct
40 Correct 9 ms 2436 KB Output is correct
41 Correct 30 ms 2484 KB Output is correct
42 Correct 9 ms 2700 KB Output is correct
43 Correct 12 ms 2960 KB Output is correct
44 Correct 44 ms 2988 KB Output is correct
45 Correct 13 ms 3160 KB Output is correct
46 Correct 13 ms 3460 KB Output is correct
47 Correct 40 ms 3476 KB Output is correct
48 Correct 25 ms 3708 KB Output is correct
49 Correct 22 ms 4068 KB Output is correct
50 Correct 71 ms 4044 KB Output is correct
51 Correct 21 ms 4308 KB Output is correct
52 Correct 19 ms 4652 KB Output is correct
53 Correct 85 ms 4712 KB Output is correct
54 Correct 29 ms 4856 KB Output is correct
55 Correct 21 ms 5376 KB Output is correct
56 Correct 75 ms 5360 KB Output is correct
57 Correct 25 ms 5552 KB Output is correct
58 Correct 24 ms 6076 KB Output is correct
59 Correct 126 ms 6048 KB Output is correct
60 Correct 27 ms 6228 KB Output is correct
61 Correct 25 ms 6828 KB Output is correct
62 Correct 123 ms 6832 KB Output is correct
63 Correct 99 ms 6828 KB Output is correct
64 Correct 213 ms 6836 KB Output is correct
65 Correct 135 ms 6820 KB Output is correct
66 Correct 119 ms 6808 KB Output is correct
67 Correct 105 ms 6872 KB Output is correct
68 Correct 56 ms 6836 KB Output is correct
69 Correct 56 ms 6844 KB Output is correct
70 Correct 42 ms 6876 KB Output is correct
71 Correct 49 ms 6920 KB Output is correct
72 Correct 39 ms 6864 KB Output is correct
73 Correct 37 ms 6864 KB Output is correct
74 Correct 51 ms 6876 KB Output is correct
75 Correct 73 ms 6852 KB Output is correct
76 Correct 75 ms 6900 KB Output is correct
77 Correct 58 ms 6860 KB Output is correct
78 Correct 75 ms 6872 KB Output is correct
79 Correct 45 ms 6864 KB Output is correct
80 Correct 57 ms 6860 KB Output is correct
81 Correct 79 ms 6824 KB Output is correct
82 Correct 62 ms 6848 KB Output is correct
83 Correct 82 ms 6824 KB Output is correct
84 Correct 80 ms 6828 KB Output is correct
85 Correct 75 ms 6844 KB Output is correct
86 Correct 100 ms 6820 KB Output is correct
87 Correct 68 ms 6880 KB Output is correct
88 Correct 64 ms 6828 KB Output is correct
89 Correct 110 ms 6852 KB Output is correct
90 Correct 90 ms 6832 KB Output is correct
91 Correct 70 ms 6876 KB Output is correct
92 Correct 126 ms 6824 KB Output is correct