uva11624

maksyuki 发表于 oj 分类,标签:
1

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

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 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 these lines contains exactly

C 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.

 

Output

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.

 

Sample Input

2

4 4

####

#JF#

#..#

#..#

3 3

###

#J.

#.F

 

Sample Output

3

IMPOSSIBLE

 

题目类型:BFS

算法分析:同时扩展Joe可以走的位置和着火的区域(这里分别是将两个扩展的时间同步来实现的),注意图中可以存在多个Fire的地方!!!

 

fzu2150

maksyuki 发表于 oj 分类,标签:
0

Problem 2150 Fire Game

Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)

You can assume that the grass in the board would never burn out and the empty grid would never get fire.

Note that the two grids they choose can be the same.

 Input

The first line of the date is an integer T, which is the number of the text cases.

Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.

1 <= T <=100, 1 <= n <=10, 1 <= m <=10

 Output

For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.

 Sample Input

4

3 3

.#.

###

.#.

3 3

.#.

#.#

.#.

3 3

...

#.#

...

3 3

###

..#

#.#

 Sample Output

Case 1: 1

Case 2: -1

Case 3: 0

Case 4: 2

 

题目类型:BFS(两个点)

算法分析:由于问题的规模比较小,所以可以枚举任意两个种草的地方并计算最小的相遇时间。如果枚举完发现还有种草的地方没有被访问到,则直接输出-1。如果发现在一次枚举过程中出现没有被访问到的节点,则不更新此时的最小值(此时包含了可能存在两个联通块的情况!!!)