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
|
language ESSENCE' 1.0
given initial_grid : matrix indexed by [int(1..n),int(1..n)] of int(0,1)
letting fig = flatten(initial_grid)
letting spaces = (n * n) - sum(fig)
find order : matrix indexed by [int(1..spaces)] of int(1..n*n)
find moves : matrix indexed by [int(1..spaces-1)] of int(-1,1,-n,n)
ifdef(`optimise_moves', `dnl
find chdir : matrix indexed by [int(2..spaces-1)] of int(0,1)
minimising sum(chdir)
')dnl
such that
ifdef(`optimise_moves', `dnl
forAll i: int(2..spaces-1) .
chdir[i] = 1 <-> moves[i-1] != moves[i],
')dnl
allDiff(order),
ifdef(`fill_all', `dnl
forAll i: int(1..n*n) .
fig[i] = 0 <-> exists j: int(1..spaces) . order[j] = i,
', `dnl
forAll i: int(1..spaces) .
fig[order[i]] != 1,
')dnl
ifdef(`no_block_moves', `dnl
forAll i: int(1..spaces-1) .
fig[order[i] + moves[i]] != 1,
')dnl
ifdef(`no_repeat_moves', `dnl
forAll i: int(2..spaces-1) .
forAll j: int(1..i-1) .
order[i] + moves[i] != order[j],
')dnl
forAll i: int(1..spaces-1) .
order[i] + moves[i] = order[i+1],
forAll i: int(1..spaces-1) .
ifdef(`up_down_bounds', `dnl
((order[i] <= n) -> (moves[i] != -n)) /\
((order[i] > n * n - n) -> (moves[i] != n)) /\
')dnl
((order[i] % n = 1) -> (moves[i] != -1)) /\
((order[i] % n = 0) -> (moves[i] != 1)) /\
true,
ifdef(`opposite_move_check', `dnl
forAll i: int(2..spaces-1) .
moves[i] != -moves[i-1],
')dnl
forAll i: int(2..spaces-1) .
(moves[i] = moves[i-1]) \/
(fig[order[i] + moves[i-1]] = 1) \/
(exists j: int(1..i-1) . order[i] + moves[i-1] = order[j]) \/
(moves[i-1] = -n /\ order[i] <= n) \/
ifdef(`throw_exception', `dnl
(moves[i-1] = n /\ order[i] > n * n - 1) \/
', `dnl
(moves[i-1] = n /\ order[i] > n * n - n) \/
')dnl
(moves[i-1] = -1 /\ order[i] % n = 1) \/
(moves[i-1] = 1 /\ order[i] % n = 0) \/
false,
true
|