bfs+二分图(dbl)--poj3281

Language: Default
Evacuation
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1076   Accepted: 274

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

题意:X表示墙,.表示空地,D表示门,刚开始空地上占满了人,问所有的人从问出去需要的最短时间,门一秒钟只能走一个人。
思路:每个时间和门的二元组,都能确定一个能逃脱的人的集合,通过最大匹配是可以判断是否都可以逃脱。
下面是代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long LL;
using namespace std;
const int MAX_X=15,MAX_Y=15;
int X,Y;
int dX[]= {-1,0,0,1};
int dY[]= {0,1,-1,0};
char field[MAX_X][MAX_Y+1];
vector dx,dy,px,py;
int dist[MAX_X][MAX_Y][MAX_X][MAX_Y];
vector G[10000];
int match[10000];
bool used[10000];

void add_edge(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 qx,qy;
    d[x][y]=0;
    qx.push(x);
    qy.push(y);
    while(!qx.empty())
    {
        x=qx.front();
        y=qy.front();
        qx.pop();
        qy.pop();
        for(int k=0; k<4; k++)
        {
            int x2=x+dX[k],y2=y+dY[k];
            if(x2>=0&&x2=0&&y2=0)
                for(int k=dist[dx[i]][dy[i]][px[j]][py[j]];k<=n;k++)
                add_edge((k-1)*d+i,n*d+j);
        }
    }
    if(p==0)
    {
        cout<<0<>t;
    while(t--)
    {
        cin>>X>>Y;
        getchar();
        //memset(field,0,sizeof(field));
        for(int i=0; i>field[i][j];
        solve();
    }
    return 0;
}


你可能感兴趣的:(二分图,BFS)