跳转至

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

题目

清奇的 2-SAT 建模题。

题意

有一张地图,有一些激光炮台、障碍和反射镜,分配炮台的方向,使炮台不互相攻击。

建模

首先将炮台从 \(1\) 到 \(p\) 编号。

预处理出每一个炮台横着和竖着放能打到哪些点,如果会打到某个炮台,则炮台不能放在这个方向。如果炮台横竖都不能放,则无解。

我们规定 \(0\) 表示竖放炮台,\(1\) 表示横放炮台,\(a_i\) 表示第 \(i\) 个炮台的朝向,\(S(i,j)\) 表示第 \(i\) 个炮台朝向 \(j\) 时能打到的点的集合,如果这个炮台打不到自己,不包括第 \(i\) 个炮台,否则包括。

然后我们统计每一个空地可能被哪些炮台在哪些方向打到。如果没有炮台能打到这块空地,无解。

现在,所有空地都会被 \(1\) 或 \(2\) 个炮台与方向打中,也就是对于任意空地 \((x,y)\) 有 \(1\) 或 \(2\) 个 \((i,j)\) 使 \((x,y)\in S(i,j)\)。感性理解一下,如果有超过 \(2\) 个炮台可以打到空地,那一定有一个炮台的激光可以顺着另一个炮台激光的轨迹打爆它。

图解

既然炮台数量不超过 \(2\),这不就是 2-SAT 吗?

枚举空地 \((x,y)\),我们可以列出约束:

\[ \begin{cases} a_i=j & \text{有且仅有一组} (i,j)\text{使}(x,y)\in S(i,j) \\ (a_{i_0}=j_0 \operatorname{or} a_{i_1}=j_1)= 1 & \text{有两组} (i,j)\text{使}(x,y)\in S(i,j)\text{ 记为 $(i_0,j_0)$ 和 $(i_1,j_1)$} \\ \end{cases} \]

再加上炮台不互打的约束,即对于每一个 \(1\le i \le p\),\(j \in \left\{0,1\right\}\),如果 \(S(i,j)\) 包含某个炮台(也就是炮台会打到另外炮台),则 \(a_i\not= j\)

求解 2-SAT 问题即可。

参考程序
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// by juruo999
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>

using namespace std;

namespace sat{  // 2-SAT

    const int maxn=1000006;
    vector<int> e[maxn*2];

    int n;
    inline int rho(int i,bool x){   // <i,x>
        return i*2-x;
    }
    void add(int i,bool a,int j,bool b){    // x[i]==a => x[j]  ==b
        e[rho(i,a)].push_back(rho(j,b));
        e[rho(j,b^1)].push_back(rho(i,a^1));
    }

    int dfn[maxn*2],low[maxn*2],vis[maxn*2],cnt=0;
    int scc[maxn*2],sccn=0,stk[maxn*2],tp=0;
    void dfs(int u){
        low[u]=dfn[u]=++cnt;
        stk[++tp]=u;
        vis[u]=1;
        for(int i=e[u].size()-1;i>=0;i--){
            int v=e[u][i];
            if(!dfn[v]){
                dfs(v);
                low[u]=min(low[u],low[v]);
            }else if(vis[v]){
                low[u]=min(low[u],dfn[v]);
            }
        }
        if(low[u]==dfn[u]){
            sccn++;
            while(stk[tp]!=u){
                vis[stk[tp]]=0;
                scc[stk[tp]]=sccn;
                tp--;
            }
            vis[stk[tp]]=0;
            scc[stk[tp]]=sccn;
            tp--;
        }
    }

    bool x[maxn];

    inline void clear(){
        for(int i=1;i<=2*n;i++){
            e[i].clear();dfn[i]=vis[i]=low[i]=scc[i]=stk[i]=0;
        }
        cnt=sccn=tp=n=0;
    }
    inline void init(int n_){
        clear();
        n=n_;
    }

    bool solve(){
        for(int i=1;i<=2*n;i++){
            if(!dfn[i]){ dfs(i); }
        }
        for(int i=1;i<=n;i++){
            if(scc[rho(i,0)]==scc[rho(i,1)]){
                return 0;
            }else{
                x[i]=scc[rho(i,0)]>scc[rho(i,1)];
            }
        }
        return 1;
    }

    bool& get(int i){
        return x[i];
    }

}

void work();

int main(){
    int _;scanf("%d",&_);while(_--){
        work();
    }
    return 0;
}

