题解:P13173 [GCJ 2017 #2] Beaming With Joy
题目描述
本题是一个激光与镜子网格的约束满足问题。我们需要判断能否通过旋转部分激光发射器(- 变 |,或 | 变 -),使得:
- 每个空单元格(
.)至少被一束激光经过; - 没有任何激光发射器(
-或|)被任何激光束击中(包括自己)。
算法分析
暴力做法
若网格中有
验证时,从每个激光器沿其两个方向发射光线,记录经过的空格,并检查是否打到任何发射器。若所有空格都被覆盖且没有发射器被击中,则该方案合法。
时间复杂度为
代码
#include<bits/stdc++.h>
using namespace std;
int T, n, m, tot;
char a[55][55], b[55][55];
int id[55][55], X[105], Y[105];
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int to1[]={2,3,0,1}, to2[]={3,2,1,0}; //方向数组 //方向数组
int cov[2505];
// 从(x,y)沿dir方向模拟光线, 返回1表示安全(没打到发射器)
int go(int x,int y,int dir) {
while(1) {
x += dx[dir];
y += dy[dir];
if(x<1||x>n||y<1||y>m) return 1; // 出界,安全
if(b[x][y]=='#') return 1; // 撞墙,安全
if(b[x][y]=='-'||b[x][y]=='|') return 0; // Oh,no!打到发射器了!
if(b[x][y]=='/') dir=to2[dir];
else if(b[x][y]=='\\') dir=to1[dir];
else if(b[x][y]=='.') cov[(x-1)*m+y]=1;
}
}
// 检查当前网格b是否合法
int check() {
memset(cov,0,sizeof(cov));
// 所有激光器发射
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
if(b[i][j]=='-') {
if(!go(i,j,0)) return 0;
if(!go(i,j,1)) return 0;
}
else if(b[i][j]=='|') {
if(!go(i,j,2)) return 0;
if(!go(i,j,3)) return 0;
}
}
// 检查空格是不是都被照到了
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]=='.' && !cov[(i-1)*m+j]) return 0;
return 1;
}
// 枚举第dep个激光器的方向
int dfs(int dep) {
if(dep>tot) return check();
int x=X[dep], y=Y[dep];
b[x][y]='-';
if(dfs(dep+1)) return 1;
b[x][y]='|';
if(dfs(dep+1)) return 1;
b[x][y]=a[x][y]; // 回溯
return 0;
}
int main() {
scanf("%d",&T);
for(int cas=1;cas<=T;cas++) {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
// 给激光器编号,记录坐标
tot=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i][j]=='-'||a[i][j]=='|') {
id[i][j]=++tot;
X[tot]=i; Y[tot]=j;
}
memcpy(b,a,sizeof(a));
printf("Case #%d: ",cas);
if(dfs(1)) {
puts("POSSIBLE");
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) putchar(b[i][j]);
putchar('\n');
}
}
else puts("IMPOSSIBLE");
}
return 0;
}
转化为 2-SAT
暴力枚举的指数级开销源于各激光器的方向组合相互独立,但实际上约束条件往往只涉及少数变量。本题的两个约束:
- 单激光器安全约束:若某个方向会导致该激光器的光线击中任意发射器,则该方向不可选,只能选另一个方向。
- 空格覆盖约束:每个空格需要被至少一个激光器的某个方向照射到,候选数量通常很少(由于光线路径在网格中传播,每个空格能被照到的激光器方向通常不超过
2 个)。
这些约束可以转化为标准的 2-SAT 问题。每个激光器对应一个布尔变量,取真表示水平(-),取假表示垂直(|)。每个约束最多涉及两个变量,因此可以用 2-SAT 高效求解。
若不熟悉 2-SAT,可先参阅 P4782 【模板】2-SAT。
变量定义
设共有 -),|)。两者互为否定。
单激光器安全约束
对每个激光器
- 若水平方向不安全(即某条光线打到了发射器),则必须选择垂直方向,添加蕴含边:
\neg H_i \rightarrow V_i ,即i+L \rightarrow i (因为H_i 对应节点i ,V_i 对应i+L ,\neg H_i 就是i+L )。 - 若垂直方向不安全,则必须选择水平方向,添加边:
\neg V_i \rightarrow H_i ,即i \rightarrow i+L 。 - 若两个方向均不安全,则无解。
若某个方向安全,则将其路径上经过的所有空格记录为该激光器该方向的候选覆盖。
空格覆盖约束
对于每个空格,收集所有能覆盖它的 (激光器编号, 方向) 候选。由于光线路径的几何性质,每个空格被照到的候选数通常不超过
- 若无候选,则无解。
- 若仅有一个候选
(i, d) (d=0 表示水平,d=1 表示垂直),则强制该方向为真,添加边:\neg (i=d) \rightarrow (i=d) ,即若变量取反则推出本身,等价于强制该变量取指定值。 - 若有两个候选
(a, d_a) 和(b, d_b) ,则需要至少选一个,即\neg (a=d_a) \rightarrow (b=d_b) 和\neg (b=d_b) \rightarrow (a=d_a) 。用节点表示则为:(\neg A) \rightarrow B 和(\neg B) \rightarrow A ,其中A 表示(a, d_a) 对应的布尔节点,B 表示(b, d_b) 对应的节点。
求解与输出
建图后,使用 Tarjan 算法求强连通分量(SCC)。若存在某个变量
否则,按拓扑序(即 SCC 编号的逆序)为每个变量赋值:若
代码
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
const int MAXM = 20005;
int head[MAXN], ver[MAXM], nxt[MAXM], tot;
int dfn[MAXN], low[MAXN], num, stk[MAXN], ins[MAXN], tp, c[MAXN], scc;
int R, C, L;
char mp[55][55];
int laser_id[55][55];
int lx[105], ly[105];
vector<pair<int,int>> cand[2505]; // 空格 -> (激光器,方向)
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
const int refl_b[4] = {2, 3, 0, 1};
const int refl_f[4] = {3, 2, 1, 0};
inline void addedge(int x, int y) {
ver[++tot] = y; nxt[tot] = head[x]; head[x] = tot;
}
void tarjan(int u) {
dfn[u] = low[u] = ++num;
stk[++tp] = u; ins[u] = 1;
for (int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
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]) {
++scc; int v;
do {
v = stk[tp--]; ins[v] = 0;
c[v] = scc;
} while (u != v);
}
}
// 走光线, 返回1表示没打到发射器
bool simulate(int x, int y, int dir, vector<int>& path) {
while (true) {
x += dx[dir]; y += dy[dir];
if (x < 1 || x > R || y < 1 || y > C || mp[x][y] == '#')
return true;
if (mp[x][y] == '-' || mp[x][y] == '|')
return false;
if (mp[x][y] == '.')
path.push_back((x-1)*C + y);
else if (mp[x][y] == '/')
dir = refl_f[dir];
else if (mp[x][y] == '\\')
dir = refl_b[dir];
}
}
void solve() {
memset(head, 0, sizeof head);
memset(dfn, 0, sizeof dfn);
tot = num = scc = tp = 0;
L = 0;
memset(laser_id, 0, sizeof laser_id);
for (int i = 1; i <= R*C; ++i) cand[i].clear();
scanf("%d%d", &R, &C);
for (int i = 1; i <= R; ++i) scanf("%s", mp[i] + 1);
for (int i = 1; i <= R; ++i)
for (int j = 1; j <= C; ++j)
if (mp[i][j] == '-' || mp[i][j] == '|') {
laser_id[i][j] = ++L;
lx[L] = i; ly[L] = j;
}
bool ok = true;
vector<int> path;
// 每个激光器两个方向都试试
for (int id = 1; id <= L; ++id) {
int x = lx[id], y = ly[id];
path.clear();
bool h1 = simulate(x, y, 0, path);
bool h2 = simulate(x, y, 1, path);
bool safe_h = h1 && h2;
if (safe_h) {
for (int pos : path)
if ((int)cand[pos].size() < 2)
cand[pos].push_back({id, 0});
} else {
addedge(id, id + L); // 横的不行, 只能竖
}
path.clear();
bool v1 = simulate(x, y, 2, path);
bool v2 = simulate(x, y, 3, path);
bool safe_v = v1 && v2;
if (safe_v) {
for (int pos : path)
if ((int)cand[pos].size() < 2)
cand[pos].push_back({id, 1});
} else {
addedge(id + L, id); // 竖的不行, 只能横
}
if (!safe_h && !safe_v) { ok = false; break; }
}
// 空格至少要有一个候选能照到
if (ok) {
for (int i = 1; i <= R && ok; ++i)
for (int j = 1; j <= C; ++j) {
if (mp[i][j] != '.') continue;
int pos = (i-1)*C + j;
if (cand[pos].empty()) { ok = false; break; }
if (cand[pos].size() == 1) {
int id = cand[pos][0].first, d = cand[pos][0].second;
int node = (d == 0 ? id : id + L);
int neg = (d == 0 ? id + L : id);
addedge(neg, node);
} else {
int a_id = cand[pos][0].first, a_d = cand[pos][0].second;
int b_id = cand[pos][1].first, b_d = cand[pos][1].second;
int a = (a_d == 0 ? a_id : a_id + L);
int a_neg = (a_d == 0 ? a_id + L : a_id);
int b = (b_d == 0 ? b_id : b_id + L);
int b_neg = (b_d == 0 ? b_id + L : b_id);
addedge(a_neg, b);
addedge(b_neg, a);
}
}
}
if (!ok) {
puts("IMPOSSIBLE");
return;
}
for (int i = 1; i <= 2*L; ++i)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= L; ++i)
if (c[i] == c[i+L]) { ok = false; break; }
if (!ok) {
puts("IMPOSSIBLE");
return;
}
puts("POSSIBLE");
for (int i = 1; i <= R; ++i) {
for (int j = 1; j <= C; ++j) {
if (mp[i][j] == '-' || mp[i][j] == '|') {
int id = laser_id[i][j];
putchar(c[id] > c[id+L] ? '|' : '-');
} else {
putchar(mp[i][j]);
}
}
putchar('\n');
}
}
int main() {
int T; scanf("%d", &T);
for (int cas = 1; cas <= T; ++cas) {
printf("Case #%d: ", cas);
solve();
}
return 0;
}
复杂度分析
- 光线模拟:对每个激光器模拟两个方向,每个方向最多遍历
O(RC) 个格子,总时间O(L \cdot RC) 。 - 建图边数:单变量约束最多
2L 条边;空格覆盖约束每个空格最多产生2 条边(若候选为2 个),故边数O(L + RC) 。 - Tarjan:
O(V+E) ,其中V = 2L ,E = O(L + RC) 。 - 总时间复杂度:
O(L \cdot RC) ,足以通过大数据限制。
参考
- P4782 【模板】2-SAT 问题