Submission #203554

#TimeUsernameProblemLanguageResultExecution timeMemory
203554mdn2002Portals (BOI14_portals)C++14
20 / 100
13 ms4732 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; for(int i=0;i<4;i++) { int tx=x+xx[i],ty=y+yy[i]; 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>0) { 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+1;i++) { for(int j=0;j<=m+1;j++)gr[i][j]='#'; } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { cin>>gr[i][j]; if(gr[i][j]=='S') { sx=i,sy=j; gr[i][j]='.'; } if(gr[i][j]=='C') { fx=i,fy=j; gr[i][j]='.'; } dis[i][j]=1e9; } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(gr[i-1][j]=='#')up[i][j]=i; else up[i][j]=up[i-1][j]; } } for(int i=n;i>=1;i--) { for(int j=1;j<=m;j++) { if(gr[i+1][j]=='#')dw[i][j]=i; else dw[i][j]=dw[i+1][j]; } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(gr[i][j-1]=='#')lf[i][j]=j; else lf[i][j]=lf[i][j-1]; } } for(int i=1;i<=n;i++) { for(int j=m;j>=1;j--) { 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...