hdu4198-优先队列+bfs

来源:互联网 发布:hello树先生知乎 编辑:程序博客网 时间:2024/04/29 01:24

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
 

题意;这道题的题意是叫你用最短的时间走出迷宫走向大海;S表示起点,走到@时间加上d+1;

走到 "."时间加1,’#‘表示墙,

注意的是最后结果还要+1,因为你只是走出了迷宫还没有走出大海;


思路:就是一道比较简单的bfs,由于出口可能有很多,所以要用优先队列来做;

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
#define nn 609
struct node
{
    int x,y;
    int step;
    friend bool operator < (node a,node b)
    {
        return a.step>b.step;
    }
};
int  sx,sy;
int h,w,d;


int use[nn][nn];


char ma[nn][nn];
int dir[4][2]={{1,0},
               {-1,0},
               {0,1},
               {0,-1}
              };


priority_queue<node> que;
void bfs()
{
    int i,j;
    memset(use,0,sizeof(use));
    node st,en;
    int tx,ty;
    int ff;
    st.x=sx;
    st.y=sy;
    st.step=0;
    while(!que.empty())
        que.pop();


   use[sx][sy]=1;
    que.push(st);
    while(!que.empty())
    {
        en=que.top();
        que.pop();
        if(en.x==0||en.x==h-1||en.y==0||en.y==w-1)
        {
               printf("%d\n",en.step+1);
               return;
        }
        for(i=0;i<4;i++)
        {
            tx=en.x+dir[i][1];
            ty=en.y+dir[i][0];
            if(tx>=0 && tx<h &&  ty>=0&& ty<w && ma[tx][ty]!='#'&&use[tx][ty]==0)
            {
                use[tx][ty]=1;
                if(ma[tx][ty]=='.')
                 st.step=en.step+1;
                else if(ma[tx][ty]=='@')
                    st.step=en.step+d+1;  //卧槽,我把这里的st写成en了。然后我错了一个下午,55555555
                    st.x=tx;
                    st.y=ty;
                 que.push(st);
            }
        }
    }


}
int main()
{
    int i,j;
    int t;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d %d %d",&h,&w,&d);
            for(i=0;i<h;i++)
            {
                for(j=0;j<w;j++)
                {
                    cin>>ma[i][j];
                    if(ma[i][j]=='S')
                    {
                        sx=i;
                        sy=j;
                       ma[i][j]='.';
                   }
                }
            }
            bfs();
        }
    }
}

0 0