답안 #875307

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
875307 2023-11-19T04:33:48 Z irmuun 포탈들 (BOI14_portals) C++17
100 / 100
498 ms 155912 KB
#include<bits/stdc++.h>

using namespace std;
 
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int r,c;
    cin>>r>>c;
    string s[r];
    int si,sj,ci,cj;
    for(int i=0;i<r;i++){
        cin>>s[i];
        for(int j=0;j<c;j++){
            if(s[i][j]=='S'){
                si=i;
                sj=j;
            }
            if(s[i][j]=='C'){
                ci=i;
                cj=j;
            }
        }
    }
    vector<array<int,3>>adj[r][c];
    int a[r][c][4];
    for(int i=0;i<r;i++){
        int cur=0;
        for(int j=0;j<c;j++){
            if(s[i][j]=='#'){
                cur=j+1;
            }
            else{
                a[i][j][0]=cur;
            }
        }
        cur=c-1;
        for(int j=c-1;j>=0;j--){
            if(s[i][j]=='#'){
                cur=j-1;
            }
            else{
                a[i][j][1]=cur;
            }
        }
    }
    for(int j=0;j<c;j++){
        int cur=0;
        for(int i=0;i<r;i++){
            if(s[i][j]=='#'){
                cur=i+1;
            }
            else{
                a[i][j][2]=cur;
            }
        }
        cur=r-1;
        for(int i=r-1;i>=0;i--){
            if(s[i][j]=='#'){
                cur=i-1;
            }
            else{
                a[i][j][3]=cur;
            }
        }
    }
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            if(s[i][j]=='#') continue;
            if(i<r-1&&s[i][j]!='#'&&s[i+1][j]!='#'){
                adj[i][j].pb({i+1,j,1});
                adj[i+1][j].pb({i,j,1});
            }
            if(j<c-1&&s[i][j]!='#'&&s[i][j+1]!='#'){
                adj[i][j].pb({i,j+1,1});
                adj[i][j+1].pb({i,j,1});
            }
            int d=min({j-a[i][j][0],a[i][j][1]-j,i-a[i][j][2],a[i][j][3]-i})+1;
            adj[i][j].pb({i,a[i][j][0],d});
            adj[i][j].pb({i,a[i][j][1],d});
            adj[i][j].pb({a[i][j][2],j,d});
            adj[i][j].pb({a[i][j][3],j,d});
        }
    }
    vector<vector<int>>dist(r,vector<int>(c,1e9));
    set<array<int,3>>st;
    st.insert({0,si,sj});
    dist[si][sj]=0;
    while(!st.empty()){
        auto [d,i,j]=*st.begin();
        st.erase(st.begin());
        for(auto [x,y,w]:adj[i][j]){
            if(dist[i][j]+w<dist[x][y]){
                if(dist[x][y]<1e9){
                    st.erase({dist[x][y],x,y});
                }
                dist[x][y]=dist[i][j]+w;
                st.insert({dist[x][y],x,y});
            }
        }
    }
    cout<<dist[ci][cj];
}

Compilation message

portals.cpp: In function 'int main()':
portals.cpp:94:16: warning: 'sj' may be used uninitialized in this function [-Wmaybe-uninitialized]
   94 |     dist[si][sj]=0;
      |                ^
portals.cpp:108:22: warning: 'cj' may be used uninitialized in this function [-Wmaybe-uninitialized]
  108 |     cout<<dist[ci][cj];
      |                      ^
portals.cpp:94:12: warning: 'si' may be used uninitialized in this function [-Wmaybe-uninitialized]
   94 |     dist[si][sj]=0;
      |            ^
portals.cpp:108:18: warning: 'ci' may be used uninitialized in this function [-Wmaybe-uninitialized]
  108 |     cout<<dist[ci][cj];
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 452 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 868 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 9 ms 4700 KB Output is correct
6 Correct 11 ms 4700 KB Output is correct
7 Correct 11 ms 4956 KB Output is correct
8 Correct 7 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 716 KB Output is correct
11 Correct 1 ms 708 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 9 ms 4808 KB Output is correct
15 Correct 10 ms 4920 KB Output is correct
16 Correct 11 ms 5152 KB Output is correct
17 Correct 10 ms 4956 KB Output is correct
18 Correct 13 ms 5752 KB Output is correct
19 Correct 15 ms 6748 KB Output is correct
20 Correct 13 ms 6492 KB Output is correct
21 Correct 9 ms 4700 KB Output is correct
22 Correct 10 ms 4876 KB Output is correct
23 Correct 11 ms 4956 KB Output is correct
24 Correct 13 ms 6656 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 6 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 460 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 600 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 1 ms 860 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 9 ms 4788 KB Output is correct
15 Correct 10 ms 4700 KB Output is correct
16 Correct 11 ms 5208 KB Output is correct
17 Correct 11 ms 4956 KB Output is correct
18 Correct 13 ms 5724 KB Output is correct
19 Correct 14 ms 6748 KB Output is correct
20 Correct 13 ms 6492 KB Output is correct
21 Correct 9 ms 4836 KB Output is correct
22 Correct 10 ms 4700 KB Output is correct
23 Correct 11 ms 4956 KB Output is correct
24 Correct 334 ms 120764 KB Output is correct
25 Correct 498 ms 155912 KB Output is correct
26 Correct 458 ms 155284 KB Output is correct
27 Correct 447 ms 155488 KB Output is correct
28 Correct 257 ms 108116 KB Output is correct
29 Correct 278 ms 109656 KB Output is correct
30 Correct 308 ms 111200 KB Output is correct
31 Correct 16 ms 6744 KB Output is correct
32 Correct 458 ms 155084 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 349 ms 128288 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 6 ms 4996 KB Output is correct
38 Correct 205 ms 118724 KB Output is correct
39 Correct 231 ms 100404 KB Output is correct