HDU 4198 Quick out of the Harbour(BFS+优先队列)

来源:互联网 发布:工作站知乎 编辑:程序博客网 时间:2024/05/19 05:05

Quick out of the Harbour

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
这里写图片描述

Sample Output
16
11

大致题意
一艘船在一片海域中,‘#’表示障碍,‘.’表示水路可以通过,‘@’表示桥,船需要等待一定的时间才能通过此处。问你最少需要多少时间才能走出这片海域。保证一定有一条出路。

思路
bfs+优先队列(用时少,优先度高)

代码如下

#include<iostream>#include<algorithm>#include<string>#include<cstdio>#include<cstring>#include<queue>#include<vector>using namespace std;int dix[4]={0,0,1,-1};int diy[4]={1,-1,0,0};char map[505][505];int vis[505][505];struct node{    int x;    int y;    int time;    friend bool operator<(node a,node b)    {        return a.time>b.time; //用时少,优先度高    }};int n,m,q; priority_queue<node>que;//优先队列void bfs(){    while(!que.empty())    {        node u=que.top();        que.pop();        if(u.x==1||u.x==n||u.y==1||u.y==m)//如果到达边界,则还需一个单位时间走出        {            cout<<u.time+1<<endl;            return;        }        for(int i=0;i<4;i++)        {            int x=u.x+dix[i];            int y=u.y+diy[i];            if(map[x][y]!='#'&&vis[x][y]==0&&x>=1&&x<=n&&y>=1&&y<=m)            {                if(map[x][y]=='.')//如果是水路                {                    node v;                    v.x=x;                    v.y=y;                    v.time=u.time+1;                    vis[x][y]=1;                    que.push(v);                }                else if(map[x][y]=='@')//如果是桥                {                    node v;                    v.x=x;                    v.y=y;                    v.time=u.time+1+q;                    vis[x][y]=1;                    que.push(v);                }            }        }     } }int main(void){    int T;    cin>>T;    while(T--)    {        while(!que.empty())        que.pop();        memset(vis,0,sizeof(vis));        cin>>n>>m>>q;        getchar();        for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)        {            cin>>map[i][j];            if(map[i][j]=='S')            {                node u;                u.x=i;                u.y=j;                u.time=0;                que.push(u);            }        }        bfs();    }    return 0;}
0 0
原创粉丝点击