Evacuation POJ - 3057 二分图匹配+bfs

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

题意:D为门,每一秒钟仅能通过一人,.为空地 每个空地有一个人,求所有女人都出去的最短时间,出不去输出impossible。


思路:对于每个门,每一个个时间都可能通过 或者不通过,有D*T中情况 ,而人需要在D*T种情况找到符合的进行连线(二分图匹配)。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int M = 3500;
bool vis[M];
int match[M];
int G[M][M];
int X,Y;
int t;
int dis[20][20][20][20];
char ma[20][20];
int di[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
vector<int> dx;
vector<int> dy;
vector<int> px;
vector<int> py;
int d,p;
bool check(int x,int y)
{
    if(x<0||x>=X||y<0||y>=Y)
        return 0;
    else
        return 1;
}
bool found(int u)
{
    for(int i=0;i<p;i++)
    {
        if(!vis[i]&&G[u][i])
        {
            vis[i]=1;
            if(match[i]==-1||found(match[i]))
            {
                match[i]=u;
                return true;
            }
        }
    }
    return false;
}
void bfs(int x,int y,int d[20][20])
{
    queue<int>qx;
    queue<int>qy;
    qx.push(x);
    qy.push(y);
    d[x][y]=0;
    while(qx.size())
    {
        x=qx.front();qx.pop();
        y=qy.front();qy.pop();
        for(int i=0;i<4;i++)
        {
            int nx=x+di[i][0],ny=y+di[i][1];
            if(check(nx,ny)&&ma[nx][ny]=='.'&&d[nx][ny]==-1)
            {
                d[nx][ny]=d[x][y]+1;
                qx.push(nx);
                qy.push(ny);
            }
        }
    }
}
int solve()
{
    memset(G,0,sizeof(G));
    memset(match,-1,sizeof(match));
    d=dx.size();p=px.size();
    int res=0;
    for(int T=0;T<X*Y;T++)
    {
        for(int i=0;i<d;i++)
        {
            for(int j=0;j<p;j++)
            {
                int x=dx[i],y=dy[i];
                int mx=px[j],my=py[j];
                int k=dis[x][y][mx][my];
                if(k!=-1&&k<=T+1&& !G[i][j])
                    G[i][j]=1;
            }
        }
        for(int i=0;i<d;i++)
        {
            memset(vis,0,sizeof(vis));
            if(found(i))
                res++;
        }
        if(res==px.size())
            return T+1;
    }
    return -1;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>X>>Y;
        dx.clear();dy.clear();
        px.clear();py.clear();
        for(int i=0;i<X;i++)
            cin>>ma[i];
        memset(dis,-1,sizeof(dis));
        for(int i=0;i<X;i++)
        {
            for(int j=0;j<Y;j++)
            {
                if(ma[i][j]=='D')
                {
                    dx.push_back(i);
                    dy.push_back(j);
                    bfs(i,j,dis[i][j]);
                }
                else
                    if(ma[i][j]=='.')
                {
                    px.push_back(i);
                    py.push_back(j);
                }
            }
        }
        int ans=solve();
        if(ans==-1)
            printf("impossible\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/liluoyu_1016/article/details/80141493