首页 > 代码库 > 诡异的楼梯(bfs)
诡异的楼梯(bfs)
诡异的楼梯
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 9045 Accepted Submission(s): 2238
Problem Description
Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.
Input
测试数据有多组,每组的表述如下:
第一行有两个数,M和N,接下来是一个M行N列的地图,‘*‘表示障碍物,‘.‘表示走廊,‘|‘或者‘-‘表示一个楼梯,并且标明了它在一开始时所处的位置:‘|‘表示的楼梯在最开始是竖直方向,‘-‘表示的楼梯在一开始是水平方向.地图中还有一个‘S‘是起点,‘T‘是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在‘.‘或‘S‘和‘T‘所标记的格子内.
第一行有两个数,M和N,接下来是一个M行N列的地图,‘*‘表示障碍物,‘.‘表示走廊,‘|‘或者‘-‘表示一个楼梯,并且标明了它在一开始时所处的位置:‘|‘表示的楼梯在最开始是竖直方向,‘-‘表示的楼梯在一开始是水平方向.地图中还有一个‘S‘是起点,‘T‘是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在‘.‘或‘S‘和‘T‘所标记的格子内.
Output
只有一行,包含一个数T,表示到达目标的最短时间.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
Sample Input
5 5
**..T
**.*.
..|..
.*.*.
S....
Sample Output
7 Hint地图如下:
优先队列+bfs
ps:http://acm.hdu.edu.cn/showproblem.php?pid=1180
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int stair[22][22];struct node{ int x,y; int step; friend bool operator<(node n1,node n2) { return n2.step<n1.step; }};int map[22][22];int n,m;int dir[4][2]={1,0, 0,-1, -1,0, 0,1};int x_s,y_s;int x_e,y_e;int judge(int x,int y){ if(x<0 || x>=n || y<0 || y>=m) return 1; if(map[x][y]==1) return 1; if(map[x][y]==2) return 2; return 0;}int judge2(int step,int x2,int y2,int k){ if((step+stair[x2][y2])%2!=0 && (k==0 || k==2)) return 1; if((step+stair[x2][y2])%2==0 && (k==1 || k==3)) return 1; return 0;}int BFS(){ priority_queue<node>q; node cur,next; int i; int temp; int t_x,t_y; cur.x=x_s; cur.y=y_s; cur.step=0; map[cur.x][cur.y]=1; q.push(cur); while(!q.empty()) { cur=q.top(); q.pop(); if(cur.x==x_e && cur.y==y_e) return cur.step; for(i=0;i<4;i++) { next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; temp=judge(next.x,next.y); if(temp==1) continue; if(!temp) { next.step=cur.step+1; map[next.x][next.y]=1; q.push(next); } else { t_x=next.x+dir[i][0]; t_y=next.y+dir[i][1]; if(judge(t_x,t_y)==1) continue; if(judge2(cur.step,next.x,next.y,i)) { next.x=t_x; next.y=t_y; next.step=cur.step+1; } else { next.x=t_x; next.y=t_y; next.step=cur.step+2; } q.push(next); } } } return -1;}int main(){ int i,l; int ans; char str[25]; while(scanf("%d%d",&n,&m)!=-1) { for(i=0;i<n;i++) { scanf("%s",str); for(l=0;str[l];l++) { if(str[l]==‘S‘) {x_s=i;y_s=l;map[i][l]=0;} else if(str[l]==‘T‘){x_e=i;y_e=l;map[i][l]=0;} else if(str[l]==‘|‘){stair[i][l]=1;map[i][l]=2;} else if(str[l]==‘-‘){stair[i][l]=0;map[i][l]=2;} else if(str[l]==‘*‘)map[i][l]=1; else if(str[l]==‘.‘)map[i][l]=0; } } ans=BFS(); printf("%d\n",ans); } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。