HDU【1180】诡异的楼梯

来源:互联网 发布:网络专升本 编辑:程序博客网 时间:2024/05/01 02:54
诡异的楼梯
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

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

#include <cstdio>#include <cstring>#include <iostream>#include <queue>using namespace std;const int maxn = 25;int n,m,sx,sy,str[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};char map[maxn][maxn];int flag[maxn][maxn];struct Node{    int x,y,t;    bool operator < (const Node no)const    {        return t > no.t;    }}node,pre;bool judge(int x,int y){    if(x >= 0 && x < n && y >= 0 && y < m && !flag[x][y] && map[x][y] != '*')        return true;    return false;}void BFS(int sx,int sy){    priority_queue <Node> q;    node.x = sx;    node.y = sy;    node.t = 0;    flag[sx][sy] = 1;    q.push(node);    while(!q.empty())    {        node = q.top();        q.pop();        if(map[node.x][node.y] == 'T')        {            printf("%d\n",node.t);            return ;        }        for(int i = 0; i < 4; i++)        {            pre.x = node.x + str[i][0];            pre.y = node.y + str[i][1];            pre.t = node.t + 1;            if(judge(pre.x,pre.y))            {                if(map[pre.x][pre.y] == '-')

                {

//通过奇偶判断该楼梯能否直接过去,能直接过去,t不变,不能直接过去,需要等一秒,即t加1.                    if((pre.t%2 == 0 && str[i][0]) || (pre.t%2 == 1 && str[i][1]))                    {                        pre.x += str[i][0];                        pre.y += str[i][1];                        if(judge(pre.x,pre.y))                        {                            q.push(pre);                            flag[pre.x][pre.y] = 1;                        }                    }                    else                    {                        pre.x += str[i][0];                        pre.y += str[i][1];                        if(judge(pre.x,pre.y))                        {                            pre.t++;                            q.push(pre);                            flag[pre.x][pre.y] = 1;                        }                    }                    continue;  

//如果为楼梯无论能否走都不能往下执行,那样会往队列中加入不需要的元素                }                else if(map[pre.x][pre.y] == '|')                {                    if((pre.t%2 == 0 && str[i][1]) || (pre.t%2 == 1 && str[i][0]))                    {                        pre.x += str[i][0];                        pre.y += str[i][1];                        if(judge(pre.x,pre.y))                        {                            q.push(pre);                            flag[pre.x][pre.y] = 1;                        }                    }                    else                    {                        pre.x += str[i][0];                        pre.y += str[i][1];                        if(judge(pre.x,pre.y))                        {                            pre.t++;                            q.push(pre);                            flag[pre.x][pre.y] = 1;                        }                    }                    continue;                }                q.push(pre);                flag[pre.x][pre.y] = 1;            }        }    }}int main(){    while(scanf("%d%d",&n,&m) != EOF)    {        for(int i = 0; i < n; i++)            scanf("%s",map[i]);        for(int i = 0; i < n; i++)            for(int j = 0; j < m; j++)                if(map[i][j] == 'S')                {                    sx = i;                    sy = j;                }        memset(flag,0,sizeof(flag));        BFS(sx,sy);    }    return 0;}

1 0