设为首页 加入收藏

TOP

hdu1180 诡异的楼梯(BFS+优先队列)
2015-11-21 00:56:12 来源: 作者: 【 】 浏览:1
Tags:hdu1180 诡异 楼梯 BFS 优先 队列

?

诡异的楼梯

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 10827 Accepted Submission(s): 2694



Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.

Input 测试数据有多组,每组的表述如下:
第一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.

Output 只有一行,包含一个数T,表示到达目标的最短时间.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.

Sample Input
5 5
**..T
**.*.
..|..
.*.*.
S....

Sample Output
7

HintHint 
地图如下:
\

?

?

开始wa了很多次,原来是题意没搞清,还以为只有例子中' |' 这一种楼梯的符号,原来还有’-‘!

要点: 优先队列的构造,判断条件的抽取,特殊条件(等待一秒在前进)

优先队列,使得当前时间小的先出队列,

所以当前坐标的结构体需要这样写

?

struct node{
	int x,y,step; 
        //两种方式重载小于号
	/* friend bool operator <(node a,node b){
		return a.step>b.step;
	} */
	bool operator <(const node &a)const{
		return step>a.step;//步数小的先出队列
	}
}init;
因为使用了优先队列,所以不再用传统的vis标记是否走过,对于一个已经走过的位置,如果当前step小于之前的step,则可以重复走,

?

所以用statue[][]这个数组储存走过位置的当前步数;

判断条件很多,我们把它抽取到函数judge

?

bool judge(node no){
?? ?//如果超出边界,或者该位置是墙,或者当前步数大于之前步数,返回false
?? ?if(no.x<0||no.x>=n||no.y<0||no.y>=m||map[no.x][no.y]=='*'||(statue[no.x][no.y]&&no.step>=statue[no.x][no.y]))
?? ??? ?return false;
?? ?return true;
}
如果遇到楼梯方向与前进方向不一致,可以等待一秒,再前进

?

将这个情况与 一致时的情况放在一起,只是步数不同

【源代码】

?

#include 
  
   
#include 
   
     #include 
    
      using namespace std; int n,m; const int maxn = 21; int stx,sty,gx,gy; char map[maxn][maxn]; bool vis[maxn][maxn]; int statue[maxn][maxn]; int dx[4]={0,0,1,-1}; int dy[4]={-1,1,0,0}; struct node{ int x,y,step; /* friend bool operator <(node a,node b){ return a.step>b.step; } */ bool operator <(const node &a)const{ return step>a.step;//步数小的先出队列 } }init; bool judge(node no){ //如果超出边界,或者该位置是墙,或者当前步数大于之前步数,返回false if(no.x<0||no.x>=n||no.y<0||no.y>=m||map[no.x][no.y]=='*'||(statue[no.x][no.y]&&no.step>=statue[no.x][no.y])) return false; return true; } int bfs(){ init.x=stx;init.y=sty;init.step=0; statue[init.x][init.y]=1; priority_queue
     
      pq; while(!pq.empty()) pq.pop(); pq.push(init); while(!pq.empty()){ node past=pq.top(); node now; pq.pop(); for(int i=0;i<4;i++){ now.x=past.x+dx[i]; now.y=past.y+dy[i]; now.step=past.step+1; if(!judge(now)) continue; if(map[now.x][now.y]=='|'){ if(now.x==past.x&&(past.step & 1)==0) //当横着走,且 | 不变 now.step++; if(now.y==past.y&&(past.step & 1)==1)//当 竖着走 且 |变 { now.step++; } now.x+=dx[i]; now.y+=dy[i]; } else if(map[now.x][now.y]=='-'){ if(now.x==past.x&&(past.step & 1)==1) //当横着走,且 -变 now.step++; if(now.y==past.y&&(past.step & 1)==0)//当竖着走 且-不变 now.step++; now.x+=dx[i]; now.y+=dy[i]; } if(!judge(now)) continue; if(map[now.x][now.y]=='T') return now.step; statue[now.x][now.y]=now.step; pq.push(now); } } return 0; } int main(){ while(cin>>n>>m){ for(int i=0;i
      
       >map[i][j]; if(map[i][j]=='S') stx=i,sty=j; if(map[i][j]=='T') gx=i,gy=j; } memset(statue,0,sizeof(statue)); int ans=bfs(); cout<
       
        

?

?

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu2489Minimal Ratio Tree 最小.. 下一篇POJ2528 Mayor's posters 线..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: