poj3057 bfs+二分匹配

 

如题:http://poj.org/problem?id=3057

 

Evacuation
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1624 Accepted: 406

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

 

 

思路:先使用bfs记录每一个门到每一个人的最短距离。

然后枚举时间。

发现每一个人对应一个门同时对应一个时间,就是一条可行匹配。当匹配数==人的数量,所有人就出去了。

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define MAX 15

int X,Y;
char map[MAX][MAX];
int people_flect[MAX][MAX];
int d[MAX*4][MAX*MAX]; 
int cnt_door;
int cnt_people;
int dir[][2]={1,0,0,1,-1,0,0,-1};
int visit[MAX*4][MAX*MAX];
int match[MAX*4][MAX*MAX];
int d1[MAX][MAX];
int in(int x,int y)
{
 return x>=1&&y>=1&&x<=Y&&y<=X;
}

void bfs(int num,int x,int y)
{
 typedef pair<int,int>P;
 queue<P>q;
 memset(d1,-1,sizeof(d1));
 memset(d[num],-1,sizeof(d[num]));
 q.push(P(x,y));
 d1[x][y]=0;
  while(!q.empty())
  {
   int x1=q.front().first;
   int y1=q.front().second;
   q.pop();
   int i;
   for(i=0;i<4;i++)
   {
    int tx=x1+dir[i][0];
    int ty=y1+dir[i][1];
    if(in(tx,ty)&&d1[tx][ty]==-1&&map[tx][ty]=='.')
    {
     d1[tx][ty]=d1[x1][y1]+1;
     d[num][people_flect[tx][ty]]=d1[tx][ty];
     q.push(P(tx,ty));
    }
   }
  }
}

 

int dfs(int v,int t)
{
 int i,j;
 for(i=1;i<=cnt_door;i++)
  if(d[i][v]!=-1)
   for(j=t;j>=d[i][v];j--)
    if(!visit[i][j])
    {
     visit[i][j]=1;
     if(match[i][j]==-1||dfs(match[i][j],t))
     {
      match[i][j]=v;
      return 1;
     }
    }
 return 0;
}

void work()
{
 cnt_door=cnt_people=0;
  cin>>Y>>X;
  int i,j;
  memset(people_flect,0,sizeof(people_flect));
  for(i=1;i<=Y;i++)
     scanf("%s",map[i]+1);
  for(i=1;i<=Y;i++)
   for(j=1;j<=X;j++)
    if(map[i][j]=='.')
    {
     ++cnt_people;
     people_flect[i][j]=cnt_people;
    }
  for(i=1;i<=Y;i++)
   for(j=1;j<=X;j++)
    if(map[i][j]=='D')
    {
     ++cnt_door;
     bfs(cnt_door,i,j);
    }
  if(cnt_people==0)
  {
   printf("0\n");
   return;
  }
  int t=1;
  for(t=1;t<=X*Y;t++)
  {
   int res=0;
   memset(match,-1,sizeof(match));
   for(i=1;i<=cnt_people;i++)
   {
    memset(visit,0,sizeof(visit));
    if(dfs(i,t))
     res++;
   }
    if(res==cnt_people)
    {
    printf("%d\n",t);
    return;
    }
  }
  printf("impossible\n");
}
int main()
{
// freopen("C:\\1.txt","r",stdin);
 int T;
 cin>>T;
  while(T--)
 {
  work();
 }
 return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值