답안 #912820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
912820 2024-01-20T01:12:42 Z 1075508020060209tc 포탈들 (BOI14_portals) C++14
100 / 100
551 ms 99884 KB
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
#define SZ(x) (int)(x).size()
int n;int m;
string gr[200005];
int dir[5]={0,1,0,-1,0};
pair<int,int>dp[1010][1010][4];
int ok(int x,int y){
if(x>=1&&y>=1&&x<=n&&y<=m){return 1;}
return 0;
}
int ndis[1010][1010];
void buildnear(){

queue<pair<int,int>>q;
for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        ndis[i][j]=1e18;
        if(gr[i][j]=='#'){continue;}
        int fok=0;
        for(int d=0;d<=3;d++){
            int x=i+dir[d];int y=j+dir[d+1];
            if(!ok(x,y)||gr[x][y]=='#'){
                fok=1;
            }
        }
        if(fok){
            q.push({i,j});
            ndis[i][j]=0;
        }
    }
}

while(q.size()){
    int nwx=q.front().first;
    int nwy=q.front().second;
    q.pop();
    for(int d=0;d<=3;d++){
        int x=nwx+dir[d];int y=nwy+dir[d+1];
        if(!ok(x,y)||gr[x][y]=='#'){continue;}
        if(ndis[x][y]>12345678){
            ndis[x][y]=ndis[nwx][nwy]+1;
            q.push({x,y});
        }
    }
}

}


void builddp(){
for(int i=1;i<=n;i++){
    for(int j=m;j>=1;j--){
        if(gr[i][j]=='#'){dp[i][j][0]={-1,-1};}
        int x=i+0;int y=j+1;
        if(!ok(x,y)||gr[x][y]=='#'){
            dp[i][j][0]={i,j};
        }else{
            dp[i][j][0]=dp[i][j+1][0];
        }
    }
}
for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        if(gr[i][j]=='#'){dp[i][j][2]={-1,-1};}
        int x=i+0;int y=j-1;
        if(!ok(x,y)||gr[x][y]=='#'){
            dp[i][j][2]={i,j};
        }else{
            dp[i][j][2]=dp[i][j-1][2];
        }
    }
}
//
for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        if(gr[i][j]=='#'){dp[i][j][3]={-1,-1};}
        int x=i-1;int y=j;
        if(!ok(x,y)||gr[x][y]=='#'){
            dp[i][j][3]={i,j};
        }else{
            dp[i][j][3]=dp[i-1][j][3];
        }
    }
}
for(int i=n;i>=1;i--){
    for(int j=1;j<=m;j++){
        if(gr[i][j]=='#'){dp[i][j][1]={-1,-1};}
        int x=i+1;int y=j;
        if(!ok(x,y)||gr[x][y]=='#'){
            dp[i][j][1]={i,j};
        }else{
            dp[i][j][1]=dp[i+1][j][1];
        }
    }
}
}
int dis[1010][1010];
bool vis[1010][1010];
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
    cin>>gr[i];
    gr[i]='#'+gr[i]+"#";
}
gr[0]="";gr[n+1]="";
for(int i=1;i<=m;i++){
    gr[0]+="#";
    gr[n+1]+="#";
}
builddp();
buildnear();
int stx;int sty;int enx;int eny;
for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++){
        if(gr[i][j]=='S'){
            stx=i;sty=j;
        }
        if(gr[i][j]=='C'){
            enx=i;eny=j;
        }
        dis[i][j]=1e12;
    }
}
priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>> ,greater<pair<int,pair<int,int>>>  >q;
q.push({0,{stx,sty}});
dis[stx][sty]=0;
//cout<<"hehe\n";
while(q.size()){
    int nwx=q.top().second.first;
    int nwy=q.top().second.second;
    q.pop();
    if(vis[nwx][nwy]){continue;}
    vis[nwx][nwy]=1;
    for(int d=0;d<=3;d++){
        int x=nwx+dir[d];int y=nwy+dir[d+1];
        if(vis[x][y]){continue;}
        if(!ok(x,y)||gr[x][y]=='#'){continue;}
        dis[x][y]=min(dis[x][y],dis[nwx][nwy]+1);
        if(dis[x][y]==dis[nwx][nwy]+1){
            q.push({dis[x][y],{x,y}});
        }
    }
    for(int d=0;d<=3;d++){
        int x=dp[nwx][nwy][d].first;
        int y=dp[nwx][nwy][d].second;
        if(vis[x][y]){continue;}
        dis[x][y]=min(dis[x][y],dis[nwx][nwy]+ndis[nwx][nwy]+1);
        if(dis[x][y]==dis[nwx][nwy]+ndis[nwx][nwy]+1){
            q.push({dis[x][y],{x,y}});
        }
    }
}
cout<<dis[enx][eny]<<"\n";
}




Compilation message

portals.cpp: In function 'int main()':
portals.cpp:131:14: warning: 'sty' may be used uninitialized in this function [-Wmaybe-uninitialized]
  131 | dis[stx][sty]=0;
      | ~~~~~~~~~~~~~^~
