同样优先队列的广搜题 hdu 4198

来源:互联网 发布:美国人学中文 知乎 编辑:程序博客网 时间:2024/06/05 18:45

hdu  4198

Quick out of the Harbour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 584    Accepted Submission(s): 265


Problem Description
Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect and repair the ship. Now, a few days later, the pirates are getting landsick(Pirates get landsick when they don't get enough of the ships' rocking motion. That's why pirates often try to simulate that motion by drinking rum.). Before all of the pirates become too sick to row the boat out of the harbour, captain Clearbeard decided to leave the harbour as quickly as possible.
Unfortunately the harbour isn't just a straight path to open sea. To protect the city from evil pirates, the entrance of the harbour is a kind of maze with drawbridges in it. Every bridge takes some time to open, so it could be faster to take a detour. Your task is to help captain Clearbeard and the fastest way out to open sea.
The pirates will row as fast as one minute per grid cell on the map. The ship can move only horizontally or vertically on the map. Making a 90 degree turn does not take any extra time.
 

Input
The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:
1. One line with three integers, h, w (3 <= h;w <= 500), and d (0 <= d <= 50), the height and width of the map and the delay for opening a bridge.
2.h lines with w characters: the description of the map. The map is described using the following characters:
—"S", the starting position of the ship.
—".", water.
—"#", land.
—"@", a drawbridge.
Each harbour is completely surrounded with land, with exception of the single entrance.
 

Output
For every test case in the input, the output should contain one integer on a single line: the travelling time of the fastest route to open sea. There is always a route to open sea. Note that the open sea is not shown on the map, so you need to move outside of the map to reach open sea.
 

Sample Input
26 5 7######S..##@#.##...##@####.###4 5 3######S#.##@..####@#
 

Sample Output
1611

同上一篇一样,使用优先队列,注意:最后结果要+1,因为open sea 在map外面,还有就是一个特殊情况要注意:

2 3 4

###

#S#

结果为1,而不是无解。。。

题目的意思是说,给你一个地图,上面‘.’代表水,‘#’表示陆地,‘S’表示船的起始位置,表示桥。我们知道船是不可以走陆地的,所以只有‘.’和可以走,但是走桥的时候有一个打开桥的时间d,过桥还需要1分钟,因为每个点可以走多次,所以要用到优先队列,每次都让步数最小的点出队列!

#include <iostream>#include <functional>#include <queue>#include <stdio.h>using namespace std;class Node{    public :        int x, y, time;        friend bool operator < (const Node &a, const Node &b)        {            return a.time > b.time;        }};priority_queue <Node> Q;Node first, next;char map[501][501];int n, m, a, b,d;int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};int bfs(){    first.x = a;    first.y = b;    first.time = 0;    Q.push(first);    int fx, fy;    while (!Q.empty())    {        first = Q.top();        Q.pop();        int i;        for (i = 0; i < 4; ++i)        {            fx = first.x + dir[i][0];            fy = first.y + dir[i][1];            if (fx >= 0 && fx < n && fy >= 0 && fy < m && map[fx][fy] != '#')            {                next.x = fx;                next.y = fy;                if (map[fx][fy] == '.')                {                     map[fx][fy] = '#';                     next.time = first.time + 1;                     Q.push(next);                }                else if (map[fx][fy] == '@')                {                    map[fx][fy] = '#';                    next.time = first.time + d+1;                    Q.push(next);                }                if(fx==(n-1)||fy==(m-1)||fx==0||fy==0)                return next.time;            }        }    }    return -1;}int main(){    int cnt,t;    scanf("%d",&t);    while (t--)    {        scanf("%d %d%d", &n, &m,&d);        int i, j;        for (i = 0; i < n; ++i)        {            getchar();            for (j = 0; j < m; ++j)            {                scanf("%c", &map[i][j]);                if (map[i][j] == 'S')                {                    map[i][j] = '#';                    a = i;                    b = j;                }            }        }        while (!Q.empty())        {            Q.pop();        }        cnt = bfs();        printf("%d\n", cnt+1);    }    return 0;}