现在的位置: 首页 > 综合 > 正文

杭电1180

2018年02月01日 ⁄ 综合 ⁄ 共 2638字 ⁄ 字号 评论关闭

诡异的楼梯

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


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
地图如下:
 

下面代码已经通过AC:

本题使用BFS进行搜索,不过关键在于,当楼梯不可走的时候需要等待一个时间,这样在下一个时间点该楼梯就一定可以通过,

对于停留一个时间的处理是通过将当前节点的时间增加一然后重新放入优先级队列进行实现的。

#include <iostream>
#include <queue>
using namespace std;

char map[20][20];
bool used[20][20];
int m, n;
typedef struct node 
{
    int row, col;
    int time;
    friend bool operator < (node n1, node n2)
    {
        return n1.time > n2.time;
    }
}Node;

typedef struct 
{
    int row, col;
}Point;
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
Point start , end;

bool check(Node n, char ch, int i )
{
    if ((n.time%2 == 0 && ch == '|') || (n.time%2 == 1 && ch == '-') )
    {
        if (i == 0 || i == 1)
            return true;
    }
    else
        if ((n.time%2 == 0 && ch == '-') || (n.time%2 == 1 && ch == '|'))
        {
            if (i == 2 || i == 3)
                return true;
        }
    return false;
}

int bfs()
{
    if (start.row == end.row && start.col == end.col)
        return 0;
    priority_queue<Node> Q;
    Node temp ;
    temp.row = start.row;
    temp.col = start.col;
    temp.time = 0;
    Q.push(temp);
    used[start.row][start.col] = true;
    
    while (!Q.empty()) {
        Node curNode = Q.top();
        Q.pop();
        for (int i = 0;  i < 4; ++ i)
        {
            int nextRow = curNode.row + dir[i][0];
            int nextCol = curNode.col + dir[i][1];
            if (nextRow >= 0 && nextRow < m && nextCol >= 0 && nextCol < n && used[nextRow][nextCol] != true && map[nextRow][nextCol] != '*')
            {
                if (map[nextRow][nextCol] == '|' || map[nextRow][nextCol] == '-')
                {
                    if (check(curNode, map[nextRow][nextCol], i))
                    {
                        nextRow = nextRow + dir[i][0];
                        nextCol = nextCol + dir[i][1];
                        if (used[nextRow][nextCol] == true || map[nextRow][nextCol] == '*')
                        {
                            continue;
                        }
                    }
                    else//如果楼梯不可以通过,等待一个时间步长
                    {
                        curNode.time = curNode.time + 1;
                        Q.push(curNode);//将时间增加一后放入优先级队列中,表示在原地停留一分钟
                        curNode.time = curNode.time - 1;//恢复原来的时间数值,用于扩展其他的节点
                        continue;

                    }
                }
                
                Node addNode ;
                addNode.row = nextRow;
                addNode.col = nextCol;
                addNode.time = curNode.time + 1;
                
                //判断是否达到终点
                if (nextRow == end.row && nextCol == end.col)
                    return addNode.time;
                else
                {
                    used[nextRow][nextCol] = true;
                    Q.push(addNode);
        
                }
            }
        }
    }
    return -1;
}

int main()
{
    while (cin >> m >> n) {
        
        for (int i =0; i < m; ++ i) 
            for (int j = 0; j < n; ++ j)
            {
                cin >> map[i][j];
                used[i][j] = false;
                if (map[i][j] == 'S')
                {
                    start.row = i;
                    start.col = j;
                }
                else
                    if (map[i][j] == 'T')
                    {
                        end.row = i;
                        end.col = j;
                    }
            }
        int result = bfs();
        if (result!= -1)
            cout << result << endl;
    }
    return 0;
}

抱歉!评论已关闭.