挺简单的一道广搜题,只要用判断时间是偶数还是奇数就可以判断楼梯的方位,但是我这傻逼居然写了那么久啊那么久,我果然秀逗了,,,,
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> using namespace std; #define MAXN 30 int map[MAXN][MAXN],n,m; bool vis[MAXN][MAXN]; struct tt { int x,y,step; }; queue<tt>q; int xx[4]={0,0,1,-1}; int yy[4]={1,-1,0,0}; int bfs(int x,int y) { while(!q.empty()) q.pop(); memset(vis,false,sizeof(vis)); tt front,rear,temp; front.x=x;front.y=y;front.step=0; q.push(front); vis[x][y]=true; while(!q.empty()) { temp=q.front(); q.pop(); if(map[temp.x][temp.y]=='T') return temp.step; for(int i=0;i<4;i++) { rear.x=temp.x+xx[i]; rear.y=temp.y+yy[i]; rear.step=temp.step+1; if(rear.x>=0&&rear.x<n&&rear.y>=0&&rear.y<m&&map[rear.x][rear.y]!='*'&&vis[rear.x][rear.y]==false) { if(map[rear.x][rear.y]=='.'||map[rear.x][rear.y]=='T') { q.push(rear); vis[rear.x][rear.y]=true; } if(xx[i]==0) { if(map[rear.x][rear.y]=='-') { if(rear.y+yy[i]>=0&&rear.y+yy[i]<m&&(map[rear.x][rear.y+yy[i]]=='.'||map[rear.x][rear.y+yy[i]]=='T')&&vis[rear.x][rear.y+yy[i]]==false) { if(rear.step%2==0) rear.y=rear.y-yy[i]; else rear.y=rear.y+yy[i],vis[rear.x][rear.y]=true; q.push(rear); } } else if(map[rear.x][rear.y]=='|') { if(rear.y+yy[i]>=0&&rear.y+yy[i]<m&&(map[rear.x][rear.y+yy[i]]=='.'||map[rear.x][rear.y+yy[i]]=='T')&&vis[rear.x][rear.y+yy[i]]==false) { if(rear.step%2) rear.y=rear.y-yy[i]; else rear.y=rear.y+yy[i],vis[rear.x][rear.y]=true; q.push(rear); } } } if(yy[i]==0) { if(map[rear.x][rear.y]=='|') { if(rear.x+xx[i]>=0&&rear.x+xx[i]<n&&(map[rear.x+xx[i]][rear.y]=='.'||map[rear.x+xx[i]][rear.y]=='T')&&vis[rear.x+xx[i]][rear.y]==false) { if(rear.step%2==0) rear.x=rear.x-xx[i]; else rear.x=rear.x+xx[i],vis[rear.x][rear.y]=true; q.push(rear); } } else if(map[rear.x][rear.y]=='-') { if(rear.x+xx[i]>=0&&rear.x+xx[i]<n&&(map[rear.x+xx[i]][rear.y]=='.'||map[rear.x+xx[i]][rear.y]=='T')&&vis[rear.x+xx[i]][rear.y]==false) { if(rear.step%2) rear.x=rear.x-xx[i]; else rear.x=rear.x+xx[i],vis[rear.x][rear.y]=true; q.push(rear); } } } } } } return 0; } int main() { int i,j,sx,sy,ans; while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++) { getchar(); for(j=0;j<m;j++) { scanf("%c",&map[i][j]); if(map[i][j]=='S') { sx=i;sy=j; } } } ans=bfs(sx,sy); printf("%d ",ans); } return 0; } /* 1 3 S|T 1 3 S-T 20 20 S.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.| |.|.|.|.|.|.|.|.|.|. .|.|.|.|.|.|.|.|.|.T 5 5 **..T **.*. ..|.. .*.*. S.... */