답안 #769864

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
769864 2023-06-30T11:47:27 Z Ronin13 포탈들 (BOI14_portals) C++17
100 / 100
719 ms 151688 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]
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 304 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 636 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 16 ms 6228 KB Output is correct
6 Correct 15 ms 6244 KB Output is correct
7 Correct 21 ms 6212 KB Output is correct
8 Correct 12 ms 6100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 1 ms 212 KB Output is correct
6 Correct 0 ms 296 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 304 KB Output is correct
9 Correct 1 ms 596 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 2 ms 724 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 2 ms 688 KB Output is correct
14 Correct 15 ms 6196 KB Output is correct
15 Correct 17 ms 6228 KB Output is correct
16 Correct 20 ms 6116 KB Output is correct
17 Correct 15 ms 6200 KB Output is correct
18 Correct 23 ms 6248 KB Output is correct
19 Correct 19 ms 6100 KB Output is correct
20 Correct 21 ms 6100 KB Output is correct
21 Correct 15 ms 6196 KB Output is correct
22 Correct 15 ms 6236 KB Output is correct
23 Correct 15 ms 6228 KB Output is correct
24 Correct 16 ms 6112 KB Output is correct
25 Correct 1 ms 212 KB Output is correct
26 Correct 1 ms 596 KB Output is correct
27 Correct 0 ms 212 KB Output is correct
28 Correct 11 ms 6072 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 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 684 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 17 ms 6132 KB Output is correct
15 Correct 15 ms 6148 KB Output is correct
16 Correct 16 ms 6160 KB Output is correct
17 Correct 17 ms 6228 KB Output is correct
18 Correct 22 ms 6144 KB Output is correct
19 Correct 19 ms 6044 KB Output is correct
20 Correct 15 ms 6100 KB Output is correct
21 Correct 14 ms 6140 KB Output is correct
22 Correct 15 ms 6228 KB Output is correct
23 Correct 16 ms 6228 KB Output is correct
24 Correct 575 ms 150244 KB Output is correct
25 Correct 713 ms 151688 KB Output is correct
26 Correct 719 ms 151684 KB Output is correct
27 Correct 701 ms 151600 KB Output is correct
28 Correct 583 ms 150288 KB Output is correct
29 Correct 581 ms 150188 KB Output is correct
30 Correct 624 ms 150100 KB Output is correct
31 Correct 19 ms 6100 KB Output is correct
32 Correct 691 ms 151440 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 2 ms 684 KB Output is correct
35 Correct 601 ms 150568 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 16 ms 6100 KB Output is correct
38 Correct 549 ms 148892 KB Output is correct
39 Correct 495 ms 149572 KB Output is correct