标签:tmp line each ever nta spec obs eva which
Description
Input
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
//注意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; }
标签:tmp line each ever nta spec obs eva which
原文地址:https://www.cnblogs.com/hyfer/p/9476703.html