杭电OJ 4198(广搜+优先队列~)图搜.Quick out of the Harbour

Quick out of the Harbour

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


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
   
   
   
   
2 6 5 7 ##### #S..# #@#.# #...# #@### #.### 4 5 3 ##### #S#.# #@..# ###@#
 

Sample Output
   
   
   
   
16 11
 



这里帮助一下只学了广搜 只了解了队列却不知道什么是优先队列的小伙伴们普及一下优先队列的知识~

转自搜狗百科:

优先队列(priorityqueue)普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出(largest-in,first-out)的行为特征。

还不会优先队列的小伙伴们可以来这个博客学习一下:

http://www.cppblog.com/shyli/archive/2007/04/06/21366.html

这里也贴上我的优先结构体队列的模板:

struct zuobiao
{
    int x,y,output;
    friend bool operator <(zuobiao a,zuobiao b)
    {
        return a.output>b.output;
    }
};



这里定义最小值优先当定义队列的时候就要写成这样

    priority_queue<zuobiao>s;

另外注意优先队列的对头不是front而是top


本题设计的理念涉及到两个解题关键点:1.最短路径找到出口.2.分别思考两种路径走法哪种更优.(过桥和走水上.)

普通用广搜找出口的题目大家做多了之后就有体会了~广搜是属于无脑找出口的一种方式(缺点)但是能那够活用.比如利用贪心思想(找到了第一个出口就直接return之类的用法)(优点)

虽然灵活但是比较无脑所以我们就要利用各种小技巧来补广搜的缺点(如以前博客中写的哈理工2074逃生).

还有这个题也是要用小技巧来解决缺点-----------------------------优先队列.

优先队列的用处就是来解决这个问题的关键所在.(用最少的步数解决问题)

这里贴上AC代码:并不是很难 理解起来还是比较容易的:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
char a[12121][12121];
int m,n,d;
int qidianx,qidiany;
int zongdianx,zongdiany;
using namespace std;
struct zuobiao
{
    int x,y,output;
    friend bool operator <(zuobiao a,zuobiao b)
    {
        return a.output>b.output;
    }
}now,nex;
int fx[4]={0,0,-1,1};
int fy[4]={1,-1,0,0};
void bfs(int x,int y)
{
    now.x=x;
    now.y=y;
    now.output=0;
    priority_queue<zuobiao>s;
    s.push(now);
    a[x][y]='#';
    while(!s.empty())
    {
        now=s.top();
        s.pop();
        if(now.x==zongdianx&&now.y==zongdiany)
        {
            printf("%d\n",now.output+1);
            return ;
        }
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            if(nex.x>=0&&nex.x<m&&nex.y>=0&&nex.y<n&&a[nex.x][nex.y]!='#')
            {
                if(a[nex.x][nex.y]=='.')
                nex.output=now.output+1;
                else
                nex.output=now.output+d+1;
                a[nex.x][nex.y]='#';
                s.push(nex);
            }
        }
    }
    return ;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&m,&n,&d);
        for(int i=0;i<m;i++)
        {
            scanf("%s",a[i]);
            for(int j=0;j<n;j++)
            {
                if(a[i][j]=='S')
                {
                    qidianx=i;
                    qidiany=j;
                }
                if(i==0||j==0||i==m-1||j==n-1)
                {
                    if(a[i][j]!='#')
                    {
                        zongdianx=i;
                        zongdiany=j;
                    }
                }
            }
        }
        bfs(qidianx,qidiany);
    }
}


你可能感兴趣的:(优先队列,杭电,bfs,4198)