Submission #727450

# Submission time Handle Problem Language Result Execution time Memory
727450 2023-04-20T17:13:22 Z _martynas Portals (BOI14_portals) C++11
100 / 100
389 ms 47432 KB
// THE CAKE IS A LIE!
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

const int MOD = 1e9+7;

#define F first
#define S second
#define MP make_pair
#define PB push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

const int MXN = 1005;
const int INF = 1e7;

int n, m, si, sj, fi, fj;
string A[MXN];
bool visited[MXN][MXN];
int to_wall[MXN][MXN];
pii go[4][MXN][MXN];
int dist[MXN][MXN];

const int dirI[] = {-1, 1, 0, 0};
const int dirJ[] = {0, 0, -1, 1};

bool in(int i, int j) {
    return i < n && j < m && i >= 0 && j >= 0;
}

int main() {
    cin >> n >> m;
    for(int i = 0; i < n; i++) cin >> A[i];
    for(int i = 0; i < n; i++)
    for(int j = 0; j < m; j++) {
        if(A[i][j] == 'S') {
            si = i, sj = j;
        }
        if(A[i][j] == 'C') {
            fi = i, fj = j;
        }
    }
    queue<pii> Q;
    for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) to_wall[i][j] = INF;
    for(int i = 0; i < n; i++)
    for(int j = 0; j < m; j++) {
        if(A[i][j] == '#') to_wall[i][j] = 0, Q.push({i, j});
    }
    for(int i = 0; i < n; i++)
    for(int j = 0; j < m; j++) {
        if(A[i][j] != '#' && (i == 0 || j == 0 || i == n-1 || j == m-1)) to_wall[i][j] = 1, Q.push({i, j});
    }
    while(!Q.empty()) {
        int i = Q.front().F;
        int j = Q.front().S;
        Q.pop();
        for(int k = 0; k < 4; k++) {
            int ii = i+dirI[k], jj = j+dirJ[k];
            if(!in(ii, jj)) continue;
            if(to_wall[ii][jj] > to_wall[i][j]+1) {
                to_wall[ii][jj] = to_wall[i][j]+1;
                Q.push({ii, jj});
            }
        }
    }
    for(int k = 0; k < 4; k++) {
        int di = dirI[k] == 1 ? -1 : 1;
        int dj = dirJ[k] == 1 ? -1 : 1;
        for(int i = (di == -1 ? n-1 : 0); i != (di == -1 ? -1 : n); i += di) {
            for(int j = (dj == -1 ? m-1 : 0); j != (dj == -1 ? -1 : m); j += dj) {
                int ii = i+dirI[k], jj = j+dirJ[k];
                if(!in(ii, jj) || A[ii][jj] == '#') {
                    go[k][i][j] = {i, j};
                }
                else {
                    go[k][i][j] = go[k][ii][jj];
                }
            }
        }
    }
    for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) dist[i][j] = INF;
    dist[si][sj] = 0;
    priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> PQ;
    PQ.push({dist[si][sj], {si, sj}});
    while(!PQ.empty()) {
        int i = PQ.top().S.F;
        int j = PQ.top().S.S;
        PQ.pop();
        // normal movement
        for(int k = 0; k < 4; k++) {
            int ii = i+dirI[k], jj = j+dirJ[k];
            if(!in(ii, jj)) continue;
            if(A[ii][jj] == '#') continue;
            if(dist[ii][jj] > dist[i][j]+1) {
                dist[ii][jj] = dist[i][j]+1;
                PQ.push({dist[ii][jj], {ii, jj}});
            }
        }
        // portal go brrrrrrr
        for(int k = 0; k < 4; k++) {
            int ii = go[k][i][j].F, jj = go[k][i][j].S;
            // too bad this violates unweighted bfs
            if(dist[ii][jj] > dist[i][j]+to_wall[i][j]) {
                dist[ii][jj] = dist[i][j]+to_wall[i][j];
                PQ.push({dist[ii][jj], {ii, jj}});
            }
        }
    }
    cout << dist[fi][fj] << "\n";
    return 0;
}

/*
4 4
.#.C
.#.#
....
S...



*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 592 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 464 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 1620 KB Output is correct
10 Correct 2 ms 1620 KB Output is correct
11 Correct 2 ms 1620 KB Output is correct
12 Correct 2 ms 1620 KB Output is correct
13 Correct 2 ms 1616 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 2 ms 1620 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 10 ms 6620 KB Output is correct
6 Correct 13 ms 6636 KB Output is correct
7 Correct 11 ms 6644 KB Output is correct
8 Correct 6 ms 6624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 1620 KB Output is correct
10 Correct 2 ms 1620 KB Output is correct
11 Correct 2 ms 1620 KB Output is correct
12 Correct 2 ms 1620 KB Output is correct
13 Correct 2 ms 1616 KB Output is correct
14 Correct 11 ms 6616 KB Output is correct
15 Correct 11 ms 6628 KB Output is correct
16 Correct 10 ms 6612 KB Output is correct
17 Correct 9 ms 6528 KB Output is correct
18 Correct 12 ms 6484 KB Output is correct
19 Correct 14 ms 6356 KB Output is correct
20 Correct 10 ms 6468 KB Output is correct
21 Correct 10 ms 6628 KB Output is correct
22 Correct 10 ms 6612 KB Output is correct
23 Correct 11 ms 6612 KB Output is correct
24 Correct 11 ms 6436 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 1620 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 6 ms 6560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 592 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 2 ms 1616 KB Output is correct
10 Correct 2 ms 1620 KB Output is correct
11 Correct 2 ms 1616 KB Output is correct
12 Correct 2 ms 1620 KB Output is correct
13 Correct 2 ms 1620 KB Output is correct
14 Correct 11 ms 6500 KB Output is correct
15 Correct 13 ms 6620 KB Output is correct
16 Correct 12 ms 6644 KB Output is correct
17 Correct 13 ms 6484 KB Output is correct
18 Correct 14 ms 6492 KB Output is correct
19 Correct 15 ms 6464 KB Output is correct
20 Correct 14 ms 6472 KB Output is correct
21 Correct 12 ms 6616 KB Output is correct
22 Correct 11 ms 6624 KB Output is correct
23 Correct 12 ms 6616 KB Output is correct
24 Correct 239 ms 45348 KB Output is correct
25 Correct 389 ms 42916 KB Output is correct
26 Correct 280 ms 42712 KB Output is correct
27 Correct 292 ms 42748 KB Output is correct
28 Correct 192 ms 46240 KB Output is correct
29 Correct 203 ms 46144 KB Output is correct
30 Correct 219 ms 47432 KB Output is correct
31 Correct 9 ms 6356 KB Output is correct
32 Correct 221 ms 42636 KB Output is correct
33 Correct 1 ms 596 KB Output is correct
34 Correct 1 ms 1620 KB Output is correct
35 Correct 188 ms 44660 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 6 ms 6612 KB Output is correct
38 Correct 106 ms 45512 KB Output is correct
39 Correct 179 ms 46672 KB Output is correct