Submission #203549

#TimeUsernameProblemLanguageResultExecution timeMemory
203549mdn2002Portals (BOI14_portals)C++14
20 / 100
13 ms4728 KiB
#include<bits/stdc++.h> using namespace std; int n,m,sx,sy,fx,fy,dis[1050][1050],xx[]={1,-1,0,0},yy[]={0,0,1,-1}; int up[1050][1050],dw[1050][1050],lf[1050][1050],rt[1050][1050]; char gr[1050][1050]; void bfs() { queue<pair<int,int> >q; q.push({sx,sy}); dis[sx][sy]=0; while(q.size()) { pair<int,int>z=q.front(); q.pop(); int wl=0,x=z.first,y=z.second; if(gr[x][y]=='#')continue; for(int i=0;i<4;i++) { int tx=x+xx[i],ty=y+yy[i]; if(0>tx||tx>=n||0>ty||ty>=m) { wl++; continue; } if(gr[tx][ty]=='#') { wl++; continue; } if(dis[tx][ty]>dis[x][y]+1) { dis[tx][ty]=dis[x][y]+1; q.push({tx,ty}); } } if(wl) { int tx,ty; tx=up[x][y],ty=y; if(dis[tx][ty]>dis[x][y]+1) { dis[tx][ty]=dis[x][y]+1; q.push({tx,ty}); } tx=dw[x][y],ty=y; if(dis[tx][ty]>dis[x][y]+1) { dis[tx][ty]=dis[x][y]+1; q.push({tx,ty}); } tx=x,ty=lf[x][y]; if(dis[tx][ty]>dis[x][y]+1) { dis[tx][ty]=dis[x][y]+1; q.push({tx,ty}); } tx=x,ty=rt[x][y]; if(dis[tx][ty]>dis[x][y]+1) { dis[tx][ty]=dis[x][y]+1; q.push({tx,ty}); } } } } int main() { cin>>n>>m; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>gr[i][j]; if(gr[i][j]=='S')sx=i,sy=j; if(gr[i][j]=='C')fx=i,fy=j; dis[i][j]=1e9; } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(i==0) { up[i][j]=i; continue; } if(gr[i-1][j]=='#')up[i][j]=i; else up[i][j]=up[i-1][j]; } } for(int i=n-1;i>=0;i--) { for(int j=0;j<m;j++) { if(i==n-1) { dw[i][j]=i; continue; } if(gr[i+1][j]=='#')dw[i][j]=i; else dw[i][j]=dw[i+1][j]; } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(j==0) { lf[i][j]=j; continue; } if(gr[i][j-1]=='#')lf[i][j]=j; else lf[i][j]=lf[i][j-1]; } } for(int i=0;i<n;i++) { for(int j=m-1;j>=0;j--) { if(j==m-1) { rt[i][j]=j; continue; } if(gr[i][j+1]=='#')rt[i][j]=j; else rt[i][j]=rt[i][j+1]; } } bfs(); cout<<dis[fx][fy]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...