Submission #1029289

# Submission time Handle Problem Language Result Execution time Memory
1029289 2024-07-20T15:26:42 Z VMaksimoski008 Portals (BOI14_portals) C++17
100 / 100
209 ms 34788 KB
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Node = array<int, 3>;

const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;

int dr[8] = {0, -1, 0, 1, 1, 1, -1, -1};
int dc[8] = {-1, 0, 1, 0, 1, -1, 1, -1};
int U[1005][1005], R[1005][1005], L[1005][1005], D[1005][1005];
int D1[1005][1005], vis[1005][1005], dist[1005][1005];
char mat[1005][1005];

signed main() {
    int n, m;
    cin >> n >> m;

    pii S, C;
    for(int i=0; i<=n+1; i++)
        for(int j=0; j<=m+1; j++) mat[i][j] = '#';

    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            cin >> mat[i][j];
            if(mat[i][j] == 'S') S = { i, j };
            if(mat[i][j] == 'C') C = { i, j };
        }
    }

    for(int i=1; i<=n; i++) {
        int last = 0;
        for(int j=1; j<=m; j++) {
            if(mat[i][j] == '#') last = j;
            L[i][j] = last + 1;
        }

        last = m+1;
        for(int j=m; j>=1; j--) {
            if(mat[i][j] == '#') last = j;
            R[i][j] = last - 1;
        }
    }

    for(int j=1; j<=m; j++) {
        int last = 0;
        for(int i=1; i<=n; i++) {
            if(mat[i][j] == '#') last = i;
            U[i][j] = last + 1;
        }

        last = n+1;
        for(int i=n; i>=1; i--) {
            if(mat[i][j] == '#') last = i;
            D[i][j] = last - 1;
        }
    }

    queue<pii> q;
    for(int i=0; i<=n+1; i++) {
        for(int j=0; j<=m+1; j++) {
            if(mat[i][j] == '#') q.push({ i, j }), vis[i][j] = 1, D1[i][j] = -1;
            else D1[i][j] = 1e9;
        }
    }

    while(!q.empty()) {
        auto [r, c] = q.front(); q.pop();

        for(int i=0; i<4; i++) {
            int nr = r + dr[i], nc = c + dc[i];
            if(nr < 0 || nr > n + 1 || nc < 0 || nc > m + 1) continue;
            if(vis[nr][nc] || mat[nr][nc] == '#') continue;
            vis[nr][nc] = 1;
            q.push({ nr, nc });
            D1[nr][nc] = D1[r][c] + 1;
        }
    }

    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++) dist[i][j] = 1e9;
    dist[S.first][S.second] = 0;

    priority_queue<Node, vector<Node>, greater<Node> > pq;
    pq.push({ 0, S.first, S.second });

    while(!pq.empty()) {
        auto [d, r, c] = pq.top(); pq.pop();
        if(d != dist[r][c]) continue;

        for(int i=0; i<4; i++) {
            int nr = r + dr[i], nc = c + dc[i];
            if(nr < 1 || nr > n || nc < 1 || nc > m) continue;
            if(mat[nr][nc] == '#') continue;

            if(dist[nr][nc] > dist[r][c] + 1) {
                dist[nr][nc] = d + 1;
                pq.push({ d + 1, nr, nc });
            }
        }

        //try up
        if(dist[U[r][c]][c] > dist[r][c] + D1[r][c] + 1) {
            dist[U[r][c]][c] = dist[r][c] + D1[r][c] + 1;
            pq.push({ dist[r][c] + D1[r][c] + 1, U[r][c], c });
        }

        if(dist[D[r][c]][c] > dist[r][c] + D1[r][c] + 1) {
            dist[D[r][c]][c] = dist[r][c] + D1[r][c] + 1;
            pq.push({ dist[r][c] + D1[r][c] + 1, D[r][c], c });
        }

        if(dist[r][L[r][c]] > dist[r][c] + D1[r][c] + 1) {
            dist[r][L[r][c]] = dist[r][c] + D1[r][c] + 1;
            pq.push({ dist[r][L[r][c]], r, L[r][c] });
        }

        if(dist[r][R[r][c]] > dist[r][c] + D1[r][c] + 1) {
            dist[r][R[r][c]] = dist[r][c] + D1[r][c] + 1;
            pq.push({ dist[r][R[r][c]], r, R[r][c] });
        }
    }

    // for(int i=1; i<=n; i++) {
    //     for(int j=1; j<=m; j++) cout << (dist[i][j] == 1e9 ? -1 : dist[i][j]) << " ";
    //     cout << '\n';
    // }

    cout << dist[C.first][C.second] << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 600 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 0 ms 440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
12 Correct 1 ms 1884 KB Output is correct
13 Correct 1 ms 1884 KB Output is correct
14 Correct 0 ms 604 KB Output is correct
15 Correct 1 ms 1884 KB Output is correct
16 Correct 0 ms 448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 700 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 6 ms 6428 KB Output is correct
6 Correct 8 ms 6236 KB Output is correct
7 Correct 8 ms 6236 KB Output is correct
8 Correct 5 ms 6416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 0 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 2 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
12 Correct 2 ms 1884 KB Output is correct
13 Correct 1 ms 1884 KB Output is correct
14 Correct 6 ms 6236 KB Output is correct
15 Correct 7 ms 6432 KB Output is correct
16 Correct 7 ms 6236 KB Output is correct
17 Correct 7 ms 6236 KB Output is correct
18 Correct 12 ms 6392 KB Output is correct
19 Correct 9 ms 6232 KB Output is correct
20 Correct 8 ms 6236 KB Output is correct
21 Correct 8 ms 6316 KB Output is correct
22 Correct 7 ms 6308 KB Output is correct
23 Correct 11 ms 6236 KB Output is correct
24 Correct 10 ms 6236 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 1884 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 5 ms 6308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 0 ms 604 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 0 ms 604 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
12 Correct 1 ms 1884 KB Output is correct
13 Correct 1 ms 2136 KB Output is correct
14 Correct 6 ms 6236 KB Output is correct
15 Correct 7 ms 6236 KB Output is correct
16 Correct 7 ms 6236 KB Output is correct
17 Correct 7 ms 6288 KB Output is correct
18 Correct 8 ms 6492 KB Output is correct
19 Correct 9 ms 6236 KB Output is correct
20 Correct 8 ms 6236 KB Output is correct
21 Correct 6 ms 6232 KB Output is correct
22 Correct 9 ms 6236 KB Output is correct
23 Correct 8 ms 6236 KB Output is correct
24 Correct 160 ms 33460 KB Output is correct
25 Correct 203 ms 30296 KB Output is correct
26 Correct 209 ms 30072 KB Output is correct
27 Correct 186 ms 30036 KB Output is correct
28 Correct 118 ms 33724 KB Output is correct
29 Correct 134 ms 34644 KB Output is correct
30 Correct 166 ms 34788 KB Output is correct
31 Correct 8 ms 6236 KB Output is correct
32 Correct 174 ms 29816 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 1 ms 1884 KB Output is correct
35 Correct 144 ms 31872 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 5 ms 6236 KB Output is correct
38 Correct 74 ms 32824 KB Output is correct
39 Correct 113 ms 34132 KB Output is correct