Solve a Hopido puzzle: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: syntax coloured, made p2js compatible) |
|||
Line 1,443: | Line 1,443: | ||
solution found in 67702 tries (0.09s) |
solution found in 67702 tries (0.09s) |
||
</pre> |
</pre> |
||
=={{header|Picat}}== |
|||
<lang Picat> |
|||
import sat. |
|||
main => |
|||
Grid = {{0,1,1,0,1,1,0}, |
|||
{1,1,1,1,1,1,1}, |
|||
{1,1,1,1,1,1,1}, |
|||
{0,1,1,1,1,1,0}, |
|||
{0,0,1,1,1,0,0}, |
|||
{0,0,0,1,0,0,0}}, |
|||
NR = len(Grid), NC = len(Grid[1]), |
|||
Es = [{(R,C), (R1,C1), _} : R in 1..NR, C in 1..NC, R1 in 1..NR, C1 in 1..NC, % Edges |
|||
((R1 = R, abs(C1 - C) = 3); (C1 = C, abs(R1 - R) = 3); % horizontal hop |
|||
(abs(R1 - R) = 2, abs(C1 - C) = 2)), % diagonal hop |
|||
Grid[R,C] = 1, Grid[R1,C1] = 1], |
|||
hcp_grid(Grid, Es), % find a Hamiltion cylce on the vertices in Grid through the edges in Es |
|||
solve(vars(Es)), |
|||
% Write the solution as a matrix, starting at the base tile 6|4: |
|||
M = {{0: _ in 1..NC} : _ in 1..NR}, |
|||
R1 = 6, C1 = 4, I = 0, |
|||
do |
|||
I := I + 1, M[R1,C1] := I, Stop = 0, |
|||
foreach({(R1,C1), (R2,C2), 1} in Es, break(Stop == 1)) |
|||
R1 := R2, C1 := C2, Stop := 1 |
|||
end |
|||
while ((R1,C1) != (6,4)), |
|||
foreach(R in 1..NR, C in 1..NC) |
|||
if M[R,C] = 0 then print(" ") else printf("%2d ", M[R,C]) end, |
|||
if C = NC then nl end |
|||
end. /* |
|||
24 15 23 26 |
|||
6 9 12 5 8 11 4 |
|||
14 17 20 25 16 19 22 |
|||
2 7 10 3 27 |
|||
13 18 21 |
|||
1 |
|||
CPU time 0.019 seconds, correct */ |
|||
</lang> |
|||
=={{header|Prolog}}== |
=={{header|Prolog}}== |