1797. Evacuation

单点时限: 2.0 sec

内存限制: 256 MB

Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time, it may take a while for everyone to escape.

You are given the floorplan of a room and must find out how much time it will take for everyone to get out. Rooms consist of obstacles and walls, which are represented on the map by an X, empty squares, represented by a . and exit doors, which are represented by a D. The boundary of the room consists only of doors and walls, and there are no doors inside the room. The interior of the room contains at least one empty square.

Initially, there is one person on every empty square in the room and these persons should move to a door to exit. They can move one square per second to the North, South, East or West. While evacuating, multiple persons can be on a single square. The doors are narrow, however, and only one person can leave through a door per second.

What is the minimal time necessary to evacuate everybody? A person is evacuated at the moment he or she enters a door square.

输入格式

The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with two integers $Y$ and $X$, separated by a single space, satisfying $3 \le Y, X \le 12$: the size of the room
  • $Y$ lines with $X$ characters, each character being either X, ., or D: a valid description of a room

输出格式

For every test case in the input, the output should contain a single line with the minimal evacuation time in seconds, if evacuation is possible, or impossible, if it is not.

样例

Input
3
5 5
XXDXX
X...X
D...X
X...D
XXXXX
5 12
XXXXXXXXXXXX
X..........D
X.XXXXXXXXXX
X..........X
XXXXXXXXXXXX
5 5
XDXXX
X.X.D
XX.XX
D.X.X
XXXDX
Output
3
21
impossible

3 人解决,7 人已尝试。

3 份提交通过,共有 26 份提交。

8.9 EMB 奖励。

创建: 16 年,8 月前.

修改: 6 年,9 月前.

最后提交: 9 年,4 月前.

来源: The 2006 Benelux Algorithm Programming Contest

题目标签