Submission #823606

# Submission time Handle Problem Language Result Execution time Memory
823606 2023-08-12T20:50:35 Z serifefedartar Portals (BOI14_portals) C++17
100 / 100
309 ms 25456 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000007
#define LOGN 20
#define MAXN 2005

const int delRow[] = {-1, 1, 0, 0};
const int delCol[] = {0, 0, -1, 1};
vector<vector<int>> grid, nU, nD, nL, nR, dist;
pair<int,int> S = {-1, -1}, T = {-1, -1};
int R, C;
void precalc() {
    for (int i = 1; i <= C; i++) {
        int nearest = 0;
        for (int j = 1; j <= R; j++) {
            if (grid[j][i])
                nU[j][i] = nearest;
            else
                nearest = j;
        }
    }
    for (int i = 1; i <= C; i++) {
        int nearest = R+1;
        for (int j = R; j >= 1; j--) {
            if (grid[j][i])
                nD[j][i] = nearest;
            else
                nearest = j;
        }
    }
    for (int i = 1; i <= R; i++) {
        int nearest = 0;
        for (int j = 1; j <= C; j++) {
            if (grid[i][j])
                nL[i][j] = nearest;
            else
                nearest = j;
        }
    }
    for (int i = 1; i <= R; i++) {
        int nearest = C+1;
        for (int j = C; j >= 1; j--) {
            if (grid[i][j])
                nR[i][j] = nearest;
            else
                nearest = j;
        }
    }
}

int main() {
    fast
    cin >> R >> C;

    grid = vector<vector<int>>(R+1, vector<int>(C+1));
    nU = vector<vector<int>>(R+1, vector<int>(C+1, 0));
    nD = vector<vector<int>>(R+1, vector<int>(C+1, 0));
    nL = vector<vector<int>>(R+1, vector<int>(C+1, 0));
    nR = vector<vector<int>>(R+1, vector<int>(C+1, 0));
    dist = vector<vector<int>>(R+1, vector<int>(C+1, 1e9));
    for (int i = 1; i <= R; i++) {
        string s;
        cin >> s;
        for (int j = 1; j <= C; j++) {
            if (s[j-1] != '#')
                grid[i][j] = 1;

            if (s[j-1] == 'S')
                S = {i, j};
            else if (s[j-1] == 'C')
                T = {i, j};
        }
    }
    precalc();
   

    priority_queue<pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>>> q;
    dist[S.f][S.s] = 0;
    q.push({0, S});
    while (!q.empty()) {
        int row = q.top().s.f;
        int col = q.top().s.s;
        q.pop();

        bool wall = false;
        for (int i = 0; i < 4; i++) {
            int nRow = row + delRow[i];
            int nCol = col + delCol[i];

            if (nRow > 0 && nCol > 0 && nRow <= R && nCol <= C && grid[nRow][nCol]) {
                if (dist[nRow][nCol] > dist[row][col] + 1) {
                    dist[nRow][nCol] = dist[row][col] + 1;
                    q.push({dist[nRow][nCol], {nRow, nCol}});
                }
            } else
                wall = true;
        }

        pair<int,int> d1 = {row, nL[row][col] + 1};
        pair<int,int> d2 = {row, nR[row][col] - 1};
        pair<int,int> d3 = {nU[row][col] + 1, col};
        pair<int,int> d4 = {nD[row][col] - 1, col};
        vector<pair<int,int>> v = {d1, d2, d3, d4};
        for (int from = 0; from < 4; from++) {
            for (int to = 0; to < 4; to++) {
                pair<int,int> from_p = v[from];
                pair<int,int> to_p = v[to];

                if (dist[row][col] + abs(row-from_p.f) + abs(col-from_p.s) + 1 < dist[to_p.f][to_p.s]) {
                    dist[to_p.f][to_p.s] = dist[row][col] + abs(row-from_p.f) + abs(col-from_p.s) + 1;
                    q.push({dist[to_p.f][to_p.s], to_p});
                }
            }
        }
    }
    cout << dist[T.f][T.s] << "\n";
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:90:14: warning: variable 'wall' set but not used [-Wunused-but-set-variable]
   90 |         bool wall = false;
      |              ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 6 ms 1236 KB Output is correct
6 Correct 7 ms 1324 KB Output is correct
7 Correct 9 ms 1324 KB Output is correct
8 Correct 3 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 6 ms 1236 KB Output is correct
15 Correct 7 ms 1364 KB Output is correct
16 Correct 8 ms 1364 KB Output is correct
17 Correct 8 ms 1384 KB Output is correct
18 Correct 10 ms 1364 KB Output is correct
19 Correct 9 ms 1360 KB Output is correct
20 Correct 9 ms 1408 KB Output is correct
21 Correct 6 ms 1360 KB Output is correct
22 Correct 6 ms 1352 KB Output is correct
23 Correct 7 ms 1364 KB Output is correct
24 Correct 7 ms 1356 KB Output is correct
25 Correct 0 ms 324 KB Output is correct
26 Correct 1 ms 324 KB Output is correct
27 Correct 0 ms 320 KB Output is correct
28 Correct 3 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 6 ms 1236 KB Output is correct
15 Correct 7 ms 1368 KB Output is correct
16 Correct 8 ms 1364 KB Output is correct
17 Correct 7 ms 1364 KB Output is correct
18 Correct 10 ms 1364 KB Output is correct
19 Correct 9 ms 1408 KB Output is correct
20 Correct 10 ms 1388 KB Output is correct
21 Correct 6 ms 1236 KB Output is correct
22 Correct 6 ms 1364 KB Output is correct
23 Correct 7 ms 1360 KB Output is correct
24 Correct 238 ms 25312 KB Output is correct
25 Correct 309 ms 25324 KB Output is correct
26 Correct 248 ms 25192 KB Output is correct
27 Correct 237 ms 25456 KB Output is correct
28 Correct 146 ms 25064 KB Output is correct
29 Correct 164 ms 25056 KB Output is correct
30 Correct 194 ms 25100 KB Output is correct
31 Correct 7 ms 1364 KB Output is correct
32 Correct 217 ms 25132 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 178 ms 25144 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 3 ms 1236 KB Output is correct
38 Correct 76 ms 25072 KB Output is correct
39 Correct 129 ms 25036 KB Output is correct