诡异的楼梯

来源:互联网 发布:3维家居设计软件 编辑:程序博客网 时间:2024/05/17 01:43

诡异的楼梯

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 24   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Hint

Hint
地图如下:

Source

优先队列+BFS

从起点到终点问最短时间,主要是对于电梯的处理,用c=map,当map为‘|’时c=‘-’,反之亦然,在c=‘-'和’|‘时用方向数组判断是否横向还是纵向,如果相同方向,step++


#include<cstdio>#include<queue>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int n,m,k,sx,sy;int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};char str[25][25];int map[25][25];struct node{   int x,y,step;   friend bool operator<(node a,node b)   {       return a.step>b.step;   }};int bfs(int x,int y){   priority_queue<node> Q;   node now,next;   now.x=x;   now.y=y;   now.step=0;   Q.push(now);   char c;   int i;   while(!Q.empty())   {       now=Q.top();       Q.pop();       for(i=0;i<=4;i++)       {           next.x=now.x+dir[i][0];           next.y=now.y+dir[i][1];           next.step=now.step+1;           if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&(str[next.x][next.y]=='-'||str[next.x][next.y]=='|'))           {               if(!(now.step%2))                   c=str[next.x][next.y];               else if(now.step%2)               {                   if(str[next.x][next.y]=='|')                       c='-';                   else                       c='|';               }               if(((dir[i][0]==-1&&dir[i][1]==0)||(dir[i][0]==1&&dir[i][1]==0))&&c=='|')               {                   next.x+=dir[i][0];                   next.y+=dir[i][1];               }               else if(((dir[i][0]==0&&dir[i][1]==-1)||(dir[i][0]==0&&dir[i][1]==1))&&c=='-')               {                   next.x+=dir[i][0];                   next.y+=dir[i][1];               }               else if(((dir[i][0]==-1&&dir[i][1]==0)||(dir[i][0]==1&&dir[i][1]==0))&&c=='-')               {                   next.x+=dir[i][0];                   next.y+=dir[i][1];                   next.step++;               }               else if(((dir[i][0]==0&&dir[i][1]==-1)||(dir[i][0]==0&&dir[i][1]==1))&&c=='|')               {                   next.x+=dir[i][0];                   next.y+=dir[i][1];                   next.step++;               }           }           if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&map[next.x][next.y]==0)           {               if(str[next.x][next.y]=='T')                   return next.step;               else if(str[next.x][next.y]=='.')               {                   map[next.x][next.y]=1;                   Q.push(next);               }           }       }   }   return -1;}int main(){   int i,j,time;   while(scanf("%d%d",&n,&m)!=EOF)   {       for(i=0;i<n;i++)       {           scanf("%s",str[i]);           for(j=0;j<m;j++)           {               if(str[i][j]=='S')               {                   sx=i;sy=j;               }           }       }       memset(map,0,sizeof(map));       map[sx][sy]=1;       time=bfs(sx,sy);       printf("%d\n",time);   }   return 0;}


0 0
原创粉丝点击