码迷,mamicode.com
首页 > 其他好文 > 详细

Evacuation (二分图)

时间:2020-05-16 12:15:25      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:single   follow   char   tput   min   with   cape   roo   lang   

Evacuation (二分图)

Describe

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.

Input

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 <= Y, X <= 12: the size of the room
Y lines with X characters, each character being either ‘X‘, ‘.‘, or ‘D‘: a valid description of a room

Output

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.

Sample 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

Sample Output

3
21
impossible

Summary

有一个X*Y的房间,‘X’代表墙壁,‘D’是门,‘.’代表人。这个房间着火了,人要跑出去,但是每一个时间点只有一个人可以从门出去。
问最后一个人逃出去的最短时间,如果不能逃出去,输出impossible。

Solution

以前有一道小杉的题,也是逃跑,但是那个题的门被使用一次就消失了,这个可以多次使用,不过每个时刻只能有一人使用,其实本质上差不多。

首先我们要 Bfs 一遍,以每个门为起点,找人到门的最短距离,如果有人无法到达所有门,那么直接输出"impossible"。

然后我们不断增加时间,每个时间我们增加门的数量,增加的数量就是门的总数,然后在这个时间内可以逃脱的人向门连一条边,然后我们进行二分图匹配。如果得出的结果跟人数一样,那么这个时间就是最小时间,否则,我们在重复之前的操作。

直到最大匹配得出的结果和人数一样为止。

Evacuation (二分图)

标签:single   follow   char   tput   min   with   cape   roo   lang   

原文地址:https://www.cnblogs.com/Lour688/p/12899602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!