Submission #800634

# Submission time Handle Problem Language Result Execution time Memory
800634 2023-08-01T17:10:30 Z tch1cherin Portals (BOI14_portals) C++17
100 / 100
289 ms 27696 KB
#include <bits/stdc++.h>
using namespace std;

template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<string> S(N + 2, string(M + 2, '#'));
  for (int i = 1; i <= N; i++) {
    string v;
    cin >> v;
    for (int j = 1; j <= M; j++) {
      S[i][j] = v[j - 1];
    }
  }
  N += 2, M += 2;
  vector<vector<int>> up(N, vector<int>(M, -1)), down(N, vector<int>(M, -1));
  vector<vector<int>> left(N, vector<int>(M, -1)), right(N, vector<int>(M, -1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i][j] != '#') {
        up[i][j] = up[i - 1][j] + 1;
        left[i][j] = left[i][j - 1] + 1;
      }
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    for (int j = M - 1; j >= 0; j--) {
      if (S[i][j] != '#') {
        down[i][j] = down[i + 1][j] + 1;
        right[i][j] = right[i][j + 1] + 1;
      }
    }
  }
  queue<pair<int, int>> q;
  vector<vector<int>> nearest_wall(N, vector<int>(M, INT_MAX));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i][j] == '#') {
        q.push({i, j});
        nearest_wall[i][j] = 0;
      }
    }
  }
  while (!q.empty()) {
    auto [x, y] = q.front();
    q.pop();
    for (auto [dx, dy] : vector<pair<int, int>> {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}) {
      int i = x + dx, j = y + dy;
      if (i >= 0 && j >= 0 && i < N && j < M && nearest_wall[i][j] == INT_MAX) {
        nearest_wall[i][j] = nearest_wall[x][y] + 1;
        q.push({i, j});
      }
    }
  }
  min_heap<tuple<int, int, int>> pq;
  vector<vector<int>> dist(N, vector<int>(M, INT_MAX));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i][j] == 'S') {
        pq.push({dist[i][j] = 0, i, j});
      }
    }
  }
  while (!pq.empty()) {
    auto [d, x, y] = pq.top();
    pq.pop();
    if (d == dist[x][y]) {
      for (auto [dx, dy] : vector<pair<int, int>> {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, 
                          {-up[x][y], 0}, {down[x][y], 0}, {0, -left[x][y]}, {0, right[x][y]}}) {
        int w = max(abs(dx), abs(dy)) == 1 ? 1 : nearest_wall[x][y];
        int i = x + dx, j = y + dy;
        if (i >= 0 && i < N && j >= 0 && j < M && S[i][j] != '#' && dist[i][j] > dist[x][y] + w) {
          pq.push({dist[i][j] = dist[x][y] + w, i, j});
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i][j] == 'C') {
        cout << dist[i][j] << "\n";
      }
    }
  }
}
# 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 1 ms 212 KB Output is correct
5 Correct 0 ms 320 KB Output is correct
6 Correct 0 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 224 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 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 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 212 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# 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 1 ms 212 KB Output is correct
5 Correct 6 ms 1364 KB Output is correct
6 Correct 7 ms 1364 KB Output is correct
7 Correct 7 ms 1464 KB Output is correct
8 Correct 4 ms 1364 KB Output is correct
# 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 0 ms 212 KB Output is correct
6 Correct 0 ms 316 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 316 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 6 ms 1364 KB Output is correct
15 Correct 8 ms 1432 KB Output is correct
16 Correct 8 ms 1364 KB Output is correct
17 Correct 8 ms 1364 KB Output is correct
18 Correct 8 ms 1360 KB Output is correct
19 Correct 9 ms 1364 KB Output is correct
20 Correct 8 ms 1460 KB Output is correct
21 Correct 6 ms 1364 KB Output is correct
22 Correct 7 ms 1408 KB Output is correct
23 Correct 7 ms 1364 KB Output is correct
24 Correct 8 ms 1428 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 4 ms 1352 KB Output is correct
# 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 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 324 KB Output is correct
14 Correct 6 ms 1364 KB Output is correct
15 Correct 8 ms 1364 KB Output is correct
16 Correct 7 ms 1464 KB Output is correct
17 Correct 7 ms 1364 KB Output is correct
18 Correct 8 ms 1452 KB Output is correct
19 Correct 9 ms 1456 KB Output is correct
20 Correct 8 ms 1364 KB Output is correct
21 Correct 6 ms 1364 KB Output is correct
22 Correct 6 ms 1448 KB Output is correct
23 Correct 7 ms 1448 KB Output is correct
24 Correct 188 ms 26296 KB Output is correct
25 Correct 289 ms 26388 KB Output is correct
26 Correct 258 ms 26288 KB Output is correct
27 Correct 245 ms 26324 KB Output is correct
28 Correct 141 ms 26912 KB Output is correct
29 Correct 160 ms 26956 KB Output is correct
30 Correct 190 ms 26956 KB Output is correct
31 Correct 8 ms 1364 KB Output is correct
32 Correct 248 ms 26144 KB Output is correct
33 Correct 0 ms 320 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 209 ms 26188 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 5 ms 1492 KB Output is correct
38 Correct 104 ms 27696 KB Output is correct
39 Correct 138 ms 26324 KB Output is correct