POJ 3057 Evacuation(BFS+二分图匹配)

原题链接

Problem 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’代表门。现在要让所有人都移动到门,每秒移动一格,每扇门每秒只能通过一个人,问让所有人到达门花的最短时间。

解题思路

从每个人的位置出发,搜索出到每扇门的距离,pp.size()为人的数量,dd.size()为门的数量,这样二分图的左部端点的数量固定为pp.size(),右部每秒增加dd.size()个端点,并根据每个人是否到达了这扇门来增加边,用匈牙利算法算出匹配数,直到匹配数到达pp.size()。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define PI 3.1415927
#define inf 0x3f3f3f3f
#define mp make_pair
#define fi first
#define se second
#define maxn 5000
using namespace std;
int dir[4][2]={-1,0,1,0,0,1,0,-1};
struct door
{
    int x,y;
    door (int xx,int yy)
    {
        x=xx;
        y=yy;
    }
};
struct pe
{
    int x,y;
    int step;
    vector<pair<int,int> > ddis;
    pe (int xx,int yy,int sstep)
    {
        x=xx;
        y=yy;
        step=sstep;
    }
};
int uN,vN;  
int g[150][5000];
int linker[maxn];
bool used[maxn];
bool dfs(int u)
{
    int v;
    for(v=0;v<vN;v++)
        if(g[u][v]&&!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    return false;
}
int main(void)
{
    int t,x,y;
    cin>>t;
    while(t--)
    {
        int flag=1;
        char mat[20][20];
        int vis[20][20];
        cl(mat);
        vector<door> dd;
        vector<pe> pp;
        scanf("%d%d",&x,&y);
        rep(i,0,x)
        {
            getchar();
            rep(j,0,y)
            {
                scanf("%c",&mat[i][j]);
                if(mat[i][j]=='.')
                {
                    pp.pb(pe(i,j,0));
                }
                else if(mat[i][j]=='D')
                {
                    dd.pb(door(i,j));
                }
            }
        }


        for(int i=0;i<pp.size();++i)
        {
            cl(vis);
            queue<pe> qq;
            qq.push(pe(pp[i].x,pp[i].y,pp[i].step));
            vis[pp[i].x][pp[i].y]=1;
            while(!qq.empty())
            {
                pe temp=qq.front();qq.pop();
                if(mat[temp.x][temp.y]=='D')
                {
                    for(int j=0;j<dd.size();++j)
                    {
                        if(dd[j].x==temp.x&&dd[j].y==temp.y)
                        {
                            pp[i].ddis.pb(mp(j,temp.step));
                            break;
                        }
                    }
                }
                else if(mat[temp.x][temp.y]!='X')
                {
                    for(int j=0;j<4;++j)
                    {
                        int px=temp.x+dir[j][0];
                        int py=temp.y+dir[j][1];
                        if(mat[px][py]!='X'&&!vis[px][py])
                        {
                            vis[px][py]=1;
                            qq.push(pe(px,py,temp.step+1));
                        }
                    }
                }
            }
            if(pp[i].ddis.empty())
            {
                flag=0;
                break;
            }
        }
        /*
        for(int i=0;i<pp.size();++i)
        {
            for(int j=0;j<pp[i].ddis.size();++j)
            {
                printf("%d %d\n",pp[i].ddis[j].fi,pp[i].ddis[j].se);
            }
            cout<<endl;
        }
          */


        if(!flag) printf("impossible\n");
        else
        {
            cl(g);
            int res=0;
            int u;
            uN=pp.size();
            vN=0;
            int ttime=0;
            while(res<pp.size())
            {
                res=0;
                memset(linker,-1,sizeof(linker));
                for(u=0;u<uN;u++)
                {
                    memset(used,0,sizeof(used));
                    if(dfs(u))  res++;
                }
                ttime++;
                vN+=dd.size();
                for(int i=0;i<pp.size();++i)
                {
                    for(int j=0;j<pp[i].ddis.size();++j)
                    {
                        if(ttime>=pp[i].ddis[j].se)
                        {
                            g[i][(ttime-1)*dd.size()+pp[i].ddis[j].fi]=1;

                        }
                    }
                }
            }
            printf("%d\n",ttime-1);


        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值