답안 #501941

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
501941 2022-01-04T21:21:56 Z lorenzoferrari Mecho (IOI09_mecho) C++14
84 / 100
178 ms 6744 KB
#include <bits/stdc++.h>
using namespace std;
using LL = long long;

int32_t main() {
  int n, s;
  cin >> n >> s;
  vector<vector<char>> m(n, vector<char> (n));
  int xs, ys;
  int xe, ye;
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
    cin >> m[i][j];
    if (m[i][j] == 'M') xs = i, ys = j;
    if (m[i][j] == 'D') xe = i, ye = j;
  }
  queue<pair<int, int>> Q;
  vector<vector<int>> d(n, vector<int> (n, 1e9));
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
    if (m[i][j] == 'H') {
      d[i][j] = 0;
      Q.push({i, j});
    }
  }
  while (!Q.empty()) {
    auto [x, y] = Q.front();
    Q.pop();
    if (x     && (m[x-1][y  ] == 'G' || m[x-1][y  ] == 'M') && d[x-1][y  ] > d[x][y] + 1) d[x-1][y  ] = d[x][y] + 1, Q.push({x-1, y  });
    if (y     && (m[x  ][y-1] == 'G' || m[x  ][y-1] == 'M') && d[x  ][y-1] > d[x][y] + 1) d[x  ][y-1] = d[x][y] + 1, Q.push({x  , y-1});
    if (x<n-1 && (m[x+1][y  ] == 'G' || m[x+1][y  ] == 'M') && d[x+1][y  ] > d[x][y] + 1) d[x+1][y  ] = d[x][y] + 1, Q.push({x+1, y  });
    if (y<n-1 && (m[x  ][y+1] == 'G' || m[x  ][y+1] == 'M') && d[x  ][y+1] > d[x][y] + 1) d[x  ][y+1] = d[x][y] + 1, Q.push({x  , y+1});
  }
  auto psb = [&](int delay) {
    // bfs from mecho's location
    // it's possible iff dist_from_mecho[i][j]/s + delay < d[i][j]
    queue<pair<int, int>> Q;
    vector<vector<int>> dm(n, vector<int> (n, 1e9));
    Q.push({xs, ys});
    dm[xs][ys] = 0;
    while (!Q.empty()) {
      auto [x, y] = Q.front();
      Q.pop();
      if (x     && m[x-1][y  ] != 'T' && dm[x-1][y  ] > dm[x][y] + 1 && delay + (dm[x][y] + 1) / s < d[x-1][y  ]) dm[x-1][y  ] = dm[x][y] + 1, Q.push({x-1, y  });
      if (y     && m[x  ][y-1] != 'T' && dm[x  ][y-1] > dm[x][y] + 1 && delay + (dm[x][y] + 1) / s < d[x  ][y-1]) dm[x  ][y-1] = dm[x][y] + 1, Q.push({x  , y-1});
      if (x<n-1 && m[x+1][y  ] != 'T' && dm[x+1][y  ] > dm[x][y] + 1 && delay + (dm[x][y] + 1) / s < d[x+1][y  ]) dm[x+1][y  ] = dm[x][y] + 1, Q.push({x+1, y  });
      if (y<n-1 && m[x  ][y+1] != 'T' && dm[x  ][y+1] > dm[x][y] + 1 && delay + (dm[x][y] + 1) / s < d[x  ][y+1]) dm[x  ][y+1] = dm[x][y] + 1, Q.push({x  , y+1});
    }
    return dm[xe][ye] != 1e9;
  };
  int l = 0, r = d[xs][ys];
  while (r - l > 1) {
    int mid = (r + l) / 2;
    if (psb(mid))
      l = mid;
    else
      r = mid;
  }
  cout << l << "\n";
}

Compilation message

mecho.cpp: In function 'int32_t main()':
mecho.cpp:25:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   25 |     auto [x, y] = Q.front();
      |          ^
mecho.cpp: In lambda function:
mecho.cpp:40:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   40 |       auto [x, y] = Q.front();
      |            ^
