题解:P5297 [北京省选集训2019] 完美塔防

· · 题解

include <bits/stdc++.h>

using namespace std ;

define int long long

define rep(i,x,y) for(int i=x;i<=y;i++)

define fire signed

define pb push_back

define in(x) scanf("%lld",&x)

int t,ttt; int n,m; char c[55][55]; vector<pair<int,int>>ss; vector<pair<int,int>>op; const int N=1e4+10; / 1~nm 竖着 1+nm~2nm 横着 / vector<int>v[N]; int get(int i,int j) {return (i-1)m+j;} int p(int x) { return x+nm; } int dfn[N],low[N],idx; int is[N],tot,val[N]; stack<int>st; void tarjan(int x) { dfn[x]=low[x]=++idx; is[x]=1; st.push(x); for(auto to:v[x]) { if(!dfn[to]) tarjan(to),low[x]=min(low[x],low[to]); else if(is[to]) low[x]=min(low[x],dfn[to]); } if(low[x]==dfn[x]) { int p; ttt++; do{ p=st.top(); st.pop(); is[p]=false; val[p]=ttt; }while(p!=x); } } int dx[]={0,0,-1,1}; int dy[]={1,-1,0,0}; int cnt; vector<pair<int,int>>heng,shu; vector<pair<int,int>>a,b; void dfs(int x,int y,int k,int now) { if(c[x][y]=='#'||x<1||y<1||x>n||y>m) return; if(c[x][y]=='|'||c[x][y]=='-') { cnt++; if(now==0||now==1) heng.push_back({x,y}); else shu.push_back({x,y}); return ; } if(c[x][y]=='\') { if(k==0) dfs(x+dx[3],y+dy[3],3,now); else if(k==1) dfs(x+dx[2],y+dy[2],2,now); else if(k==2) dfs(x+dx[1],y+dy[1],1,now); else dfs(x+dx[0],y+dy[0],0,now); return ; } if(c[x][y]=='/') { if(k==3) dfs(x+dx[1],y+dy[1],1,now); else if(k==1) dfs(x+dx[3],y+dy[3],3,now); else if(k==2) dfs(x+dx[0],y+dy[0],0,now); else if(k==0) dfs(x+dx[2],y+dy[2],2,now); return; } int tx=x+dx[k],ty=y+dy[k]; dfs(tx,ty,k,now); } vector<int>vv,h,s,A,B; void dfs1(int x,int y,int k,int now) { if(c[x][y]=='#'||x<1||y<1||x>n||y>m) return; if(c[x][y]=='|'||c[x][y]=='-') { cnt++; vv.push_back(k); a.push_back({x,y}); if(now==0||now==1) A.pb(cnt); else B.pb(cnt); if(k==0||k==1) h.push_back(cnt); else s.push_back(cnt); return ; } if(c[x][y]=='\') { if(k==0) dfs1(x+dx[3],y+dy[3],3,now); else if(k==1) dfs1(x+dx[2],y+dy[2],2,now); else if(k==2) dfs1(x+dx[1],y+dy[1],1,now); else dfs1(x+dx[0],y+dy[0],0,now); return ; } if(c[x][y]=='/') { if(k==3) dfs1(x+dx[1],y+dy[1],1,now); else if(k==1) dfs1(x+dx[3],y+dy[3],3,now); else if(k==2) dfs1(x+dx[0],y+dy[0],0,now); else if(k==0) dfs1(x+dx[2],y+dy[2],2,now); return; } int tx=x+dx[k],ty=y+dy[k]; dfs1(tx,ty,k,now); } fire main() { in(t); while(t--) { op.clear(); ss.clear(); in(n),in(m); idx=ttt=false; rep(i,1,nm2) v[i].clear(); memset(val,0,sizeof val); memset(dfn,0,sizeof dfn); memset(low,0,sizeof low); rep(i,1,n) { rep(j,1,m) { cin>>c[i][j]; if(c[i][j]=='.') ss.push_back({i,j}); else if(c[i][j]=='|'||c[i][j]=='-') op.push_back({i,j}); } } int f=0; for(auto x:ss) { vv.clear(); cnt=false; h.clear(),s.clear(),A.clear(),B.clear(),a.clear(); int i=x.first,j=x.second; rep(k,0,3) { int tx=i+dx[k],ty=j+dy[k]; dfs1(tx,ty,k,k); } if(cnt==4||!cnt) { puts("IMPOSSIBLE"); f=1; break; } if(cnt==1) { if(h.size()) { v[get(a[0].first,a[0].second)].push_back(p(get(a[0].first,a[0].second))); }else v[p(get(a[0].first,a[0].second))].push_back(get(a[0].first,a[0].second)); }else if(cnt==2) { if(max(A.size(),B.size())==2) { puts("IMPOSSIBLE"); f=1; break; } if(h.size()==2) { v[get(a[0].first,a[0].second)].pb(p(get(a[1].first,a[1].second))); v[get(a[1].first,a[1].second)].pb(p(get(a[0].first,a[0].second))); }else if(s.size()==2) { v[p(get(a[0].first,a[0].second))].pb(get(a[1].first,a[1].second)); v[p(get(a[1].first,a[1].second))].pb(get(a[0].first,a[0].second)); }else { if(vv[0]==1||vv[0]==0) { v[get(a[0].first,a[0].second)].push_back(get(a[1].first,a[1].second)); v[p(get(a[1].first,a[1].second))].push_back(p(get(a[0].first,a[0].second))); }else { v[get(a[1].first,a[1].second)].push_back(get(a[0].first,a[0].second)); v[p(get(a[0].first,a[0].second))].push_back(p(get(a[1].first,a[1].second))); } } } else { if(A.size()==2) { for(auto io:A) { if(vv[io-1]==1||vv[io-1]==0) v[p(get(a[io-1].first,a[io-1].second))].pb(get(a[io-1].first,a[io-1].second)); else v[get(a[io-1].first,a[io-1].second)].pb(p(get(a[io-1].first,a[io-1].second))); } if(vv[B[0]-1]==1||vv[B[0]-1]==0) v[get(a[B[0]-1].first,a[B[0]-1].second)].push_back(p(get(a[B[0]-1].first,a[B[0]-1].second))); else v[p(get(a[B[0]-1].first,a[B[0]-1].second))].push_back(get(a[B[0]-1].first,a[B[0]-1].second)); }else { for(auto io:B) { if(vv[io-1]==1||vv[io-1]==false) v[p(get(a[io-1].first,a[io-1].second))].pb(get(a[io-1].first,a[io-1].second)); else v[get(a[io-1].first,a[io-1].second)].pb(p(get(a[io-1].first,a[io-1].second))); } if(vv[A[0]-1]==1||vv[A[0]-1]==0) v[get(a[A[0]-1].first,a[A[0]-1].second)].push_back(p(get(a[A[0]-1].first,a[A[0]-1].second))); else v[p(get(a[A[0]-1].first,a[A[0]-1].second))].push_back(get(a[A[0]-1].first,a[A[0]-1].second)); } } } if(f) continue; for(auto x:op) { cnt=false; heng.clear(),shu.clear(); int i=x.first,j=x.second; rep(k,0,3) { int tx=i+dx[k],ty=j+dy[k]; dfs(tx,ty,k,k); } if(shu.size()) v[get(i,j)].pb(p(get(i,j))); if(heng.size()) v[p(get(i,j))].pb(get(i,j)); } if(f) continue; rep(i,1,2nm) if(!dfn[i]) tarjan(i); rep(i,1,n*m) { if(val[i]==val[p(i)]) { puts("IMPOSSIBLE"); f=1; break; } } if(f) continue; cout<<"POSSIBLE\n"; rep(i,1,n) { rep(j,1,m) { if(c[i][j]!='|'&&c[i][j]!='-') { cout<<c[i][j]; continue; } if(val[get(i,j)]<val[p(get(i,j))]) cout<<'|'; else cout<<'-'; } cout<<endl; } } return false; }