HDU 1180 诡异的楼梯 (DFS)

来源:互联网 发布:欧冠足球2 球员数据 编辑:程序博客网 时间:2024/06/06 21:38

诡异的楼梯

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


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

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

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

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

Sample Output
7
Hint
Hint
地图如下:注意楼梯用过了1遍之后还可以再用 因为用来横着走之后还可以竖着走 所以不标记楼梯是否走过#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>#include<math.h>#include<queue>using namespace std;int flag[25][25],sum,x1,z1,dir[4][2]={0,-1,-1,0,0,1,1,0};char map[25][25];typedef struct node{    int x,y,step,f;}node;queue<node>a;void clear(){    while(!a.empty())    a.pop();}int bfs(int x1,int z1){    node now,next;    now.x=x1;now.y=z1;now.step=0;now.f=0;    a.push(now);    while(!a.empty())    {        now=a.front();        a.pop();        if(now.f==1)        {            now.f=0;            a.push(now);            continue;        }        for(int i=0;i<4;i++)        {            next.x=now.x+dir[i][0];            next.y=now.y+dir[i][1];            next.step=now.step+1;            next.f=0;            int xx=next.x;            int yy=next.y;            if(map[xx][yy]=='*'||flag[xx][yy]==1)            continue;            if(map[xx][yy]=='T')            return next.step;            if(map[xx][yy]=='|')            {                if((now.step%2==0&&(i==1||i==3))||now.step%2==1&&(i==0||i==2))                {                    next.x=next.x+dir[i][0];                    next.y=next.y+dir[i][1];                    if(map[next.x][next.y]=='*'||flag[xx][yy]==1)                    continue;                    if(map[next.x][next.y]=='T')                    return next.step;                    flag[next.x][next.y]=1;                    a.push(next);                }                else                {                    next.x=next.x+dir[i][0];                    next.y=next.y+dir[i][1];                    next.step=next.step+1;                    next.f=1;                    if(map[next.x][next.y]=='*'||flag[xx][yy]==1)                    continue;                    if(map[next.x][next.y]=='T')                    return next.step;                    flag[next.x][next.y]=1;                    a.push(next);                }                continue;            }            if(map[xx][yy]=='-')            {                if((now.step%2==0&&(i==0||i==2))||now.step%2==1&&(i==1||i==3))                {                    next.x=next.x+dir[i][0];                    next.y=next.y+dir[i][1];                    if(map[next.x][next.y]=='T')                    return next.step;                    if(map[next.x][next.y]=='*'||flag[xx][yy]==1)                    continue;                    flag[next.x][next.y]=1;                    a.push(next);                }                else                {                    next.x=next.x+dir[i][0];                    next.y=next.y+dir[i][1];                    if(map[next.x][next.y]=='*'||flag[xx][yy]==1)                    continue;                    next.step=next.step+1;                    next.f=1;                    if(map[next.x][next.y]=='T')                    return next.step;                    flag[next.x][next.y]=1;                    a.push(next);                }                continue;            }            flag[xx][yy]=1;            a.push(next);        }    }}int main(){    int n,m,i,j;    while(~scanf("%d%d",&n,&m))    {        sum=0;        memset(flag,0,sizeof(flag));        memset(map,'*',sizeof(map));        clear();        for(i=1;i<=n;i++)        for(j=1;j<=m;j++)        {            cin>>map[i][j];            if(map[i][j]=='S')            {                x1=i;                z1=j;            }        }        flag[x1][z1]=1;        sum=bfs(x1,z1);        cout<<sum<<endl;    }    return 0;}
0 0