POJ3057 Evacuation of Problem solving report

Source: Internet
Author: User

POJ3057 Evacuation of problem solving report

Description

Fires can disastrous, especially when a fire breaks out in a, the is completely filled with people. Rooms usually has a couple of exits and emergency exits, but with everyone rushing off at the same time, it could take a wh Ile for everyone to escape.

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

Initially, there is one person on every empty square in the the class 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 is on a single square. The doors is narrow, however, and only one person can leave through a door per second.

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

Input

The first line of the input contains a, number:the number of test cases to follow. Each test case has the following format:
One line with both integers Y and x, separated by a single space, satisfying 3 <= y, X <= 12:the size of the
Y lines with X characters, each character being either ' X ', '. ', or ' D ': A valid description of a

Output

For every test case in the input, the output should contain a, a, and the minimal evacuation time in seconds, if Evacuation is possible, or "impossible", if it's not.

Sample Input

5XXDXXX ... Xd... Xx... DXXXXX5 12XXXXXXXXXXXXX ..... Dx. Xxxxxxxxxxx ..... XXXXXXXXXXXXX5 5XDXXXX. X.dxx. XXD. X.xxxxdx

Sample Output

321impossible


The main topic: there is a n*m lattice room, some lattice is pillar X, you can not stand people, and. This is the open space, can stand infinitely many people. There are some doors on the surrounding lattice d. Now this House is on fire, every vacant lot. There is a person, and then they can walk up and down one or the other in the left or right. A door can only pass one person in a second. Ask all people to run at least how long it takes, and if you can't escape, output impossible.


Analysis: First, you need to understand the time dynamic flow model. If you do not understand, please refer to the classic example:

SGU438 the glorious Karlutka River

http://blog.csdn.net/maxichu/article/details/45219567


I looked at the "challenge" on the solution of this problem, is a binary graph matching algorithm, not very clear. So the dynamic flow model is still used. First let the super source point connect each grid, the load is 1, indicating that there is a person. Note that each lattice needs to be split, the load is INF, because the lattice can stand infinitely many people (actually do not need to split, in order to match the door I lazy score and then all dismantled ...) Anyway, ample space). Then we traverse the time, starting from 1 until 150 (because the worst case will not exceed 144s,150 is written handy). We are concerned now to proceed to the moment T, at the moment t of the level residual flow graph. For each open space (i, J) T, first the state of the T moment is split. And then the space around the four lattice on the state of a second must be able to switch over, then even the edge.


int NX = i + mox[k], NY = j + Moy[k];
if (NX >= 1 && NX <= n&&ny >= 1 && ny <= M& ; &map[nx][ny]! = ' X ')
Addedge ((t-1) * + (NX-1) * + NY +, T * 3 xx + (i-1) * + j, INF);


Likewise for the moment T's Gate (I, j) T, first connect it with the super sink point des. Then the state of the four squares around the gate can also be converted, so even the edge.


int NX = i + mox[k], NY = j + Moy[k];
if (NX >= 1 && NX <= n&&ny >= 1 && ny <= m&&map[nx][ny]! = ' X ')
Addedge ((t-1) * + (NX-1) * + NY +, T * + (I-1) * + + J, INF);


In fact, the door and open space status update is the same, the main difference is that the demolition of the point. (In fact, I wrote when I found it seems like not to break the line, but also do not want to change, embarrassed). This second after the end of the run to the maximum flow, to see if the sum of the maximum flow has exceeded the number of PEO, over the exit. Finally, the answer can be based on T output.


On the code:

#include <iostream> #include <cstring> #include <algorithm> #include <queue> #include < cstdio>using namespace Std;const int maxn = 65010;const int MAXM = 990000;const int INF = 0x3f3f3f3f;struct Edge{int fr OM, to, cap, next;}; int mox[5] = {0,-1, 1, 0, 0};int moy[5] = {0, 0, 0,-1, 1}; Edge edge[maxm];int level[maxn];char map[15][15];int head[maxn];int src, des, cnt;void addedge (int from,int to,int cap) {E Dge[cnt].from = from;edge[cnt].to = To;edge[cnt].cap = Cap;edge[cnt].next = Head[from];head[from] = Cnt++;swap (from, to) ; edge[cnt].from = from;edge[cnt].to = To;edge[cnt].cap = 0;edge[cnt].next = Head[from];head[from] = cnt++;} int BFs () {memset (level,-1, sizeof level);queue<int> Q;while (!q.empty ()) Q.pop (); LEVEL[SRC] = 0;q.push (src); whil E (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; I! = 1; i = edge[i].next) {int v = edge[i].to;if (edge[i].cap& AMP;&AMP;LEVEL[V] = =-1) {Level[v] = Level[u] + 1;q.push (v);}}} return Level[des]! =-1;} int Dfs(int u, int f) {if (U = = des) return f;int tem;for (int i = head[u]; I! = 1; i = edge[i].next) {int v = edge[i].to;if (Edge[i].cap > 0 &am  p;& Level[v] = Level[u] + 1) {tem = DFS (V, min (f, edge[i].cap)), if (Tem > 0) {edge[i].cap-= Tem;edge[i^1].cap + = Tem;return tem;}}} Level[u] = -1;return 0;} int dinic () {int ans = 0, Tem;while (BFS ()) {while (tem = DFS (src, INF) > 0)) {ans + = tem;}} return ans;} int main () {int kase;cin >> kase;int N, m,peo;src = 0; des = 65005;while (kase--) {memset (head,-1, sizeof head); CNT = 0;peo = 0;cin >> n >> m;for (int i = 1; I <= n; i++) {for (int j = 1; j <= M; j + +) {cin >> map[i][j] ;}} for (int i = 1, i <= N; i++) {for (int j = 1; j <= M; j + +) {if (map[i][j] = = '. ') {Peo++;addedge (src, (i-1) * + j, 1), Addedge ((i-1) * + j, (i-1) * + j + N, INF);}} int ans = 0;int t;for (t = 1; t <=; t++)//Enumeration time multiple augmentation {for (int i = 1; I <= n; i++) {for (int j = 1; j <= M; j + +) { if (map[i][j] = = '. ') {Addedge (T * 300 +(i-1) * + J, T * + (I-1) * + + j +, INF); for (int k = 0; k <= 4; k++) {int NX = i + mox[k], NY = j + Moy [K];if (NX >= 1 && NX <= n&&ny >= 1 && ny <= m&&map[nx][ny]! = ' X ') Addedge ((t -1) * + (NX-1) * + NY + MAX, T * + (I-1) * + j, INF);}} if (map[i][j] = = ' D ') {Addedge (T * + (I-1) * + J, DES, 1); for (int k = 0; k <= 4; k++) {int NX = i + mox[k], NY = j + moy[k];if (NX >= 1 && NX <= n&&ny >= 1 && ny <= m&&map[nx][ny]! = ' X ') add Edge ((t-1) * + (NX-1) * + NY + MAX, T * + (I-1) * + j, INF);}}} Ans + = dinic (); if (ans >= peo) break;} if (T > cout) << "Impossible" << endl;elsecout << t << Endl;} return 0;}

About the big advance over 51 to Luguhu, I have called the police, BJ4 ... It's 41 days a week!


POJ3057 Evacuation of Problem solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.