Submission #136138

# Submission time Handle Problem Language Result Execution time Memory
136138 2019-07-24T18:44:52 Z Vlatko Portals (BOI14_portals) C++14
100 / 100
331 ms 42264 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int inf = 1e9;
const int MAXN = 1010;

int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
char DCH[4] = {'N', 'E', 'S', 'W'};

struct State {
    int d, r, c;
    State(int dd, int rr, int cc) {
        d = dd, r = rr, c = cc;
    }
    State() {}
    bool operator < (const State &rhs) const {
        return d > rhs.d;
    }
};

int R, C, sr, sc, er, ec;
bool blocked[MAXN][MAXN];
int closest[MAXN][MAXN]; // distance to closest wall
pii wallpos[4][MAXN][MAXN]; // position of wall N/E/S/W of here
int dist[MAXN][MAXN]; // dijkstra
priority_queue<State, vector<State>> pq;

inline bool inside(int r, int c) {
    return r >= 0 && r <= R+1 && c >= 0 && c <= C+1;
}

void getClosest() {
    queue<pii> q;
    for (int r = 0; r <= R+1; ++r) {
        for (int c = 0; c <= C+1; ++c) {
            if (blocked[r][c]) {
                q.emplace(r, c);
                closest[r][c] = 0;
            } else {
                closest[r][c] = -1;
            }
        }
    }
    while (!q.empty()) {
        pii top = q.front();
        q.pop();
        int r = top.first, c = top.second;
        for (int d = 0; d < 4; ++d) {
            int r1 = r + dr[d], c1 = c + dc[d];
            if (closest[r1][c1] == -1) {
                closest[r1][c1] = closest[r][c] + 1;
                q.emplace(r1, c1);
            }
        }
    }
}

void getWallPos() {
    for (int d = 0; d < 4; ++d) {
        int rstart, cstart, rchange, cchange;
        if (dr[d] >= 0) {
            rstart = R, rchange = -1;
        } else { // -1
            rstart = 0, rchange = 1;
        }
        if (dc[d] >= 0) {
            cstart = C, cchange = -1;
        } else {
            cstart = 0, cchange = 1;
        }
        for (int r = rstart; inside(r, 0); r += rchange) {
            for (int c = cstart; inside(0, c); c += cchange) {
                if (!blocked[r][c]) {
                    int r1 = r + dr[d], c1 = c + dc[d];
                    if (blocked[r1][c1]) {
                        wallpos[d][r][c] = {r, c};
                    } else {
                        wallpos[d][r][c] = wallpos[d][r1][c1];
                    }
                }
            }
        }
    }
}

void getDistance() {
    for (int r = 0; r <= R+1; ++r) {
        for (int c = 0; c <= C+1; ++c) {
            dist[r][c] = inf;
        }
    }
    dist[sr][sc] = 0;
    pq.emplace(0, sr, sc);
    while (!pq.empty()) {
        State top = pq.top();
        pq.pop();
        if (top.d > dist[top.r][top.c]) {
            continue;
        }
        // adjacent cells
        for (int d = 0; d < 4; ++d) {
            int r1 = top.r + dr[d], c1 = top.c + dc[d];
            if (inside(r1, c1) && !blocked[r1][c1] && top.d + 1 < dist[r1][c1]) {
                dist[r1][c1] = top.d + 1;
                pq.emplace(top.d + 1, r1, c1);
            }
        }
        // teleport to walls in orthogonal directions
        for (int d = 0; d < 4; ++d) {
            pii target = wallpos[d][top.r][top.c];
            int tr = target.first, tc = target.second;
            int newdist = top.d + closest[top.r][top.c];
            if (newdist < dist[tr][tc]) {
                dist[tr][tc] = newdist;
                pq.emplace(newdist, tr, tc);
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> R >> C;
    for (int r = 0; r <= R+1; ++r) {
        blocked[r][0] = blocked[r][C+1] = 1;
    }
    for (int c = 0; c <= C+1; ++c) {
        blocked[0][c] = blocked[R+1][c] = 1;
    }
    for (int r = 1; r <= R; ++r) {
        for (int c = 1; c <= C; ++c) {
            char ch;
            cin >> ch;
            if (ch == 'S') {
                sr = r, sc = c;
            } else if (ch == 'C') {
                er = r, ec = c;
            }
            blocked[r][c] = (ch == '#');
        }
    }
    getClosest();
    getWallPos();
    getDistance();
    cout << dist[er][ec] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 3 ms 1656 KB Output is correct
10 Correct 3 ms 1656 KB Output is correct
11 Correct 3 ms 1784 KB Output is correct
12 Correct 3 ms 1784 KB Output is correct
13 Correct 3 ms 1656 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 3 ms 1656 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 636 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 12 ms 6836 KB Output is correct
6 Correct 13 ms 6820 KB Output is correct
7 Correct 14 ms 6856 KB Output is correct
8 Correct 10 ms 6800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 516 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 3 ms 1656 KB Output is correct
10 Correct 3 ms 1784 KB Output is correct
11 Correct 3 ms 1792 KB Output is correct
12 Correct 3 ms 1788 KB Output is correct
13 Correct 3 ms 1784 KB Output is correct
14 Correct 12 ms 6832 KB Output is correct
15 Correct 13 ms 6824 KB Output is correct
16 Correct 14 ms 6808 KB Output is correct
17 Correct 12 ms 6840 KB Output is correct
18 Correct 14 ms 6864 KB Output is correct
19 Correct 14 ms 6776 KB Output is correct
20 Correct 14 ms 6776 KB Output is correct
21 Correct 12 ms 6832 KB Output is correct
22 Correct 13 ms 6856 KB Output is correct
23 Correct 13 ms 6824 KB Output is correct
24 Correct 13 ms 6648 KB Output is correct
25 Correct 2 ms 508 KB Output is correct
26 Correct 3 ms 1784 KB Output is correct
27 Correct 2 ms 504 KB Output is correct
28 Correct 10 ms 6804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 636 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 684 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 3 ms 1784 KB Output is correct
10 Correct 3 ms 1784 KB Output is correct
11 Correct 4 ms 1736 KB Output is correct
12 Correct 3 ms 1784 KB Output is correct
13 Correct 3 ms 1784 KB Output is correct
14 Correct 12 ms 6832 KB Output is correct
15 Correct 13 ms 6824 KB Output is correct
16 Correct 14 ms 6936 KB Output is correct
17 Correct 13 ms 6736 KB Output is correct
18 Correct 14 ms 6864 KB Output is correct
19 Correct 14 ms 6776 KB Output is correct
20 Correct 14 ms 6776 KB Output is correct
21 Correct 13 ms 6828 KB Output is correct
22 Correct 13 ms 6828 KB Output is correct
23 Correct 14 ms 6828 KB Output is correct
24 Correct 226 ms 42144 KB Output is correct
25 Correct 331 ms 42184 KB Output is correct
26 Correct 318 ms 42180 KB Output is correct
27 Correct 299 ms 42168 KB Output is correct
28 Correct 194 ms 42084 KB Output is correct
29 Correct 209 ms 42136 KB Output is correct
30 Correct 244 ms 42264 KB Output is correct
31 Correct 13 ms 6672 KB Output is correct
32 Correct 294 ms 41976 KB Output is correct
33 Correct 2 ms 504 KB Output is correct
34 Correct 3 ms 1656 KB Output is correct
35 Correct 252 ms 42164 KB Output is correct
36 Correct 2 ms 504 KB Output is correct
37 Correct 10 ms 6800 KB Output is correct
38 Correct 148 ms 42080 KB Output is correct
39 Correct 162 ms 34300 KB Output is correct