Submission #784386

# Submission time Handle Problem Language Result Execution time Memory
784386 2023-07-16T05:04:40 Z raysh07 Portals (BOI14_portals) C++17
100 / 100
691 ms 151892 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
 
 
int main(){
    int n, m; cin >> n >> m;
    char c[n + 1][m + 1];
    vector <pair<pii, int> > g[n + 1][m + 1];
    for(int i= 1; i <= n; i++){
        for(int j = 1; j <= m; j++)
            cin >> c[i][j];
    }
    int xs, ys;
    int xf, yf;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(c[i][j] == 'S') xs = i, ys = j;
            if(c[i][j] == 'C') xf = i, yf = j;
        }
    }
 
    queue <pii> q;
    int d[n + 1][m + 1];
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            d[i][j] = 1e9;
            if(c[i][j] == '#'){
                d[i][j] = 0;
                q.push({i, j});
            }
        }
        d[i][0] = d[i][m + 1] = 0;
        q.push({i, 0});
        q.push({i, m + 1});
    }
    for(int i = 1; i <= m; i++){
        q.push({0, i});
        q.push({n + 1, i});
        d[0][i] = d[n + 1][i] = 0;
    }
 
    while(!q.empty()){
        int i = q.front().f;
        int j = q.front().s;
        q.pop();
        if(i > 1 && c[i - 1][j] != '#'){
            if(d[i - 1][j] > d[i][j] + 1)
            q.push({i - 1, j}), d[i - 1][j] = d[i][j] + 1;
        }
        if(i < n && c[i + 1][j] != '#'){
            if(d[i + 1][j] > d[i][j] + 1)
            q.push({i + 1, j}), d[i + 1][j] = d[i][j] + 1;
        }
        if(j > 1 && c[i][j - 1] != '#'){
            if(d[i][j - 1] > d[i][j] + 1)
            q.push({i, j - 1}), d[i][j - 1] = d[i][j] + 1;
        }
        if(j < m && c[i][j + 1] != '#'){
            if(d[i][j + 1] > d[i][j] + 1)
            q.push({i, j + 1}), d[i][j + 1] = d[i][j] + 1;
        }
    }
 
 
 
    for(int i = 1; i <= n; i++){
        int lst = 0;
        for(int j = 1; j <= m; j++){
            if(c[i][j] != '#' && ((j == 1) || (c[i][j - 1] == '#'))){
                lst = j;
            }
            g[i][j].pb({{i, lst}, d[i][j]});
        }
        lst = 0;
        for(int j = m; j >= 1; j--){
            if(c[i][j] != '#' && ((j == m) || (c[i][j + 1] == '#'))){
                lst = j;
            }
            g[i][j].pb({{i, lst}, d[i][j]});
        }
        for(int j = 1; j <= m; j++){
            if(j > 1 && c[i][j - 1] != '#') g[i][j].pb({{i, j - 1}, 1});
            if(j < m && c[i][j + 1] != '#') g[i][j].pb({{i, j + 1}, 1});
        }
    }
 
    for(int j = 1; j <= m; j++){
        int lst= 0;
        for(int i = 1; i <= n; i++){
            if(c[i][j] != '#' && (i == 1 || c[i - 1][j] == '#')){
                lst = i;
            }
            g[i][j].pb({{lst, j}, d[i][j]});
        }
        lst = 0;
        for(int i = n; i >= 1; i--){
            if(c[i][j] != '#' && (i == n || c[i + 1][j] == '#')){
                lst = i;
            }
            g[i][j].pb({{lst, j}, d[i][j]});
        }
        for(int i = 1; i <= n; ++i){
            if(i > 1 && c[i - 1][j] != '#'){
                g[i][j].pb({{i - 1, j}, 1});
            }
            if(i < n && c[i + 1][j] != '#'){
                g[i][j].pb({{i + 1, j}, 1});
            }
        }
    }
    int D[n + 1][m + 1];
    bool used[n + 1][m + 1];
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            D[i][j] = 1e9;
            used[i][j] = false;
        }
    }
    D[xs][ys] = 0;
   // cout << xs << ' ' << ys << ' ' << xf << ' ' << yf << "\n";
    priority_queue <pair<int, pii> > pq;
    pq.push({0, {xs, ys}});
    while(!pq.empty()){
        int i = pq.top().s.f, j = pq.top().s.s;
        pq.pop();
        if(used[i][j]) continue;
        used[i][j] = false;
        for(auto ooo : g[i][j]){
            int x = ooo.f.f;
            int y = ooo.f.s;
            int l = ooo.s;
            if(D[x][y] > D[i][j] + l){
                D[x][y] = D[i][j] + l;
                pq.push({-D[x][y], {x, y}});
            }
        }
    }
 
    cout << D[xf][yf];
    return 0;
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:127:15: warning: 'ys' may be used uninitialized in this function [-Wmaybe-uninitialized]
  127 |     D[xs][ys] = 0;
      |     ~~~~~~~~~~^~~
portals.cpp:127:15: warning: 'xs' may be used uninitialized in this function [-Wmaybe-uninitialized]
portals.cpp:147:21: warning: 'xf' may be used uninitialized in this function [-Wmaybe-uninitialized]
  147 |     cout << D[xf][yf];
      |                     ^
portals.cpp:147:21: warning: 'yf' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 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 1 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 272 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 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 1 ms 596 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 2 ms 680 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 684 KB Output is correct
16 Correct 1 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 0 ms 212 KB Output is correct
5 Correct 15 ms 6196 KB Output is correct
6 Correct 15 ms 6140 KB Output is correct
7 Correct 16 ms 6188 KB Output is correct
8 Correct 11 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 1 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 596 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 684 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 684 KB Output is correct
14 Correct 19 ms 6128 KB Output is correct
15 Correct 15 ms 6196 KB Output is correct
16 Correct 16 ms 6128 KB Output is correct
17 Correct 16 ms 6232 KB Output is correct
18 Correct 20 ms 6228 KB Output is correct
19 Correct 18 ms 6104 KB Output is correct
20 Correct 16 ms 6016 KB Output is correct
21 Correct 22 ms 6240 KB Output is correct
22 Correct 16 ms 6228 KB Output is correct
23 Correct 15 ms 6152 KB Output is correct
24 Correct 15 ms 6008 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 14 ms 6100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 14 ms 6184 KB Output is correct
15 Correct 17 ms 6240 KB Output is correct
16 Correct 15 ms 6228 KB Output is correct
17 Correct 15 ms 6188 KB Output is correct
18 Correct 16 ms 6224 KB Output is correct
19 Correct 16 ms 6068 KB Output is correct
20 Correct 16 ms 6076 KB Output is correct
21 Correct 14 ms 6228 KB Output is correct
22 Correct 17 ms 6200 KB Output is correct
23 Correct 19 ms 6124 KB Output is correct
24 Correct 625 ms 150220 KB Output is correct
25 Correct 691 ms 151632 KB Output is correct
26 Correct 685 ms 151892 KB Output is correct
27 Correct 654 ms 151624 KB Output is correct
28 Correct 596 ms 150132 KB Output is correct
29 Correct 582 ms 150100 KB Output is correct
30 Correct 617 ms 150092 KB Output is correct
31 Correct 19 ms 6100 KB Output is correct
32 Correct 654 ms 151368 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 593 ms 150640 KB Output is correct
36 Correct 0 ms 296 KB Output is correct
37 Correct 11 ms 6092 KB Output is correct
38 Correct 478 ms 148884 KB Output is correct
39 Correct 517 ms 149556 KB Output is correct