portals.cpp:131:14: warning: 'stx' may be used uninitialized in this function [-Wmaybe-uninitialized]
portals.cpp:158:19: warning: 'eny' may be used uninitialized in this function [-Wmaybe-uninitialized]
  158 | cout<<dis[enx][eny]<<"\n";
      |                   ^
portals.cpp:158:19: warning: 'enx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 4 ms 13404 KB Output is correct
4 Correct 3 ms 13404 KB Output is correct
5 Correct 3 ms 13404 KB Output is correct
6 Correct 3 ms 13404 KB Output is correct
7 Correct 4 ms 13404 KB Output is correct
8 Correct 4 ms 13404 KB Output is correct
9 Correct 3 ms 13404 KB Output is correct
10 Correct 4 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 1 ms 13404 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13504 KB Output is correct
6 Correct 4 ms 13404 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 4 ms 13404 KB Output is correct
9 Correct 5 ms 15668 KB Output is correct
10 Correct 5 ms 15448 KB Output is correct
11 Correct 5 ms 15452 KB Output is correct
12 Correct 5 ms 15452 KB Output is correct
13 Correct 4 ms 15556 KB Output is correct
14 Correct 4 ms 13400 KB Output is correct
15 Correct 5 ms 15452 KB Output is correct
16 Correct 3 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13660 KB Output is correct
2 Correct 3 ms 13504 KB Output is correct
3 Correct 4 ms 13404 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 11 ms 30312 KB Output is correct
6 Correct 13 ms 30272 KB Output is correct
7 Correct 16 ms 30180 KB Output is correct
8 Correct 10 ms 30588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 4 ms 13404 KB Output is correct
3 Correct 4 ms 13404 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13404 KB Output is correct
6 Correct 4 ms 13500 KB Output is correct
7 Correct 4 ms 13404 KB Output is correct
8 Correct 4 ms 13512 KB Output is correct
9 Correct 5 ms 15448 KB Output is correct
10 Correct 6 ms 15452 KB Output is correct
11 Correct 5 ms 15452 KB Output is correct
12 Correct 5 ms 15452 KB Output is correct
13 Correct 5 ms 15652 KB Output is correct
14 Correct 11 ms 30316 KB Output is correct
15 Correct 13 ms 30296 KB Output is correct
16 Correct 15 ms 30252 KB Output is correct
17 Correct 13 ms 30456 KB Output is correct
18 Correct 18 ms 30528 KB Output is correct
19 Correct 18 ms 30452 KB Output is correct
20 Correct 17 ms 30300 KB Output is correct
21 Correct 11 ms 30556 KB Output is correct
22 Correct 12 ms 30412 KB Output is correct
23 Correct 15 ms 30556 KB Output is correct
24 Correct 16 ms 30044 KB Output is correct
25 Correct 4 ms 13404 KB Output is correct
26 Correct 4 ms 15448 KB Output is correct
27 Correct 3 ms 13404 KB Output is correct
28 Correct 8 ms 30340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 3 ms 13400 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 5 ms 13404 KB Output is correct
6 Correct 4 ms 13404 KB Output is correct
7 Correct 4 ms 13404 KB Output is correct
8 Correct 4 ms 13404 KB Output is correct
9 Correct 6 ms 15452 KB Output is correct
10 Correct 5 ms 15452 KB Output is correct
11 Correct 5 ms 15452 KB Output is correct
12 Correct 5 ms 15448 KB Output is correct
13 Correct 5 ms 15452 KB Output is correct
14 Correct 12 ms 30312 KB Output is correct
15 Correct 15 ms 30300 KB Output is correct
16 Correct 15 ms 30260 KB Output is correct
17 Correct 13 ms 30548 KB Output is correct
18 Correct 17 ms 30300 KB Output is correct
19 Correct 19 ms 30280 KB Output is correct
20 Correct 18 ms 30300 KB Output is correct
21 Correct 11 ms 30556 KB Output is correct
22 Correct 12 ms 30408 KB Output is correct
23 Correct 13 ms 30552 KB Output is correct
24 Correct 261 ms 98356 KB Output is correct
25 Correct 487 ms 91536 KB Output is correct
26 Correct 551 ms 93388 KB Output is correct
27 Correct 418 ms 91260 KB Output is correct
28 Correct 181 ms 91400 KB Output is correct
29 Correct 215 ms 99876 KB Output is correct
30 Correct 231 ms 99884 KB Output is correct
31 Correct 18 ms 30040 KB Output is correct
32 Correct 435 ms 90976 KB Output is correct
33 Correct 3 ms 13404 KB Output is correct
34 Correct 4 ms 15548 KB Output is correct
35 Correct 307 ms 93232 KB Output is correct
36 Correct 4 ms 13400 KB Output is correct
37 Correct 9 ms 30248 KB Output is correct
38 Correct 132 ms 92900 KB Output is correct
39 Correct 168 ms 90476 KB Output is correct