[kuangbin带你飞]专题一 简单搜索J - Fire!(UVA 11624)

来源:互联网 发布:win10跟windows server 编辑:程序博客网 时间:2024/05/16 14:16

J - Fire

Joe works in a maze. Unfortunately, portions of the maze have 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 are 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 square that is on 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 is occupied by a wall.

Input Specification

The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integersR and C, separated by spaces, with 1 <= R,C <= 1000. The followingR lines of the test case each contain one row of the maze. Each of these lines contains exactlyC characters, and each of these characters is one of:
  • #, a wall
  • ., a passable square
  • J, Joe's initial position in the maze, which is a passable square
  • F, a square that is on fire
There will be exactly one J in each test case.

Sample Input

24 4#####JF##..##..#3 3####J.#.F

Output Specification

For each test case, output a single line containing IMPOSSIBLE if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Output for Sample Input

3IMPOSSIBLE


题意:J表示人在的位置,F表示火的位置(这是个坑没有说只有一个火源),求人安全逃出的时间,'.'表示草(可燃) ,‘#’表示墙

要仔细看懂题,不要想当然的以为是这样。


方法一:

两个bfs,一个对人,求出人到达各个点所需的时间,

一个对火,求出火烧到各个点所需的时间,

'.'位置的初始化距离为10000,否者为0

在然后求出四个边界的人到的时间是否有小于火的。如果有求出其中最小的,加上一就是结果。


这里面的情况很多要全部考虑到,交一发WA时,改了之后不要马上交看看还有其它错误没有(一般情况下很有肯能是错的)

详见代码


方法二:

在一个图上,只用一个bfs。

先让火传播然后是人。

<div></div>


0 0