Anonymous user
Solve a Hidato puzzle: Difference between revisions
m
→{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (added whitespace before the TOC.) |
m (→{{header|REXX}}: changed/added comments and whitespace, changed indentations.) |
||
Line 2,800:
<br>If ''any'' marker is negative, then it's assumed to be a Numbrix puzzle (and the absolute value is used).
<br>Over half of the REXX program deals with validating the input and displaying the puzzle.
<br><br>''Hidato'' and ''Numbrix'' are registered trademarks.▼
<lang rexx>/*REXX pgm solves a Hidato or Numbrix puzzle, displays puzzle & solution*/▼
maxr=0; maxc=0; maxx=0; minr=9e9; minc=9e9; minx=9e9; cells=0; @.=▼
parse arg xxx; PZ='Hidato puzzle' /*get cell definitions from C.L. */▼
xxx=translate(xxx, , "/\;:_", ',') /*also allow other chars as comma*/▼
do while xxx\=''; parse var xxx r c marks ',' xxx▼
▲<lang rexx>/*REXX
do while marks\=''; _=@.r.c▼
parse var marks x marks
if datatype(x,'N') then do; x=x/1 /*normalize X*/
if x<0 then PZ= 'Numbrix puzzle'
x=abs(x) /*use │x│ */
end
if
if x==. then iterate /*hole? Skip.*/▼
if \datatype(x,'W') then call err 'illegal marker specified:' x
end /*while marks¬='' */
end /*while xxx ¬='' */
call
Nr = '0 1 0 -1 -1 1 1 -1' /*possible row for the next move. */
Nc = '1 0 -1 0 1 -1 1 -1' /* "
pMoves=words(Nr) -4*(left(PZ,1)=='N') /*is this to be a Numbrix puzzle ? */
do i=1 for pMoves; Nr.i=word(Nr,i); Nc.i=word(Nc,i); end /*for fast moves. */
if \next(2,!r,!c) then
say 'A solution for the' PZ "exists."; say;
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
err: say; say '***error
/*──────────────────────────────────────────────────────────────────────────────────────*/
next: procedure expose @. Nr. Nc. cells pMoves; parse arg #,r,c; ##=#+1
do t=1 for pMoves /* [↓] try some moves. */
parse value r+Nr.t c+Nc.t with nr nc /*next move coördinates.*/
if @.nr.nc==. then do;
if #==cells
if next(##,nr,nc)
@.nr.nc=. /*undo the above move. */
iterate /*go & try another move.*/
end
if @.nr.nc==# then do /*
if #==cells
if next(##,nr,nc)
end
end /*t*/
return 0 /*
/*──────────────────────────────────────────────────────────────────────────────────────*/
if
▲w=length(cells); do r=maxr to minr by -1; _=
say;
'''output''' when using the following as input:▼
▲'''output''' using the following as input:
<br> <tt> 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 . .</tt>
<pre>
|