UVA11624 fire! BFS

Source: Internet
Author: User

11624 fire!
Joe works in a maze. Unfortunately, portions of the maze has caught
On fire, and the owner of the maze neglected to create a fire escape plan.
Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze is
On fire, you must determine whether Joe can exit the maze before the
Fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or
Horizontally (not diagonally). The fire spreads all four directions from
Each of the square is on the fire. Joe may exit the maze from any square
That borders the edge of the maze. Neither Joe nor the fire may enter
A square that's occupied by a wall.
Input
The first line of input contains a single integer, the number of test cases
To follow. The first line of all test case contains the integers R
And C, separated by spaces, with 1≤r, c≤1000. The following R lines of the test case each contain
One row of the maze. Each of the these lines contains exactly C characters, and each of the these characters is
One of:
#, a wall
•., a passable square
J., Joe's initial position in the maze, which are a passable square
F, a square that's on fire
There'll is exactly one J in each test case.
Output
For each test case, output a single line containing ' impossible ' if Joe cannot exit the maze before the
Fire reaches him, or a integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2
4 4
####
#JF #
#.. #
#.. #
3 3
###
#J.
#. Funiversidad de Valladolid oj:11624–fire! 2/2
Sample Output
3
Impossible

Test instructions

Joe worked on a farm, and the farm was a n*m matrix, and one day, the farm suddenly caught fire.

Fire will spread to 4 directions per minute, fortunately wall can not burn up (but people can not pass), Joe every minute walk a unit of lattice.

It's safe to walk out of this farm.

Ask Joe to get out of the farm? If you can, the minimum time required.

The farm has some grass, walls, a burning position, and Joe.

My thoughts are:

-2 stands for wall,

-1 represents the initial grass

0 represents the location of the ignition

-3 stands for Joe's position

The step in the struct point represents the time that passes through the dot (x, y).

Then 0 of the location of the BFS, if the maze[i][j]==-1 will be followed by burning, at this time update the value of Maze[i][j], record its burning time.

And then the BFS for Joe's position (I wrote 2 BFS), if Joe passes less than the maze value, he can pass.

Then submit, WA up.

Thinking about it, the position of F (i.e. 0) may be more than one.

Modified, Maze[i][j]=min (Maze[i][j],du.step)

It's tle.

Another thought, if DU.STEP>=MAZE[I][J] (maze[i][j]!=-1), that the lattice time than the previous slow, then do not continue BFS, direct return;

After the change of 575ms.

Click Expand Code

UVA11624 fire! BFS

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.