HDU 1180 诡异的楼梯

诡异的楼梯

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

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

Source
 
Recommend
JGShining
 
思路:
这个题目思路挺简单的,就是BFS 加优先队列,但是我却调试到了凌晨一点才过,本人觉得
挺坑爹,关键在于在楼梯处怎么处理,因为可以在原处等待直到可以过去为止,看别人可以用
普通队列,我没怎么知道怎么去做
 
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
char map[22][22];
int hash[22][22];
int n,m;
int move[4][2] = {1,0,-1,0,0,1,0,-1};
int sx,sy,tx,ty;
struct Node
{
    int x,y;
    int time;
    friend bool operator < (const Node & a,const Node & b)
    {
        return a.time > b.time;
    }
};
void BFS()
{
    priority_queue < Node > q;
    Node top;top.x = sx;top.y = sy;top.time = 0;
    q.push(top);
    while(!q.empty())
    {
        Node temp;
        temp = q.top();q.pop();
        //printf("%d %d have %d ",temp.x,temp.y,temp.time);
        if(temp.x == tx && temp.y == ty)
        {
               printf("%d ",temp.time);
               break;
        }
        for(int i = 0;i < 4;i ++)
        {
            int x = temp.x + move[i][0];int y = temp.y + move[i][1];
            //printf("%d                %d %c ",x,y,map[x][y]);
            int time = temp.time;
            if(map[x][y] != '*' && hash[x][y] == 0 && x >= 1 && x <= n
            && y >= 1 && y <= m)
            {
                if(map[x][y] == '.' || map[x][y] == 'T')
                {
                    Node xin;xin.x = x;xin.y = y;xin.time = time + 1;
                    hash[x][y] = 1;
                    //if(temp.x == 2 && temp.y == 5)
                    //printf("%d %d %c ",x,y,map[x][y]);
                    q.push(xin);
                }
             if(hash[x + move[i][0]][y + move[i][1]] == 0)
              {
                if(map[x][y] == '|')
                {
                    //printf("%d  %d  %c ",x,y,map[x][y]);
                    //hash[x][y] = 1;
                    if(time % 2 == 0)
                     {
                        if(x == temp.x)
                        {
                            time = time + 2;
                            y += move[i][1];
                        }
                        if(y == temp.y)
                        {
                            time = time + 1;
                            x += move[i][0];
                        }
                    }
                    else
                    {
                    if(time % 2 == 1)
                    {
                        if(x == temp.x)
                        {
                            time = time + 1;
                            y += move[i][1];
                        }
                        if(y == temp.y)
                        {
                            time = time + 2;
                            x += move[i][0];
                        }
                     }
                    }
                     Node xin;xin.x = x;xin.y = y;xin.time = time;
                     q.push(xin);
                     //printf("%d %d ",x,y);
                     hash[x][y] = 1;
                  }
                 if(map[x][y] == '-')
                 {
                    //printf("%d  %d  %c ",x,y,map[x][y]);
                    //hash[x][y] = 1;
                    if(time % 2 == 0)
                    {
                        if(x == temp.x)
                        {
                            time = time + 1;
                            y += move[i][1];
                        }
                        if(y == temp.y)
                        {
                            time = time + 2;
                            x += move[i][0];
                        }
                    }
                    else
                    {
                    if(time % 2 == 1)
                    {
                        if(x == temp.x)
                        {
                            time = time + 2;
                            y += move[i][1];
                        }
                        if(y == temp.y)
                        {
                            time = time + 1;
                            x += move[i][0];
                        }
                     }
                    }
                     Node xin;xin.x = x;xin.y = y;xin.time = time;
                     q.push(xin);
                     //printf("%d %d ",x,y);
                     hash[x][y] = 1;
                 }
                }
             }
         }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(hash,0,sizeof(hash));
        for(int i = 1;i <= n;i ++)
           for(int j = 1;j <= m;j ++)
           {
              scanf(" %c",&map[i][j]);
              if(map[i][j] == 'S')
              {
                    sx = i;sy = j;
                    hash[i][j] = 1;
              }
              if(map[i][j] == 'T')
              {
                    tx = i;ty = j;
              }
            }
        BFS();
    }
    return 0;
}
           
             
 
原文地址:https://www.cnblogs.com/GODLIKEING/p/3284058.html