Solve a Hidato puzzle: Difference between revisions

→‎{{header|REXX}}: added the REXX language. -- ~~~~
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 2,115:
#[#f #f #f #f 17 7 6 3]
#[#f #f #f #f #f #f 5 4]])
</pre>
 
=={{header|REXX}}==
Programming note: &nbsp; the coördinates for the cells used are the same as an &nbsp; X Y &nbsp; grid, that is,
<br>the bottom left-most cell is &nbsp; 1 1 &nbsp; and the tenth cell on row 2 would be &nbsp; 2 10
<lang rexx>/*REXX program solves a Hildato puzzle, displays the puzzle and solution*/
maxr=0; maxc=0; maxx=0; minr=9e9; minc=9e9; minx=9e9; cells=0; @.=
parse arg xxx /*get cell definitions from C.L. */
do while strip(xxx)\==''
parse var xxx placeMarkers ',' xxx
parse var placeMarkers r c marks
do while strip(marks)\==''
parse var marks x marks
minr=min(minr,r); maxr=max(maxr,r)
minc=min(minc,c); maxc=max(maxc,c)
if x==1 then do; startr=r; startc=c; end
@.r.c=x; c=c+1; cells=cells+1
if \datatype(x,'W') then iterate
minx=min(minx,x); maxx=max(maxx,x)
end /*while strip(marks)¬==''*/
end /*while strip(xxx) ¬==''*/
call showGrid
Nr = '0 0 1 1 1 -1 -1 -1' /*possible row for the next move.*/
Nc = '1 -1 0 -1 1 -1 0 1' /* " col " " " " */
pMoves=words(Nr); do i=1 for pMoves; Nr.i=word(Nr,i); Nc.i=word(Nc,i); end
NoNo= 'No solution possible' /* [↑] pre-compute possible moves*/
if cells\==1 then if \next(2,startr,startc) then do; say NoNo; exit; end
say; say 'A solution for the Hildato puzzle exists.'; say; call showGrid
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────ERR subroutine──────────────────────*/
err: say; say '***error!***: ' arg(1); say; exit 13
/*──────────────────────────────────NEXT subroutine─────────────────────*/
next: procedure expose @. Nr. Nc. cells pMoves; parse arg #,r,c; ##=#+1
do t=1 for pMoves; nr=r+Nr.t; nc=c+Nc.t /*next. */
if @.nr.nc==. then do; @.nr.nc=# /*a move.*/
if #==cells then leave /*last 1?*/
if next(##,nr,nc) then return 1
@.nr.nc=. /*undo the above move. */
end
if @.nr.nc==# then do /*is this a fill-in ? */
if #==cells then return 1 /*last 1.*/
if next(##,nr,nc) then return 1 /*fill-in*/
end
end /*t*/
return 0 /*This ain't working. */
/*──────────────────────────────────SHOWGRID subroutine─────────────────*/
showGrid: if maxr<1 | maxc<1 then call err 'no legal cell was specified.'
if minx<1 then call err 'no 1 was specified for the start of the puzzle.'
if maxx\==cells then call err 'no' cells "was specified for the end of the puzzle."
w=length(maxx); do r=maxr to minr by -1; _=
do c=minc to maxc; _=_ right(@.r.c,w); end /*c*/
say _
end /*r*/
return</lang>
'''output''' using the following as input: 1 7 5 .,2 5 . 7 . .,3 3 . . 18 . .,4 1 27 . . . 9 . 1,5 1 . 26 . 13 40 11,6 1 . . . 21 . .,7 1 . . 24 22 .,8 1 . 33 35 . .
<pre>
. 33 35 . .
. . 24 22 .
. . . 21 . .
. 26 . 13 40 11
27 . . . 9 . 1
. . 18 . .
. 7 . .
5 .
 
A solution for the hildato puzzle exists.
 
32 33 35 36 37
31 34 24 22 38
30 25 23 21 12 39
29 26 20 13 40 11
27 28 14 19 9 10 1
15 16 18 8 2
17 7 6 3
5 4
</pre>