this repo has no description
1int: h; set of int: H = 1..h; % 板高度
2int: w; set of int: W = 1..w; % 板宽度
3array[H,W] of -1..5: b; % 板
4int: E = -1; % 空白格
5set of int: N = 0..4; % 填充并含有数字的网格
6int: F = 5; % 填充但不含有数字的网格
7
8% 位置 (i1,j1) 对位置 (i2,j2) 可见
9test visible(int: i1, int: j1, int: i2, int: j2) =
10 ((i1 == i2) /\ forall(j in min(j1,j2)..max(j1,j2))(b[i1,j] == E))
11 \/ ((j1 == j2) /\ forall(i in min(i1,i2)..max(i1,i2))(b[i,j1] == E));
12
13array[H,W] of var bool: l; % is there a light
14
15% 填充的网格没有灯泡
16constraint forall(i in H, j in W where b[i,j] != E)(l[i,j] == false);
17% lights next to filled numbered square agree
18
19constraint forall(i in H, j in W where b[i,j] in N)(
20 bool_sum_eq([ l[i1,j1] | i1 in i-1..i+1, j1 in j-1..j+1 where
21 abs(i1 - i) + abs(j1 - j) == 1 /\
22 i1 in H /\ j1 in W ], b[i,j]));
23% 每个空白网格是被照亮的
24constraint forall(i in H, j in W where b[i,j] == E)(
25 exists(j1 in W where visible(i,j,i,j1))(l[i,j1]) \/
26 exists(i1 in H where visible(i,j,i1,j))(l[i1,j])
27 );
28% 任何两个灯泡看不到彼此
29constraint forall(i1,i2 in H, j1,j2 in W where
30 (i1 != i2 \/ j1 != j2) /\ b[i1,j1] == E
31 /\ b[i2,j2] == E /\ visible(i1,j1,i2,j2))(
32 not l[i1,j1] \/ not l[i2,j2]
33 );
34
35solve satisfy;
36output [ if b[i,j] != E then show(b[i,j])
37 else if fix(l[i,j]) then "L" else "." endif
38 endif ++ if j == w then "\n" else " " endif |
39 i in H, j in W];