int n,m;
char c[55][55];

struct pos{ // 二维向量
    int x,y; pos(){x=y=0;} pos(int x_,int y_){ x=x_,y=y_; }
    pos operator+(pos b){ return pos(x+b.x,y+b.y); }
    pos operator-(pos b){ return pos(x-b.x,y-b.y); }
    pos zheng(){ return pos(y,x); } pos fu(){ return pos(-y,    -x); }
    // zheng '\' -1,0 -> 0,-1  0,1 -> 1,0  1,0 -> 0,1  x,y ->   y,x
    // fu    '/' -1,0 -> 0,1   0,1 -> -1,0 1,0 -> 0,-1 x,y ->   -y,-x
    bool in(){ return x<=n && x>=1 && y<=m && y>=1; }
    bool operator<(pos b) const{ return (x==b.x)?(y<b.y):(x<b.  x); }
    bool operator==(pos b) const{ return (x==b.x) && (y==b. y); }
};

pos dire[]={pos(0,0),pos(1,0),pos(-1,0),pos(0,1),pos(0, -1)}; // '|' -> 1,2 '-' -> 3,4

int atnum=0;    // 炮台数量
pos at[2505];   // 炮台位置

vector<pos> att[2505][2];   // i 号炮台方向为 j 能打到哪些炮台
bool chongtu[2505][2];      // i 号炮台方向为 j 是否能打到某个  炮台

vector<pair<int,int> > frm[55][55]; // 每一个空地被哪些 (i,j)   打到

void work(){

    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",c[i]+1);
    }

    atnum=0;

    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(c[i][j]=='|'){
                c[i][j]='-';
            }
            if(c[i][j]=='-'){
                at[++atnum]=pos(i,j);
            }
            frm[i][j].clear();
        }
    }

    for(int t=1;t<=atnum;t++){  // 预处理出炮台打到哪些点
        for(int i=0;i<=1;i++){
            att[t][i].clear();chongtu[t][i]=0;
            for(int j=1;j<=2;j++){
                pos fx=dire[i*2+j];
                for(pos u=at[t]+fx;u.in() && c[u.x][u.y]!='#';  u=u+fx){
                    if(c[u.x][u.y]=='-'){
                        chongtu[t][i]=1;break;
                    }else if(c[u.x][u.y]=='.'){
                        att[t][i].push_back(u);
                    }else if(c[u.x][u.y]=='\\'){
                        fx=fx.zheng();
                    }else if(c[u.x][u.y]=='/'){
                        fx=fx.fu();
                    }
                }
                if(chongtu[t][i]){
                    break;
                }
            }
        }
        if(chongtu[t][0] && chongtu[t][1]){
            puts("IMPOSSIBLE");return;
        }
    }

    for(int t=1;t<=atnum;t++){  // 预处理空地被哪些炮台打到
        for(int i=0;i<=1;i++){
            if(chongtu[t][i]){
                continue;
            }
            for(int j=att[t][i].size()-1;j>=0;j--){
                pos tmp=att[t][i][j];
                frm[tmp.x][tmp.y].push_back(make_pair(t,i));
            }
        }
    }

    // 建模部分 

    sat::init(atnum);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){  // 保证每一个空地被覆盖
            if(c[i][j]=='.'){
                if(frm[i][j].size()==0 || frm[i][j].size()>=3){
                    puts("IMPOSSIBLE");return;
                }
                if(frm[i][j].size()==1){
                    sat::add(frm[i][j][0].first,(1^(frm[i][j]   [0].second)),frm[i][j][0].first,frm[i][j]  [0].second);
                }
                if(frm[i][j].size()==2){
                    sat::add(frm[i][j][0].first,(1^(frm[i][j]   [0].second)),frm[i][j][1].first,frm[i][j]  [1].second);
                }
            }
        }
    }
    for(int t=1;t<=atnum;t++){
        for(int i=0;i<=1;i++){
            if(chongtu[t][i]){  // 防止炮台互相攻击
                sat::add(t,i,t,i^1);
            }
        }
    }

    if(sat::solve()){
        puts("POSSIBLE");
        for(int i=1;i<=atnum;i++){  // 输出
            if(sat::get(i)){
                c[at[i].x][at[i].y]='-';
            }else{
                c[at[i].x][at[i].y]='|';
            }
        }
        for(int i=1;i<=n;i++){
            puts(c[i]+1);
        }
        return;
    }else{
        puts("IMPOSSIBLE");return;
    }

}