POJ3057 Evacuation Problem Solving report

Source: Internet
Author: User

The difficulty of this problem should be to think that this is a binary map matching

When I think of this, it's a map.

//by baobaopangzi88#include <iostream>#include<sstream>#include<ios>#include<iomanip>#include<functional>#include<algorithm>#include<vector>#include<string>#include<list>#include<queue>#include<deque>#include<stack>#include<Set>#include<map>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<climits>#include<cctype>#defineINF 0x3f3f3f3f#defineMP (x, y) make_pair (x, y)#definePB (x) push_back (x)#defineREP (x,n) for (int x=0; x<n; X + +)#defineREP2 (X,L,R) for (int x=l; x<=r; X + +)#defineDEP (x,r,l) for (int x=r; x>=l; x--)#defineCLR (a,x) memset (a,x,sizeof (A))#defineIT iterator#definePI 3.14159265358979323846#define_ Ios_base::sync_with_stdio (0); Cin.tie (0);#defineX First#defineY Second#defineMax_v 10101#defineMAXN 13using namespaceStd;typedefLong LongLl;typedef pair<int,int>PII;intn,m,v;CharM[maxn][maxn];vector<int> g[30000];vector<int>dx,dy,px,py;intD[MAXN][MAXN][MAXN][MAXN];intdx[4]={-1,1,0,0},dy[4]={0,0,-1,1};intmatch[30000];BOOLused[30000];voidAdd_edge (int  from,intTo ) {g[ from].    PB (to); G[to]. PB ( from);}BOOLDfsintv) {Used[v]=1;  for(intI=0; I<g[v].size (); + +i) {        intto=g[v][i],w=Match[to]; if(w<0|| !USED[W] &&DFS (W)) {Match[v]=to ; Match[to]=v; return true; }    }    return false;}voidBFsintXintYintD[MAXN][MAXN]) {D[x][y]=0; Queue<int>qx,qy;    Qx.push (x);    Qy.push (y);  while(!Qx.empty ()) {x=Qx.front (); Qx.pop (); Y=Qy.front (); Qy.pop ();  for(intI=0;i<4;++i) {            intnx=x+dx[i],ny=y+Dy[i]; if(nx>=0&& nx<n && ny>=0&&NY<m && m[nx][ny]=='.'&& d[nx][ny]<0) {D[nx][ny]=d[x][y]+1;                Qx.push (NX);            Qy.push (NY); }        }    }}intsolve () {dx.clear ();d y.clear ();    Px.clear ();p y.clear (); memset (D,-1,sizeof(D));  for(intI=0; i<n;++i) for(intj=0; j<m;++j) {        if(m[i][j]=='D') {dx.            PB (i); Dy.            PB (j);        BFS (I,j,d[i][j]); }        Else if(m[i][j]=='.') {px.            PB (i); Py.        PB (j); }    }    //Build_graph    intTemp=n*m;//the maximum number of times    intD=dx.size (), p=px.size (); V=n*m*d+p;  for(intv=0; v<v;++v) g[v].clear ();  for(intI=0; i<d;++i) for(intj=0; j<p;++j) for(intt=0; t<temp;++6) {        if(d[dx[i]][dy[i]][px[j]][py[j]]>0&& d[dx[i]][dy[i]][px[j]][py[j]]<=t+1) {Add_edge (t*d+i,temp*d+j); }    }    if(p==0)return 0; intres=0; memset (Match,-1,sizeof(match));  for(intt=0; t<temp;++t) {         for(intv=t*d;v< (t+1) *d;++v) {            if(match[v]<0) {memset (used,0,sizeof(used)); if(Dfs (v)) {res++; }            }                }        if(res==p)returnt+1; }    return-1;}intMain () {intT; CIN>>u;  while(t--) {cin>>n>>m;  for(intI=0; i<n;++i) Cin>>M[i]; inttemp=solve (); if(temp<0) cout<<"Impossible"<<Endl; Elsecout<<temp<<Endl; }    return 0;}

POJ3057 Evacuation Problem Solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.