Description
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
Problem Analysis:
We can use breath first search to compute the time of person arrive the door. If we find some person cannot reach any door, then we get the answer “impossible”. Or after some time, all persons escaped.
In the latter case, we can build the bipartite graph, let the persons as a vertex set, and the pair (door, time) as the other vertex set, here pair means a person escaped from the ith door at jth time. So it is a maximum point coverage problem, solve it we will get the answer
#include <cstdio>
#include <vector>
#include <queue>using namespace std;#define ROW 13
#define COL 13
#define MAXITEMS ((ROW) * (COL))int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int distance2door[ROW][COL][ROW][COL];
vector<pair<int, int> > doors;
vector<pair<int, int> > persons;
char fields[ROW][COL];
vector<int> G[MAXITEMS * MAXITEMS];
int match[MAXITEMS * MAXITEMS];
bool used[MAXITEMS * MAXITEMS];
int V;void add_adge(int u, int v) {
G[u].push_back(v);
G[v].push_back(u);
}bool dfs(int v) {
used[v] = true;
for (int i = 0; i < G[v].size(); ++i) {
int u = G[v][i];
int w = match[u];
if (w < 0 || !used[w] && dfs(w)) {
match[v] = u;
match[u] = v; return true;
}
} return false;
}void computeDistance(int x, int y, int d[ROW][COL]) {
queue<pair<int, int> > q;
d[x][y] = 0;
q.push(make_pair(x, y));while (!q.empty()) {
int i = q.front().first;
int j = q.front().second;
q.pop();for (int k = 0; k < 4; ++k) {
int px = i + dx[k];
int py = j + dy[k];
if (px >= 0 && px < ROW && py >= 0 && py < COL && d[px][py] == -1 && fields[px][py] == '.') {
d[px][py] = d[i][j] + 1;
q.push(make_pair(px, py));
}
}
}
}int main(int argc, char *argv[]) {
int times;
scanf("%d", ×); while (times--) {
int rows, cols;
doors.clear();
persons.clear();
memset(distance2door, -1, sizeof(distance2door));
getchar();
scanf("%d%d", &rows, &cols); for (int i = 0; i < rows; ++i) {
getchar();
for (int j = 0; j < cols; ++j) {
char c;
scanf("%c", &fields[i][j]);
if (fields[i][j] == 'D') {
doors.push_back(make_pair(i,j));
} else if (fields[i][j] == '.') {
persons.push_back(make_pair(i,j));
}
}
}for (int i = 0; i < doors.size(); ++i)
computeDistance(doors[i].first, doors[i].second, distance2door[doors[i].first][doors[i].second]);int d = doors.size();
int p = persons.size();
for (int v = 0; v < rows * cols * doors.size(); ++v) G[v].clear();
for (int i = 0; i < d; ++i) {
for (int j = 0; j < p; ++j) {
if (distance2door[doors[i].first][doors[i].second][persons[j].first][persons[j].second] >= 0) {
for (int k = distance2door[doors[i].first][doors[i].second][persons[j].first][persons[j].second]; k <= rows * cols; ++k)
add_adge((k-1) * d + i, rows * cols * d + j);
}
}
} if (persons.size() == 0) {
printf("0\n");
} else {
int num = 0;
memset(match, -1, sizeof(match));
for (int v = 0; v < rows * cols * doors.size(); ++v) {
memset(used, 0, sizeof(used));
if (dfs(v)) {
if (++num == persons.size()) {
printf("%d\n", v / (int)doors.size() + 1);
break;
}
}
} if (num != persons.size()) printf("impossible\n");
}
} return 0;
}
time complexity is O(rows * columns * doors)
space complexity is O(rows * rows * columns * columns)