码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 3057 Evacuation

时间:2018-08-14 19:53:02      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:tmp   line   each   ever   nta   spec   obs   eva   which   

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

//注意D和D之间不能扩展啊
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#define ll long long
using namespace std;
ll read() {
    ll x=0,f=1;
    char ch=getchar();
    while(!(ch>=0&&ch<=9)) {
        if(ch==-)f=-1;
        ch=getchar();
    };
    while(ch>=0&&ch<=9) {
        x=x*10+(ch-0);
        ch=getchar();
    };
    return x*f;
}
const int MAXN = 550*20*4;//??????
const int MAXM = 550*550*20*4;//??????
struct Edge {
    int to,next;
} edge[MAXM];
int head[MAXN],tot;
void init() {
    tot = 0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v) {
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}
int linker[MAXN];
bool used[MAXN];
int uN;
struct dat{
    int tm;
    int x;
    int y;
    int id;
}tmp,nxt;
int nn,mm,lm;
char mp[20][20];
queue<dat> q;
int cntd,xd[MAXN],yd[MAXN];
bool dfs(int u) {
    for(int i = head[u]; i != -1 ; i = edge[i].next) {
        int v = edge[i].to;
        if(!used[v]) {
            used[v] = true;
            if(linker[v] == -1 || dfs(linker[v])) {
                linker[v] = u;
                return true;
            }
        }
    }
    return false;
}
int hungary() {
    int res = 0;
    memset(linker,-1,sizeof(linker));
    for(int u = 0; u <= lm*cntd; u++) {
        memset(used,false,sizeof(used));
        if(dfs(u))res++;
    }
    return res;
}
bool cmp(int a,int b){
    return a > b;
}

bool viss[100][20][20];
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
int tj;
bool judge(int y,int x){
    if(y < 1 || y > nn || x < 1 || x > mm || mp[y][x] == X || mp[y][x] == D) return false;
    return true;
}
void bfs(){
    tj = 0;
    cntd = 0;
    memset(viss,false,sizeof(viss));
    lm = nn*mm;
    int hh;
    while(!q.empty()) q.pop();
    for(int i = 1;i <= nn;i++){
        for(int j = 1;j <= mm;j++){
            if(mp[i][j] == D){
                cntd++;
                xd[cntd] = j;
                yd[cntd] = i;
            }else if(mp[i][j] == .){
                tj++;
            }
        }
    }
    for(int i = 1;i <= cntd;i++){
        tmp.id = i;
        tmp.tm = 0;
        tmp.y = yd[i];
        tmp.x = xd[i];
        viss[tmp.id][tmp.y][tmp.x] = true;
        q.push(tmp);
    }
    while(!q.empty()){
        
        tmp = q.front();
        q.pop();
        for(int k = 0;k < 4;k++){
            nxt.id = tmp.id;
            nxt.tm = tmp.tm + 1;
            nxt.y = tmp.y + dy[k];
            nxt.x = tmp.x + dx[k];
            if(!judge(nxt.y,nxt.x)) continue;
            if(viss[nxt.id][nxt.y][nxt.x]) continue;
            if(mp[nxt.y][nxt.x] == .){ 
            hh = nxt.tm;
                for(;hh <= lm;hh++)addedge((hh-1)*cntd+nxt.id-1,(nxt.y-1)*mm+nxt.x-1);
            }
            viss[nxt.id][nxt.y][nxt.x] = true;
            q.push(nxt);
        }
    }
}

int main() {
    int T,ans;
    cin>>T;
    while(T--) {
        init();
        nn = read();
        mm = read();
        
        for(int i = 1;i <= nn;i++){
            scanf("%s",mp[i]+1);
        }
        
        bfs();
        ans = hungary();
        if(ans != tj){
            cout<<"impossible"<<endl;
            continue;
        }
        ans = 0;
        for(int i = 0;i <= lm;i++){
            if(linker[i] != -1){
                //cout<<i/mm+1<<" "<<i%mm+1<<" "<<yd[linker[i]%cntd]<<" "<<xd[linker[i]%cntd]<<" "<<linker[i]/cntd+1<<endl;
                ans = max(linker[i]/cntd+1,ans);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

POJ - 3057 Evacuation

标签:tmp   line   each   ever   nta   spec   obs   eva   which   

原文地址:https://www.cnblogs.com/hyfer/p/9476703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!