标签:
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1180
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 12255 Accepted Submission(s): 3036
#include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; int dir[4][2]= {{-1,0},{0,1},{1,0},{0,-1}}; bool vis[21][21]; char map[22][22]; int m,n,sx,sy; struct node { short x,y; short int time; }; bool inside(int x,int y) { if(x>=0&&x<m&&y>=0&&y<n) return 1; return 0; } void BFS() { queue<node>q; node sta; sta.x=sx; sta.y=sy; sta.time=0; vis[sx][sy]=1; q.push(sta); while(!q.empty()) { node now=q.front(); q.pop(); if(map[now.x][now.y]==‘T‘) { printf("%d\n",now.time); return; } for(int i=0; i<4; i++) { node next; next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; next.time=now.time+1; if(inside(next.x,next.y)&&!vis[next.x][next.y]&&map[next.x][next.y]!=‘*‘) { if(map[next.x][next.y]==‘|‘) { next.x+=dir[i][0]; next.y+=dir[i][1]; if(inside(next.x,next.y)&&!vis[next.x][next.y]&&map[next.x][next.y]!=‘*‘) {if((now.time%2==0&&(i==1||i==3))||(now.time%2==1&&(i==0||i==2))) next.time+=1;} else continue; } else if(map[next.x][next.y]==‘-‘) { next.x+=dir[i][0]; next.y+=dir[i][1]; if(inside(next.x,next.y)&&!vis[next.x][next.y]&&map[next.x][next.y]!=‘*‘) {if((now.time%2==0&&(i==0||i==2))||(now.time%2==1&&(i==1||i==3))) next.time+=1;} else continue; } vis[next.x][next.y]=1; q.push(next); } } } } int main() { while(scanf("%d%d",&m,&n)!=EOF) { memset(vis,0,sizeof(vis)); for(int i=0; i<m; i++) scanf("%s",map[i]); for(int i=0; i<m; i++) for(int j=0; j<n; j++) { if(map[i][j]==‘S‘) { sx=i; sy=j; } } BFS(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jasonlixuetao/p/5468795.html