HDU 4198 quick out of the harbour (BFS + priority queue)

Source: Internet
Author: User
Quick Out Of The harbour

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 75 accepted submission (s): 47

Problem descriptioncaptain clearbeard decided to go to the harbor for a few days so his crew cocould 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 harbor, Captain clearbeard decided to leave the harbor as quickly as possible.
Unfortunately the harbor isn't just a straight path to open sea. to protect the city from edevil pirates, the entrance of the harbor is a kind of maze with drawbridges in it. every bridge takes some time to open, so it cocould 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.

 

Inputthe 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 harbor is completely surrounded with land, with exception of the single entrance.

 

Outputfor every test case in the input, the output shoshould contain one integer on a single line: the traveling 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 input2 6 5 7 ###### s .. ##@#. ##... ##@####. ### 4 5 3 ###### s #. ##@.. ####@#

 

Sample output16 11

 

Sourcebapc 2011

 

Recommendlcy is a simple question; it is BFS + priority queue... During the competition, Sy was converted into Sx, and wR was the end... Sad reminder, it took a long time to find out the problem...
 //  Search + priority queue,  
# Include <stdio. h>
# Include <queue>
# Include <iostream>
# Include < String . H>
Using Namespace STD;
Const Int Maxn =1010 ;
Struct Node
{
Int X, Y;
Int Time;
Friend Bool Operator <(Node A, Node B) // Low time has a higher priority
{
Return A. Time> B. time;
}
};
Priority_queue <node> que; // Priority queue
Char Map [maxn] [maxn]; // Record Map
Int Mark [maxn] [maxn];

Int H, W, D;
Int SX, Sy;
Int BFS ()
{
Int K;
Int Dir [ 4 ] [ 2 ] = {{ 0 , 1 },{ 0 ,- 1 },{ 1 , 0 },{- 1 , 0 }};
Node now, next;
While (! Que. Empty () que. Pop ();
Now. x = SX; now. Y = sy;
Now. Time = 1 ;

Que. Push (now );
While (! Que. Empty ())
{
Now = que. Top ();
Que. Pop ();

Mark [SX] [sy] = 1 ;
For (K = 0 ; K < 4 ; K ++)
{
Next. x = now. x + dir [k] [ 0 ];
Next. Y = now. Y + dir [k] [ 1 ];
If (Next. x < 0 | Next. x> = H | next. Y < 0 | Next. Y> = W) Return Now. time;


If (! Mark [next. x] [next. Y] & map [next. x] [next. Y]! = ' # ' )
{
If (Map [next. x] [next. Y] =' @ ' )
{
Next. Time = now. Time + D + 1 ;
}
Else
Next. Time = now. Time + 1 ;
Que. Push (next );
Mark [next. x] [next. Y] = 1 ;
}
}
}
Return - 1 ;
}
Int Main ()
{
Int T;
Scanf ( " % D " , & T );
While (T --)
{
Scanf ( " % D " , & H, & W, & D );
// Memset (mark, 0, sizeof (Mark ));
For ( Int I = 0 ; I {
For ( Int J = 0 ; J <W; j ++)
{
Scanf ( " % C " , & Map [I] [J]);

Mark [I] [J] = 0 ;
If (Map [I] [J] = ' S ' ) {SX = I; Sy = J ;}

}
}


Printf ( " % D \ n " , BFs ());



}
Return 0 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.