Submission #656481

# Submission time Handle Problem Language Result Execution time Memory
656481 2022-11-07T15:16:40 Z 600Mihnea Portals (BOI14_portals) C++17
100 / 100
283 ms 53620 KB
bool home = 0;
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

vector<int> rev(vector<int> v, int ok) {
  if (ok) {
    reverse(v.begin(), v.end());
  }
  return v;
}

const int N = 1000 + 7;
const int INF = (int) 1e9 + 7;
int n;
int m;
int a[N][N];
int r1;
int c1;
int r2;
int c2;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
int dwall[N][N];
int d[N][N];
pair<int, int> cell[N][N][4];

const int D = N;
vector<pair<int, int>> q[D];

signed main() {
  if (home == 0) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  } else {
    freopen ("___input___.txt", "r", stdin);
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dwall[i][j] = d[i][j] = INF;
    }
  }


  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    assert((int) s.size() == m);
    for (int j = 1; j <= m; j++) {
      char ch = s[j - 1];
      assert(ch == '.' || ch == '#' || ch == 'S' || ch == 'C');
      if (ch != '#') {
        a[i][j] = 1;
      }
      if (ch == 'S') {
        assert(r1 == 0 && c1 == 0);
        r1 = i;
        c1 = j;
      }
      if (ch == 'C') {
        assert(r2 == 0 && c2 == 0);
        r2 = i;
        c2 = j;
      }
    }
  }
  vector<int> _on;
  vector<int> _om;
  for (int i = 1; i <= n; i++) {
    _on.push_back(i);
  }
  for (int i = 1; i <= m; i++) {
    _om.push_back(i);
  }
  {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j]) {
          bool is_near_wall = 0;
          for (int k = 0; k < 4; k++) {
            int in = i + dr[k];
            int jn = j + dc[k];
            if (a[in][jn] == 0) {
              is_near_wall = 1;
            }
          }
          if (is_near_wall) {
            dwall[i][j] = 0;
          }
        }
      }
    }
    for (int step = 1; step <= 1; step++) {
      for (int revn = 0; revn <= 1; revn++) {
        for (int revm = 0; revm <= 1; revm++) {
          vector<int> on = rev(_on, revn);
          vector<int> om = rev(_om, revm);
          for (auto &r : on) {
            for (auto &c : om) {
              for (int k = 0; k < 4; k++) {
                int rn = r + dr[k];
                int cn = c + dc[k];
                dwall[r][c] = min(dwall[r][c], 1 + dwall[rn][cn]);
              }
            }
          }
        }
      }
    }
  }

  {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j]) {
          for (int k = 0; k < 4; k++) {
            int in = i + dr[k];
            int jn = j + dc[k];
            if (a[in][jn] == 0) {
              cell[i][j][k] = {i, j};
            }
          }
        }
      }
    }
    for (int revn = 0; revn <= 1; revn++) {
      for (int revm = 0; revm <= 1; revm++) {
        vector<int> on = rev(_on, revn);
        vector<int> om = rev(_om, revm);
        for (auto &r : on) {
          for (auto &c : om) {
            for (int k = 0; k < 4; k++) {
              int rn = r + dr[k];
              int cn = c + dc[k];
              if (a[r][c] == 1 && a[rn][cn] == 1) {
                cell[r][c][k] = cell[rn][cn][k];
              }
            }
          }
        }
      }
    }
  }

  int maxd = 0;
  q[0].push_back({r1, c1});
  d[r1][c1] = 0;

  for (int ds = 0; ds <= maxd; ds++) {
    for (auto &cellx : q[ds % D]) {
      int i = cellx.first;
      int j = cellx.second;
      if (d[i][j] != ds) {
        continue;
      }
      for (int k = 0; k < 4; k++) {
        int in = i + dr[k];
        int jn = j + dc[k];
        if (a[in][jn] && d[i][j] + 1 < d[in][jn]) {
          d[in][jn] = d[i][j] + 1;
          q[d[in][jn] % D].push_back({in, jn});
          maxd = max(maxd, d[in][jn]);
        }
        in = cell[i][j][k].first;
        jn = cell[i][j][k].second;
        if (d[i][j] + dwall[i][j] + 1 < d[in][jn]) {
          d[in][jn] = d[i][j] + dwall[i][j] + 1;
          q[d[in][jn] % D].push_back({in, jn});
          maxd = max(maxd, d[in][jn]);
        }
      }
    }
    q[ds % D].clear();
  }
  cout << d[r2][c2] << "\n";

  return 0;
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:37:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |     freopen ("___input___.txt", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 4 ms 8276 KB Output is correct
3 Correct 4 ms 8276 KB Output is correct
4 Correct 4 ms 8276 KB Output is correct
5 Correct 4 ms 8276 KB Output is correct
6 Correct 4 ms 8276 KB Output is correct
7 Correct 5 ms 8276 KB Output is correct
8 Correct 4 ms 8276 KB Output is correct
9 Correct 4 ms 8276 KB Output is correct
10 Correct 4 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 4 ms 8276 KB Output is correct
3 Correct 4 ms 8276 KB Output is correct
4 Correct 5 ms 8276 KB Output is correct
5 Correct 4 ms 8276 KB Output is correct
6 Correct 4 ms 8300 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 4 ms 8276 KB Output is correct
9 Correct 6 ms 8788 KB Output is correct
10 Correct 5 ms 8788 KB Output is correct
11 Correct 5 ms 8776 KB Output is correct
12 Correct 5 ms 8788 KB Output is correct
13 Correct 5 ms 8788 KB Output is correct
14 Correct 4 ms 8276 KB Output is correct
15 Correct 6 ms 8788 KB Output is correct
16 Correct 4 ms 8276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 5 ms 8276 KB Output is correct
3 Correct 4 ms 8276 KB Output is correct
4 Correct 4 ms 8308 KB Output is correct
5 Correct 15 ms 11220 KB Output is correct
6 Correct 14 ms 11348 KB Output is correct
7 Correct 15 ms 11392 KB Output is correct
8 Correct 11 ms 11092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 4 ms 8272 KB Output is correct
3 Correct 4 ms 8276 KB Output is correct
4 Correct 5 ms 8276 KB Output is correct
5 Correct 4 ms 8276 KB Output is correct
6 Correct 5 ms 8276 KB Output is correct
7 Correct 4 ms 8260 KB Output is correct
8 Correct 4 ms 8276 KB Output is correct
9 Correct 5 ms 8788 KB Output is correct
10 Correct 5 ms 8788 KB Output is correct
11 Correct 5 ms 8788 KB Output is correct
12 Correct 5 ms 8788 KB Output is correct
13 Correct 5 ms 8788 KB Output is correct
14 Correct 14 ms 11220 KB Output is correct
15 Correct 14 ms 11348 KB Output is correct
16 Correct 15 ms 11368 KB Output is correct
17 Correct 13 ms 11348 KB Output is correct
18 Correct 14 ms 11424 KB Output is correct
19 Correct 14 ms 11492 KB Output is correct
20 Correct 14 ms 11532 KB Output is correct
21 Correct 15 ms 11220 KB Output is correct
22 Correct 14 ms 11340 KB Output is correct
23 Correct 18 ms 11340 KB Output is correct
24 Correct 13 ms 11524 KB Output is correct
25 Correct 4 ms 8276 KB Output is correct
26 Correct 5 ms 8788 KB Output is correct
27 Correct 6 ms 8276 KB Output is correct
28 Correct 11 ms 11092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 4 ms 8276 KB Output is correct
3 Correct 7 ms 8280 KB Output is correct
4 Correct 4 ms 8276 KB Output is correct
5 Correct 4 ms 8276 KB Output is correct
6 Correct 4 ms 8276 KB Output is correct
7 Correct 4 ms 8276 KB Output is correct
8 Correct 4 ms 8276 KB Output is correct
9 Correct 5 ms 8796 KB Output is correct
10 Correct 7 ms 8800 KB Output is correct
11 Correct 5 ms 8708 KB Output is correct
12 Correct 5 ms 8800 KB Output is correct
13 Correct 5 ms 8796 KB Output is correct
14 Correct 14 ms 11232 KB Output is correct
15 Correct 14 ms 11324 KB Output is correct
16 Correct 15 ms 11432 KB Output is correct
17 Correct 14 ms 11392 KB Output is correct
18 Correct 15 ms 11464 KB Output is correct
19 Correct 14 ms 11488 KB Output is correct
20 Correct 14 ms 11488 KB Output is correct
21 Correct 15 ms 11232 KB Output is correct
22 Correct 14 ms 11416 KB Output is correct
23 Correct 14 ms 11412 KB Output is correct
24 Correct 226 ms 50676 KB Output is correct
25 Correct 283 ms 52132 KB Output is correct
26 Correct 269 ms 53488 KB Output is correct
27 Correct 258 ms 53196 KB Output is correct
28 Correct 230 ms 44272 KB Output is correct
29 Correct 237 ms 44756 KB Output is correct
30 Correct 259 ms 50284 KB Output is correct
31 Correct 15 ms 11524 KB Output is correct
32 Correct 269 ms 53620 KB Output is correct
33 Correct 5 ms 8276 KB Output is correct
34 Correct 5 ms 8664 KB Output is correct
35 Correct 255 ms 45156 KB Output is correct
36 Correct 4 ms 8276 KB Output is correct
37 Correct 11 ms 11092 KB Output is correct
38 Correct 187 ms 43724 KB Output is correct
39 Correct 172 ms 34032 KB Output is correct