bfs+二分图(dbl)--poj3281

Language:
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<iostream>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<cmath>
#include<climits>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
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<int> dx,dy,px,py;
int dist[MAX_X][MAX_Y][MAX_X][MAX_Y];
vector<int> 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<G[v].size();i++)
    {
        int u=G[v][i],w=match[u];
        if(w<0||!used[w]&&dfs(w))
        {
            match[v]=u;
            match[u]=v;
            return true;
        }
    }
    return false;
}


void bfs(int x,int y,int d[MAX_X][MAX_Y])
{
    queue<int> 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<X&&y2>=0&&y2<Y&&field[x2][y2]=='.'&&d[x2][y2]<0)
            {
                d[x2][y2]=d[x][y]+1;
                qx.push(x2);
                qy.push(y2);
            }
        }
    }
}
void solve()
{
    int n=X*Y;
    dx.clear(),dy.clear(),px.clear(),py.clear();

    memset(dist,-1,sizeof(dist));
    for(int i=0; i<X; i++)
    {
        for(int j=0; j<Y; j++)
        {
            if(field[i][j]=='D')
            {
                dx.push_back(i);
                dy.push_back(j);
                bfs(i,j,dist[i][j]);
            }
            else if(field[i][j]=='.')
            {
                px.push_back(i);
                py.push_back(j);
            }
        }
    }
    int d=dx.size(),p=px.size();
    for(int i=0;i<n*d;i++)
    G[i].clear();
    for(int i=0;i<d;i++)
    {
        for(int j=0;j<p;j++)
        {
            if(dist[dx[i]][dy[i]][px[j]][py[j]]>=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<<endl;
        return;
    }
    int num=0;
    memset(match,-1,sizeof(match));
    for(int i=0;i<n*d;i++)
    {
        memset(used,0,sizeof(used));
        if(dfs(i))
        {
            if(++num==p)
            {
                cout<<i/d+1<<endl;
                return;
            }
        }
    }
    cout<<"impossible"<<endl;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>X>>Y;
        getchar();
        //memset(field,0,sizeof(field));
        for(int i=0; i<X; i++)
            for(int j=0; j<Y; j++)
                cin>>field[i][j];
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值