Solve a Hopido puzzle: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(Added Wren) |
m (→{{header|Wren}}: Minor tidy) |
||
(8 intermediate revisions by 6 users not shown) | |||
Line 28:
* [[Solve the no connection puzzle]]
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V neighbours = [[2, 2], [-2, 2], [2, -2], [-2, -2], [3, 0], [0, 3], [-3, 0], [0, -3]]
V cnt = 0
V pWid = 0
V pHei = 0
F is_valid(a, b)
R -1 < a & a < :pWid & -1 < b & b < :pHei
F iterate(&pa, x, y, v)
I v > :cnt
R 1
L(i) 0 .< :neighbours.len
V a = x + :neighbours[i][0]
V b = y + :neighbours[i][1]
I is_valid(a, b) & pa[a][b] == 0
pa[a][b] = v
V r = iterate(&pa, a, b, v + 1)
I r == 1
R r
pa[a][b] = 0
R 0
F solve(pz, w, h)
V pa = [[-1] * h] * w
V f = 0
:pWid = w
:pHei = h
L(j) 0 .< h
L(i) 0 .< w
I pz[f] == ‘1’
pa[i][j] = 0
:cnt++
f++
L(y) 0 .< h
L(x) 0 .< w
I pa[x][y] == 0
pa[x][y] = 1
I 1 == iterate(&pa, x, y, 2)
R (1, pa)
pa[x][y] = 0
R (0, pa)
V r = solve(‘011011011111111111111011111000111000001000’, 7, 6)
I r[0] == 1
L(j) 6
L(i) 7
I r[1][i][j] == -1
print(‘ ’, end' ‘’)
E
print(‘ #02’.format(r[1][i][j]), end' ‘’)
print()
E
print(‘No solution!’, end' ‘’)</syntaxhighlight>
{{out}}
<pre>
01 25 17 03
27 13 10 07 14 11 08
24 21 18 02 22 19 16
06 26 12 09 04
23 20 15
05
</pre>
=={{header|AutoHotkey}}==
<
if (R&&C) ; if neighbors (not first iteration)
{
Line 99 ⟶ 168:
}
return StrReplace(map, ">")
}</
Examples:<
Grid := [["",0 ,0 ,"",0 ,0 ,""]
,[0 ,0 ,0 ,0 ,0 ,0 ,0]
Line 128 ⟶ 197:
;--------------------------------
MsgBox, 262144, ,% SolveHopido(Grid, Locked, Max, row, col)
return</
Outputs:<pre> 17 24 16 25
22 8 11 21 7 10 20
Line 141 ⟶ 210:
Any non-numeric value indicates a no-go.<br/>
If there are cells that require more characters, then a 2-dimensional array of ints must be used. Any number < 0 indicates a no-go.<br/>
<
using System.Collections.Generic;
using static System.Console;
Line 266 ⟶ 335:
}
}</
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 278 ⟶ 347:
=={{header|C++}}==
<
#include <vector>
#include <sstream>
Line 412 ⟶ 481:
return system( "pause" );
}
</syntaxhighlight>
{{out}}
<pre>
Line 426 ⟶ 495:
{{trans|C++}}
From the refactored C++ version with more precise typing. This tries all possible start positions. The HopidoPuzzle struct is created at compile-time, so its pre-conditions can catch most malformed puzzles at compile-time.
<
Line 539 ⟶ 608:
else
writefln("One solution:\n%(%-(%2s %)\n%)", solution);
}</
{{out}}
<pre>One solution:
Line 552 ⟶ 621:
{{trans|Ruby}}
This solution uses HLPsolver from [[Solve_a_Hidato_puzzle#Elixir | here]]
<
adjacent = [{-3, 0}, {0, -3}, {0, 3}, {3, 0}, {-2, -2}, {-2, 2}, {2, -2}, {2, 2}]
Line 564 ⟶ 633:
. . . 1 . . .
"""
HLPsolver.solve(board, adjacent)</
{{out}}
Line 587 ⟶ 656:
=={{header|Go}}==
{{trans|Java}}
<
import (
Line 709 ⟶ 778:
}
printResult()
}</
{{out}}
Line 725 ⟶ 794:
Minor variant of [[Solve_a_Holy_Knight's_tour]]. Works in Unicon only.
<
record Pos(r,c)
Line 822 ⟶ 891:
QMouse(puzzle, visit(loc.r, loc.c+3), self, val)
QMouse(puzzle, visit(loc.r-2,loc.c+2), self, val)
end</
Sample run:
Line 854 ⟶ 923:
=={{header|Java}}==
{{works with|Java|8}}
<
public class Hopido {
Line 962 ⟶ 1,031:
}
}
}</
<pre>
Line 974 ⟶ 1,043:
=={{header|Julia}}==
Uses the Hidato puzzle solver module, which has its source code listed [[Solve_a_Hidato_puzzle#Julia | here]] in the Hadato task.
<
const hopid = """
Line 990 ⟶ 1,059:
hidatosolve(board, maxmoves, hopidomoves, fixed, starts[1][1], starts[1][2], 1)
printboard(board)
</
0 0 0 0
0 0 0 0 0 0 0
Line 1,008 ⟶ 1,077:
=={{header|Kotlin}}==
{{trans|Java}}
<
val board = listOf(
Line 1,103 ⟶ 1,172:
printResult()
}</
{{out}}
Line 1,114 ⟶ 1,183:
3
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Uses shortest tours on graphs to solve it:
<syntaxhighlight lang="mathematica">puzz = ".00.00.\n0000000\n0000000\n.00000.\n..000..\n...0...";
puzz //= StringSplit[#, "\n"] & /* Map[Characters];
puzz //= Transpose /* Map[Reverse];
pos = Position[puzz, "0", {2}];
moves = Select[Select[Tuples[pos, 2], MatchQ[EuclideanDistance @@ #, 2 Sqrt[2] | 3] &], OrderedQ];
g = Graph[UndirectedEdge @@@ moves];
ord = Most[FindShortestTour[g][[2]]];
Graphics[MapThread[Text, {Range[Length[ord]], VertexList[g][[ord]]}]]</syntaxhighlight>
{{out}}
Shows a graphical solution.
=={{header|Nim}}==
{{trans|Go}}
<syntaxhighlight lang="nim">import algorithm, sequtils, strformat
const Moves = [(-3, 0), (0, 3), ( 3, 0), ( 0, -3),
( 2, 2), (2, -2), (-2, 2), (-2, -2)]
type
Hopido = object
grid: seq[seq[int]]
nRows, nCols: int
totalToFill : Natural
Neighbor = (int, int, int)
proc initHopido(board: openArray[string]): Hopido =
result.nRows = board.len + 6
result.nCols = board[0].len + 6
result.grid = newSeqWith(result.nRows, repeat(-1, result.nCols))
for row in 0..board.high:
for col in 0..board[0].high:
if board[row][col] == '0':
result.grid[row + 3][col + 3] = 0
inc result.totalToFill
proc countNeighbors(hopido: Hopido; row, col: Natural): int =
for (x, y) in Moves:
if hopido.grid[row + y][col + x] == 0:
inc result
proc neighbors(hopido: Hopido; row, col: Natural): seq[Neighbor] =
for (x, y) in Moves:
if hopido.grid[row + y][col + x] == 0:
let num = hopido.countNeighbors(row + y, col + x) - 1
result.add (row + y, col + x, num)
proc solve(hopido: var Hopido; row, col, count: Natural): bool =
if count > hopido.totalTofill: return true
var nbrs = hopido.neighbors(row, col)
if nbrs.len == 0 and count != hopido.totalToFill:
return false
nbrs.sort(proc(a, b: Neighbor): int = cmp(a[2], b[2]))
for (row, col, _) in nbrs:
hopido.grid[row][col] = count
if hopido.solve(row, col, count + 1):
return true
hopido.grid[row][col] = 0
proc findSolution(hopido: var Hopido) =
var pos = -1
var row, col: Natural
while true:
while true:
inc pos
row = pos div hopido.nCols
col = pos mod hopido.nCols
if hopido.grid[row][col] != -1:
break
hopido.grid[row][col] = 1
if hopido.solve(row, col, 2):
break
hopido.grid[row][col] = 0
if pos >= hopido.nRows * hopido.nCols:
break
proc print(hopido: Hopido) =
for row in hopido.grid:
for val in row:
stdout.write if val == -1: " " else: &"{val:2} "
echo()
when isMainModule:
const Board = [".00.00.",
"0000000",
"0000000",
".00000.",
"..000..",
"...0..."]
var hopido = initHopido(Board)
hopido.findSolution()
hopido.print()</syntaxhighlight>
{{out}}
<pre>
1 22 14 21
18 10 7 17 11 8 16
5 24 27 4 23 26 13
2 19 9 15 20
6 25 12
3
</pre>
=={{header|Perl}}==
<
use strict; # http://www.rosettacode.org/wiki/Solve_a_Hopido_puzzle
Line 1,153 ⟶ 1,347:
. 0 0 0 0 0 .
. . 0 0 0 . .
. . . 0 . . .</
{{out}}
<pre>
Line 1,168 ⟶ 1,362:
=={{header|Phix}}==
Simple brute force approach.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">board</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tries</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">ROW</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">COL</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">moves</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">col</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">nrow</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ncol</span>
<span style="color: #000000;">tries</span><span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">move</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">moves</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">nrow</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">+</span><span style="color: #000000;">moves</span><span style="color: #0000FF;">[</span><span style="color: #000000;">move</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ROW</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">ncol</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">col</span><span style="color: #0000FF;">+</span><span style="color: #000000;">moves</span><span style="color: #0000FF;">[</span><span style="color: #000000;">move</span><span style="color: #0000FF;">][</span><span style="color: #000000;">COL</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">3</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">nrow</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">nrow</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">ncol</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ncol</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">row</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">nrow</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ncol</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">' '</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">nrow</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ncol</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">ncol</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nrow</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ncol</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">nrow</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ncol</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">ncol</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">" "</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">Hopido</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ry</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">board</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">h</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">*</span><span style="color: #000000;">3</span> <span style="color: #008080;">by</span> <span style="color: #000000;">3</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">][</span><span style="color: #000000;">y</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">][</span><span style="color: #000000;">y</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">' '</span>
<span style="color: #000000;">limit</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">rx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">ry</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">3</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ry</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">' '</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">board</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ry</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'1'</span>
<span style="color: #000000;">tries</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">solve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ry</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nsolution found in %d tries (%3.2fs)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">tries</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">else</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"no solutions found\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">board1</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
.
. . 0 0 0 . .
. . . 0 . . ."""</span>
<span style="color: #000000;">Hopido</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
The best and worse cases observed were:
<pre>
Line 1,246 ⟶ 1,443:
solution found in 67702 tries (0.09s)
</pre>
=={{header|Picat}}==
<syntaxhighlight 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.
</syntaxhighlight>
Output:
<pre> 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</pre>
=={{header|Prolog}}==
This is a pure prolog implementation (no cuts,etc..), the only libary predicate used is select/3 witch is pure.
<
select(C,Grid,RGrid),
solve(RGrid,C,Solved,Xs,Ys).
Line 1,273 ⟶ 1,509:
j3(O,N,[O,_,_,N|_]).
j3(O,N,[_|T]) :- j3(O,N,T).</
To test send in a list of p/2 terms that represent points that can be hopped to (order is not important).
The grid coords can be anything so need to specify the valid coordinates as a list (in this case numbers between 0 and 6).
<
p(1,0),p(2,0) ,p(4,0),p(5,0),
p(0,1),p(1,1),p(2,1),p(3,1),p(4,1),p(5,1),p(6,1),
Line 1,290 ⟶ 1,526:
XYs = [0,1,2,3,4,5,6],
hopido(P,S,XYs,XYs),
maplist(writeln,S).</
{{out}}
<pre>
Line 1,326 ⟶ 1,562:
=={{header|Python}}==
<
from sys import stdout
Line 1,390 ⟶ 1,626:
else:
stdout.write("No solution!")
</
01 25 17 03
27 13 10 07 14 11 08
Line 1,405 ⟶ 1,641:
essentially the neighbourhood function.
<
(require "hidato-family-solver.rkt")
Line 1,422 ⟶ 1,658:
#(_ _ 0 0 0 _ _)
#(_ _ _ 0 _ _ _)))))
</syntaxhighlight>
{{out}}
Line 1,443 ⟶ 1,679:
* [[Solve the no connection puzzle#Raku|Solve the no connection puzzle]]
<syntaxhighlight lang="raku"
[2, -2], [2, 2],
[0, -3], [0, 3],
Line 1,540 ⟶ 1,776:
say "$tries tries";
}</
{{out}}
Line 1,555 ⟶ 1,791:
No particular effort was made to reduce the elapsed time in solving the puzzle.
<
call time 'Reset' /*reset the REXX elapsed timer to zero.*/
maxR=0; maxC=0; maxX=0; minR=9e9; minC=9e9; minX=9e9; cells=0; @.=
Line 1,610 ⟶ 1,846:
say _
end /*r*/
say; return</
'''output''' when the input is: <br>
<tt> 1 4 1 \2 3 . . . \3 2 . . . . . \4 1 . . . . . . . \5 1 . . . . . . . \6 2 . . \6 5 . . </tt>
Line 1,635 ⟶ 1,871:
=={{header|Ruby}}==
This solution uses HLPsolver from [[Solve_a_Hidato_puzzle#With_Warnsdorff | here]]
<
ADJACENT = [[-3, 0], [0, -3], [0, 3], [3, 0], [-2, -2], [-2, 2], [2, -2], [2, 2]]
Line 1,649 ⟶ 1,885:
t0 = Time.now
HLPsolver.new(board1).solve
puts " #{Time.now - t0} sec"</
Which produces:
<pre>
Line 1,673 ⟶ 1,909:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<
oo::class create HopidoSolver {
Line 1,768 ⟶ 2,004:
HopidoSolver create hop $puzzle
hop solve
showPuzzle [hop solution] "Output"</
{{out}}
<pre>
Line 1,791 ⟶ 2,027:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var board = [
Line 1,889 ⟶ 2,125:
if (pos >= nRows * nCols) break
}
printResult.call()</
{{out}}
Line 1,903 ⟶ 2,139:
=={{header|zkl}}==
This solution uses the code from [[Solve_a_Numbrix_puzzle#zkl]]
<
#<<<
" X 0 0 X 0 0 X
Line 1,923 ⟶ 2,159:
println();
puzzle.print_board();
println();</
{{out}}
<pre>
|