hdu 1180 詭異的樓梯
阿新 • • 發佈:2019-02-11
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; int n,m; int qx,qy,jx,jy; char map[30][30]; int vis[30][30]; struct node { int x,y,step; } q[100000000],t,f; int dx[]= {1,0,-1,0}; int dy[]= {0,1,0,-1}; void bfs() { int low=0,top=1; memset(vis,0,sizeof(vis)); q[0].x=qx; q[0].y=qy; q[0].step=0; vis[qx][qy]=1; while(low<top) { t=q[low++]; if(t.x==jx&&t.y==jy) { cout<<t.step<<endl; return ; } for(int i=0; i<4; i++) { f.x=t.x+dx[i]; f.y=t.y+dy[i]; if(f.x>=0&&f.x<n&&f.y>=0&&f.y<m&&!vis[f.x][f.y]&&map[f.x][f.y]!='*') { if(map[f.x][f.y]=='.'||map[f.x][f.y]=='T') { f.step=t.step+1; q[top++]=f; vis[f.x][f.y]=1; } else if(map[f.x][f.y]=='|') { int zhuan=t.step%2; int s=i%2; if((s==0&&zhuan==0)||(s==1&&zhuan==1)) { f.x=f.x+dx[i]; f.y=f.y+dy[i]; if(f.x>=0&&f.x<n&&f.y>=0&&f.y<m&&!vis[f.x][f.y]&&map[f.x][f.y]!='*') { f.step=t.step+1; q[top++]=f; vis[f.x][f.y]=1; } } else if((s==1&&zhuan==0)||(s==0&&zhuan==1)) { f.x=t.x; f.y=t.y; f.step=t.step+1; q[top++]=f; } } else if(map[f.x][f.y]=='-') { int zhuan=t.step%2; int s=i%2; if((s==0&&zhuan==0)||(s==1&&zhuan==1)) { f.x=t.x; f.y=t.y; f.step=t.step+1; q[top++]=f; } else if((s==1&&zhuan==0)||(s==0&&zhuan==1)) { f.x=f.x+dx[i]; f.y=f.y+dy[i]; if(f.x>=0&&f.x<n&&f.y>=0&&f.y<m&&!vis[f.x][f.y]&&map[f.x][f.y]!='*') { f.step=t.step+1; q[top++]=f; vis[f.x][f.y]=1; } } } } } } } int main() { while(~scanf("%d %d",&n,&m)) { for(int i=0; i<n; i++) scanf("%s",map[i]); for(int i=0; i<n; i++) for(int j=0.; j<m; j++) { if(map[i][j]=='S') { qx=i; qy=j; } else if(map[i][j]=='T') { jx=i; jy=j; } } bfs(); } return 0; }