mecho.cpp: In function 'int32_t main()':
mecho.cpp:47:21: warning: 'ye' may be used uninitialized in this function [-Wmaybe-uninitialized]
   47 |     return dm[xe][ye] != 1e9;
      |                     ^
mecho.cpp:47:17: warning: 'xe' may be used uninitialized in this function [-Wmaybe-uninitialized]
   47 |     return dm[xe][ye] != 1e9;
      |                 ^
mecho.cpp:49:26: warning: 'ys' may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |   int l = 0, r = d[xs][ys];
      |                          ^
mecho.cpp:49:22: warning: 'xs' may be used uninitialized in this function [-Wmaybe-uninitialized]
   49 |   int l = 0, r = d[xs][ys];
      |                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 89 ms 6668 KB Output is correct
8 Incorrect 1 ms 204 KB Output isn't correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Incorrect 1 ms 332 KB Output isn't correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 292 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 12 ms 1524 KB Output is correct
34 Correct 11 ms 1524 KB Output is correct
35 Correct 21 ms 1528 KB Output is correct
36 Correct 15 ms 1880 KB Output is correct
37 Correct 15 ms 1888 KB Output is correct
38 Correct 28 ms 1840 KB Output is correct
39 Correct 18 ms 2348 KB Output is correct
40 Correct 23 ms 2292 KB Output is correct
41 Correct 46 ms 2268 KB Output is correct
42 Correct 24 ms 2796 KB Output is correct
43 Correct 27 ms 2812 KB Output is correct
44 Correct 48 ms 2944 KB Output is correct
45 Correct 28 ms 3336 KB Output is correct
46 Correct 32 ms 3272 KB Output is correct
47 Correct 51 ms 3300 KB Output is correct
48 Correct 40 ms 3904 KB Output is correct
49 Correct 43 ms 3900 KB Output is correct
50 Correct 66 ms 3860 KB Output is correct
51 Correct 40 ms 4508 KB Output is correct
52 Correct 39 ms 4500 KB Output is correct
53 Correct 90 ms 4472 KB Output is correct
54 Correct 45 ms 5212 KB Output is correct
55 Correct 46 ms 5184 KB Output is correct
56 Correct 124 ms 5164 KB Output is correct
57 Correct 59 ms 5912 KB Output is correct
58 Correct 51 ms 5952 KB Output is correct
59 Correct 117 ms 5892 KB Output is correct
60 Correct 58 ms 6692 KB Output is correct
61 Correct 59 ms 6688 KB Output is correct
62 Correct 131 ms 6644 KB Output is correct
63 Correct 123 ms 6744 KB Output is correct
64 Correct 178 ms 6640 KB Output is correct
65 Correct 177 ms 6640 KB Output is correct
66 Correct 154 ms 6640 KB Output is correct
67 Incorrect 132 ms 6640 KB Output isn't correct
68 Correct 75 ms 6660 KB Output is correct
69 Correct 67 ms 6648 KB Output is correct
70 Correct 70 ms 6668 KB Output is correct
71 Correct 66 ms 6660 KB Output is correct
72 Correct 61 ms 6676 KB Output is correct
73 Correct 57 ms 6564 KB Output is correct
74 Correct 83 ms 6664 KB Output is correct
75 Correct 90 ms 6696 KB Output is correct
76 Correct 87 ms 6668 KB Output is correct
77 Correct 84 ms 6668 KB Output is correct
78 Incorrect 85 ms 6672 KB Output isn't correct
79 Correct 77 ms 6676 KB Output is correct
80 Correct 80 ms 6660 KB Output is correct
81 Correct 88 ms 6676 KB Output is correct
82 Correct 79 ms 6676 KB Output is correct
83 Correct 87 ms 6648 KB Output is correct
84 Correct 84 ms 6664 KB Output is correct
85 Correct 99 ms 6640 KB Output is correct
86 Correct 107 ms 6668 KB Output is correct
87 Correct 87 ms 6668 KB Output is correct
88 Correct 87 ms 6644 KB Output is correct
89 Correct 109 ms 6672 KB Output is correct
90 Correct 106 ms 6664 KB Output is correct
91 Correct 97 ms 6720 KB Output is correct
92 Correct 100 ms 6648 KB Output is correct