如题:http://poj.org/problem?id=3057
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 1624 | Accepted: 406 |
Description
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
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
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;
}
本文介绍了一种基于广度优先搜索(BFS)的疏散模拟算法,该算法用于计算房间内人群完全疏散所需的最短时间。通过预先计算每个出口到每个位置的距离,并采用匹配算法来寻找最优疏散方案。
708

被折叠的 条评论
为什么被折叠?



