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

· · 题解

很有意思的一道题。

每个炮台只有横竖两种状态,而我们需要调整状态使得满足一个条件。
这暗示我们要用2-SAT来解决这个问题。

对于每个炮台,我们都看做它是2-SAT中的一个变量。
然后模拟一下它横着、竖着打激光的结果。

感性理解一下,每个空地最多被两个炮台打到。
其实是我不会严谨证明
然后这个2-SAT的模型就很明显了。
对于每个空地,都要被能打到那里的炮台中至少一个打到,也就有\text{or}的关系。
如果只有一个炮台能打到那里也没关系,按照正常写法不会有错。
最后别忘了特判,如果有空地无论如何都不会被打到,也要输出impossible。

最后直接上2-SAT的模板即可。
如果不会2-SAT,可以先做一下模板题:【模板】2-SAT 问题

最后无耻地丢下std就跑。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>

using namespace std;
typedef long long LL;
#define pb push_back

const int maxn = 505;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};

vector<int> adj[maxn],ebk[maxn],cov[maxn][maxn];
char str[maxn][maxn];int R,C,T;
bool _cov[maxn][maxn];
bool cov0[maxn][maxn];
bool cov1[maxn][maxn];
int id[maxn][maxn],tot,scc[maxn],iscc;

int dfn[maxn],low[maxn],idx;
int stk[maxn],top;bool ins[maxn];
int que[maxn],head,tail,deg[maxn],pos[maxn];

#define clr(_) memset(_,0,sizeof _)
bool inG(int x,int y) {
    return x>=1&&x<=R&&y>=1&&y<=C&&str[x][y]!='#';
}
bool beam(int x,int y) {
    return str[x][y]=='|'||str[x][y]=='-';
}
bool dfs(bool cov[maxn][maxn],int x,int y,int d) {
    cov[x][y]=true;
    if (!inG(x+dx[d],y+dy[d]))
        return true;
    int nx=x+dx[d],ny=y+dy[d];
    if (beam(nx,ny)) return false;
    if (str[nx][ny]=='.') return dfs(cov,nx,ny,d);
    else return dfs(cov,nx,ny,d^(str[nx][ny]=='/'?1:3));
}
void tarjan(int u) {
    dfn[u]=low[u]=++idx;
    stk[++top]=u;ins[u]=true;
    for (unsigned j=0;j<adj[u].size();j++)
    {
        int v=adj[u][j];
        if (!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
        else if (ins[v]) low[u]=min(low[u],dfn[v]);
    }
    if (low[u]!=dfn[u]) return ;
    scc[u]=++iscc;
    while (stk[top]!=u) {
        int v=stk[top--];
        ins[v]=false;scc[v]=iscc;
    }
    ins[u]=false;top--;
}
void solve()
{
    scanf("%d %d",&R,&C);
    for (int i=1;i<=R;i++)
        scanf("%s",str[i]+1);
    for (int i=1;i<=tot*2+1;i++)
        adj[i].clear(),ebk[i].clear();
    for (int i=1;i<=R;i++)
    for (int j=1;j<=C;j++) cov[i][j].clear();
    clr(_cov);clr(dfn);tot=idx=iscc=0;

    for (int i=1;i<=R;i++)
    for (int j=1;j<=C;j++)
    if (beam(i,j)) {
        id[i][j]=++tot;
        clr(cov0);bool tag0=dfs(cov0,i,j,1)&dfs(cov0,i,j,3);
        clr(cov1);bool tag1=dfs(cov1,i,j,0)&dfs(cov1,i,j,2);
        if (!tag0&&!tag1) {printf("IMPOSSIBLE\n");return ;}
        if (!tag0) {
            for (int x=1;x<=R;x++)
            for (int y=1;y<=C;y++)
            if (str[x][y]=='.'&&cov1[x][y])
                cov[x][y].pb(tot<<1|1);
            adj[tot<<1].pb(tot<<1|1);
        }
        else if (!tag1) {
            for (int x=1;x<=R;x++)
            for (int y=1;y<=C;y++)
            if (str[x][y]=='.'&&cov0[x][y])
                cov[x][y].pb(tot<<1);
            adj[tot<<1|1].pb(tot<<1);
        }
        else {
            for (int x=1;x<=R;x++)
            for (int y=1;y<=C;y++)
            if (str[x][y]=='.') {
                if (cov0[x][y]&&cov1[x][y]) _cov[x][y]=true;
                else if (cov0[x][y]) cov[x][y].pb(tot<<1);
                else if (cov1[x][y]) cov[x][y].pb(tot<<1|1);
            }
        }
    }
    for (int i=1;i<=R;i++)
    for (int j=1;j<=C;j++)
    if (str[i][j]=='.'&&!_cov[i][j])
    {
        int siz=cov[i][j].size(),u,v;
        if (siz==0) {printf("IMPOSSIBLE\n");return ;}
        else if (siz==1) u=cov[i][j][0],adj[u^1].pb(u);
        else if (siz==2) u=cov[i][j][0],v=cov[i][j][1],adj[u^1].pb(v),adj[v^1].pb(u);
    }

    for (int i=2;i<=(tot<<1|1);i++)
        if (!dfn[i]) tarjan(i);
    for (int i=1;i<=tot;i++)
        if (scc[i<<1]==scc[i<<1|1]) {printf("IMPOSSIBLE\n");return ;}
    for (int v,u=2;u<=(tot<<1|1);u++)
    for (unsigned j=0;j<adj[u].size();j++)
    if (scc[v=adj[u][j]]!=scc[u])
        ebk[scc[v]].pb(scc[u]),deg[scc[u]]++;

    head=1;tail=0;
    for (int i=1;i<=iscc;i++)
        if (!deg[i]) que[++tail]=i;
    while (head<=tail) {
        int u=que[head];pos[u]=head++;
        for (unsigned j=0;j<ebk[u].size();j++)
        {
            int v=ebk[u][j];
            if (!--deg[v]) que[++tail]=v;
        }
    }

    for (int i=1;i<=tot*2+1;i++)
        ebk[i].clear(),adj[i].clear();
    printf("POSSIBLE\n");
}
int main(){
    for (cin >> T; T; T--) solve();
}

ps:
要输出方案也很简单,直接把2-SAT跑出来的结果替换原图输出。