답안 #943512

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943512 2024-03-11T14:42:44 Z myst6 Mecho (IOI09_mecho) C++17
100 / 100
166 ms 6664 KB
#include <bits/stdc++.h>

using namespace std;

const int inf = 1 << 30;
int di[4] = {1, -1, 0, 0};
int dj[4] = {0, 0, 1, -1};

const int maxn = 800;
char grid[maxn][maxn];
int dist[maxn][maxn];
int dist2[maxn][maxn];

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int N, S;
  cin >> N >> S;
  for (int i=0; i<N; i++) {
    for (int j=0; j<N; j++) {
      cin >> grid[i][j];
      dist[i][j] = inf;
    }
  }
  queue<int> Q;
  for (int i=0; i<N; i++) {
    for (int j=0; j<N; j++) {
      if (grid[i][j] == 'H') {
        dist[i][j] = 0;
        Q.push(i*N+j);
      }
    }
  }
  while (Q.size()) {
    int x = Q.front(); Q.pop();
    int i = x/N, j = x%N;
    for (int k=0; k<4; k++) {
      int i2 = i + di[k];
      int j2 = j + dj[k];
      if (i2 < 0 || j2 < 0 || i2 >= N || j2 >= N) continue;
      if (grid[i2][j2] != 'G' && grid[i2][j2] != 'M') continue;
      if (dist[i2][j2] <= dist[i][j] + 1) continue;
      dist[i2][j2] = dist[i][j] + 1;
      Q.push(i2*N+j2);
    }
  }
  pair<int,int> begin, end;
  for (int i=0; i<N; i++) {
    for (int j=0; j<N; j++) {
      if (grid[i][j] == 'M') {
        begin = {i, j};
      } else if (grid[i][j] == 'D') {
        end = {i, j};
      }
    }
  }
  int lo = 0, hi = dist[begin.first][begin.second] - 1, ans = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    // mid = number of steps the bees have progressed already
    for (int i=0; i<N; i++) {
      for (int j=0; j<N; j++) {
        dist2[i][j] = inf;
      }
    }
    dist2[begin.first][begin.second] = 0;
    Q.push(begin.first*N+begin.second);
    while (Q.size()) {
      auto x = Q.front(); Q.pop();
      int i = x/N, j = x%N;
      for (int k=0; k<4; k++) {
        int i2 = i + di[k];
        int j2 = j + dj[k];
        if (i2 < 0 || j2 < 0 || i2 >= N || j2 >= N) continue;
        if (grid[i2][j2] != 'G' && grid[i2][j2] != 'M' && grid[i2][j2] != 'D') continue;
        if (dist2[i2][j2] <= dist2[i][j] + 1) continue;
        int T = mid + (dist2[i][j] + 1) / S;
        if (dist[i2][j2] <= T) continue;
        dist2[i2][j2] = dist2[i][j] + 1;
        Q.push(i2*N+j2);
      }
    }
    if (dist2[end.first][end.second] == inf) {
      hi = mid - 1;
    } else {
      ans = mid;
      lo = mid + 1;
    }
  }
  cout << ans << "\n";
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 89 ms 6024 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 2648 KB Output is correct
14 Correct 1 ms 4704 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2652 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 1 ms 2504 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 1 ms 4704 KB Output is correct
26 Correct 1 ms 4704 KB Output is correct
27 Correct 1 ms 4700 KB Output is correct
28 Correct 1 ms 4704 KB Output is correct
29 Correct 2 ms 4704 KB Output is correct
30 Correct 1 ms 4700 KB Output is correct
31 Correct 1 ms 4704 KB Output is correct
32 Correct 1 ms 4588 KB Output is correct
33 Correct 5 ms 4960 KB Output is correct
34 Correct 5 ms 4960 KB Output is correct
35 Correct 19 ms 5104 KB Output is correct
36 Correct 6 ms 4952 KB Output is correct
37 Correct 7 ms 4956 KB Output is correct
38 Correct 25 ms 4952 KB Output is correct
39 Correct 7 ms 5212 KB Output is correct
40 Correct 8 ms 5064 KB Output is correct
41 Correct 32 ms 5236 KB Output is correct
42 Correct 11 ms 5208 KB Output is correct
43 Correct 10 ms 5212 KB Output is correct
44 Correct 41 ms 5356 KB Output is correct
45 Correct 11 ms 5212 KB Output is correct
46 Correct 12 ms 5208 KB Output is correct
47 Correct 49 ms 5212 KB Output is correct
48 Correct 12 ms 5468 KB Output is correct
49 Correct 14 ms 5464 KB Output is correct
50 Correct 56 ms 5468 KB Output is correct
51 Correct 18 ms 5976 KB Output is correct
52 Correct 16 ms 5724 KB Output is correct
53 Correct 65 ms 5724 KB Output is correct
54 Correct 16 ms 5976 KB Output is correct
55 Correct 19 ms 5980 KB Output is correct
56 Correct 80 ms 6228 KB Output is correct
57 Correct 20 ms 6180 KB Output is correct
58 Correct 22 ms 6232 KB Output is correct
59 Correct 86 ms 6244 KB Output is correct
60 Correct 21 ms 6480 KB Output is correct
61 Correct 24 ms 6228 KB Output is correct
62 Correct 113 ms 6492 KB Output is correct
63 Correct 100 ms 6396 KB Output is correct
64 Correct 157 ms 6444 KB Output is correct
65 Correct 166 ms 6576 KB Output is correct
66 Correct 107 ms 6276 KB Output is correct
67 Correct 99 ms 6492 KB Output is correct
68 Correct 42 ms 6484 KB Output is correct
69 Correct 38 ms 6492 KB Output is correct
70 Correct 35 ms 6480 KB Output is correct
71 Correct 29 ms 6484 KB Output is correct
72 Correct 30 ms 6384 KB Output is correct
73 Correct 42 ms 6492 KB Output is correct
74 Correct 64 ms 6456 KB Output is correct
75 Correct 57 ms 6660 KB Output is correct
76 Correct 59 ms 6488 KB Output is correct
77 Correct 59 ms 6484 KB Output is correct
78 Correct 67 ms 6480 KB Output is correct
79 Correct 56 ms 6492 KB Output is correct
80 Correct 61 ms 6384 KB Output is correct
81 Correct 62 ms 6492 KB Output is correct
82 Correct 57 ms 6492 KB Output is correct
83 Correct 74 ms 6596 KB Output is correct
84 Correct 63 ms 6256 KB Output is correct
85 Correct 62 ms 6492 KB Output is correct
86 Correct 75 ms 6488 KB Output is correct
87 Correct 65 ms 6480 KB Output is correct
88 Correct 75 ms 6480 KB Output is correct
89 Correct 73 ms 6492 KB Output is correct
90 Correct 86 ms 6428 KB Output is correct
91 Correct 83 ms 6564 KB Output is correct
92 Correct 92 ms 6664 KB Output is correct