Submission #976720

# Submission time Handle Problem Language Result Execution time Memory
976720 2024-05-07T03:58:18 Z eblabrada Portals (BOI14_portals) C++17
100 / 100
560 ms 112500 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
  #include "algo/debug.h"
#else
  #define debug(...) 42
#endif

using ll = long long;
using db = long double; // or double, if TL is tight
// #define int ll

// pairs
#define mp make_pair
#define f first
#define s second

// vectors
#define sz(v) (int)((v).size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back

const int N = 1000;
const int INF = 1e8;

using T = tuple<int, int, int>;
priority_queue<T, vector<T>, greater<T>> pq;

const int xdir[4] = {1, 0, -1, 0}, ydir[4] = {0, 1, 0, -1};
char a[N + 3][N + 3];
int dis[N + 3][N + 3], pdis[N + 3][N + 3];
vector<pair<int, int>> adj[N + 3][N + 3];
pair<int, int> st, en;
queue<pair<int, int>> q;

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (int i = 0; i <= N + 1; i++) {
    for (int j = 0; j <= N + 1; j++) {
      a[i][j] = '#';
    }
  }

  int n, m; cin >> n >> m;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];

      if (a[i][j] == 'S') st = {i, j};
      if (a[i][j] == 'C') en = {i, j};
    }
  }


  for (int i = 1; i <= n; i++) {
    int last = 0;
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == '#') {
        last = j;
      } else {
        adj[i][j].push_back({i, last + 1});
        for (int d = 0; d < 4; d++) {
          int ni = i + xdir[d], nj = j + ydir[d];
          adj[i][j].push_back({ni, nj});
        }  
      }
    }

    last = m + 1;
    for (int j = m; j >= 1; j--) {
      if (a[i][j] == '#') {
        last = j;
      } else {
        adj[i][j].push_back({i, last - 1});
      }
    }
  }

  for (int j = 1; j <= m; j++) {
    int last = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i][j] == '#') {
        last = i;
      } else {
        adj[i][j].push_back({last + 1, j});
      }
    }

    last = n + 1;
    for (int i = n; i >= 1; i--) {
      if (a[i][j] == '#') {
        last = i;
      } else {
        adj[i][j].push_back({last - 1, j});
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      pdis[i][j] = INF;

      bool found = false;
      for (int d = 0; d < 4; d++) {
        int ni = i + xdir[d], nj = j + ydir[d];
        if (a[ni][nj] == '#') {
          found = true;
        }
      }

      if (found) {
        pdis[i][j] = 0;
        q.push({i, j});
      }
    }
  }

  while (!q.empty()) {
    auto [x, y] = q.front(); q.pop();
    for (auto &[nx, ny]: adj[x][y]) {
      if (pdis[nx][ny] == INF) {
        pdis[nx][ny] = pdis[x][y] + 1;
        q.push({nx, ny});
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dis[i][j] = INF;
    }
  }
  
  pq.push({0, st.f, st.s}); dis[st.f][st.s] = 0;
  while (!pq.empty()) {
    auto [d, x, y] = pq.top(); pq.pop();
    if (pair(x, y) == en) {
      return cout << d << "\n", 0;
    }
    if (dis[x][y] < d) continue;
    for (auto [nx, ny]: adj[x][y]) {
      int newCost = d;
      if (abs(nx - x) == 1 || abs(ny - y) == 1) {
        newCost += 1;
      } else {
        newCost += pdis[x][y] + 1;
      }
      if (dis[nx][ny] > newCost) {
        dis[nx][ny] = newCost;
        pq.push({newCost, nx, ny});
      }
    }
  }

} 
# Verdict Execution time Memory Grader output
1 Correct 8 ms 28292 KB Output is correct
2 Correct 6 ms 28252 KB Output is correct
3 Correct 6 ms 28248 KB Output is correct
4 Correct 7 ms 28308 KB Output is correct
5 Correct 7 ms 28252 KB Output is correct
6 Correct 7 ms 28252 KB Output is correct
7 Correct 7 ms 28252 KB Output is correct
8 Correct 6 ms 28504 KB Output is correct
9 Correct 8 ms 28252 KB Output is correct
10 Correct 6 ms 28252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 28252 KB Output is correct
2 Correct 9 ms 28760 KB Output is correct
3 Correct 7 ms 28252 KB Output is correct
4 Correct 7 ms 28264 KB Output is correct
5 Correct 7 ms 28252 KB Output is correct
6 Correct 7 ms 28252 KB Output is correct
7 Correct 8 ms 28108 KB Output is correct
8 Correct 8 ms 28252 KB Output is correct
9 Correct 7 ms 28508 KB Output is correct
10 Correct 7 ms 28252 KB Output is correct
11 Correct 7 ms 28252 KB Output is correct
12 Correct 7 ms 28252 KB Output is correct
13 Correct 8 ms 28248 KB Output is correct
14 Correct 6 ms 28252 KB Output is correct
15 Correct 8 ms 28248 KB Output is correct
16 Correct 6 ms 28252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 28252 KB Output is correct
2 Correct 6 ms 28252 KB Output is correct
3 Correct 6 ms 28252 KB Output is correct
4 Correct 6 ms 28304 KB Output is correct
5 Correct 16 ms 30352 KB Output is correct
6 Correct 18 ms 30296 KB Output is correct
7 Correct 16 ms 30556 KB Output is correct
8 Correct 15 ms 30552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 28252 KB Output is correct
2 Correct 7 ms 28252 KB Output is correct
3 Correct 6 ms 28292 KB Output is correct
4 Correct 7 ms 28252 KB Output is correct
5 Correct 6 ms 28252 KB Output is correct
6 Correct 6 ms 28252 KB Output is correct
7 Correct 7 ms 28252 KB Output is correct
8 Correct 7 ms 28252 KB Output is correct
9 Correct 7 ms 28252 KB Output is correct
10 Correct 7 ms 28504 KB Output is correct
11 Correct 7 ms 28252 KB Output is correct
12 Correct 7 ms 28288 KB Output is correct
13 Correct 7 ms 28252 KB Output is correct
14 Correct 20 ms 30300 KB Output is correct
15 Correct 18 ms 30532 KB Output is correct
16 Correct 16 ms 30692 KB Output is correct
17 Correct 17 ms 30636 KB Output is correct
18 Correct 18 ms 30812 KB Output is correct
19 Correct 16 ms 31324 KB Output is correct
20 Correct 17 ms 31324 KB Output is correct
21 Correct 17 ms 30448 KB Output is correct
22 Correct 16 ms 30300 KB Output is correct
23 Correct 16 ms 30352 KB Output is correct
24 Correct 18 ms 31320 KB Output is correct
25 Correct 7 ms 28252 KB Output is correct
26 Correct 7 ms 28252 KB Output is correct
27 Correct 7 ms 28248 KB Output is correct
28 Correct 13 ms 30664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 28248 KB Output is correct
2 Correct 6 ms 28252 KB Output is correct
3 Correct 7 ms 28252 KB Output is correct
4 Correct 6 ms 28252 KB Output is correct
5 Correct 6 ms 28252 KB Output is correct
6 Correct 6 ms 28308 KB Output is correct
7 Correct 6 ms 28252 KB Output is correct
8 Correct 7 ms 28252 KB Output is correct
9 Correct 7 ms 28252 KB Output is correct
10 Correct 8 ms 28764 KB Output is correct
11 Correct 7 ms 28252 KB Output is correct
12 Correct 8 ms 28252 KB Output is correct
13 Correct 8 ms 28252 KB Output is correct
14 Correct 16 ms 30452 KB Output is correct
15 Correct 17 ms 30300 KB Output is correct
16 Correct 16 ms 30556 KB Output is correct
17 Correct 16 ms 30556 KB Output is correct
18 Correct 18 ms 30812 KB Output is correct
19 Correct 14 ms 31324 KB Output is correct
20 Correct 18 ms 31468 KB Output is correct
21 Correct 20 ms 30552 KB Output is correct
22 Correct 16 ms 30300 KB Output is correct
23 Correct 17 ms 30556 KB Output is correct
24 Correct 358 ms 93844 KB Output is correct
25 Correct 560 ms 112500 KB Output is correct
26 Correct 295 ms 112468 KB Output is correct
27 Correct 403 ms 112376 KB Output is correct
28 Correct 269 ms 86244 KB Output is correct
29 Correct 306 ms 87440 KB Output is correct
30 Correct 301 ms 87996 KB Output is correct
31 Correct 18 ms 31424 KB Output is correct
32 Correct 512 ms 112328 KB Output is correct
33 Correct 6 ms 28248 KB Output is correct
34 Correct 7 ms 28252 KB Output is correct
35 Correct 378 ms 94192 KB Output is correct
36 Correct 6 ms 28252 KB Output is correct
37 Correct 13 ms 30556 KB Output is correct
38 Correct 209 ms 91544 KB Output is correct
39 Correct 267 ms 81296 KB Output is correct