How to resolve the algorithm Solve a Numbrix puzzle step by step in the REXX programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Solve a Numbrix puzzle step by step in the REXX programming language
Table of Contents
Problem Statement
Numbrix puzzles are similar to Hidato. The most important difference is that it is only possible to move 1 node left, right, up, or down (sometimes referred to as the Von Neumann neighborhood). Published puzzles also tend not to have holes in the grid and may not always indicate the end node. Two examples follow: Problem. Solution. Problem. Solution. Write a program to solve puzzles of this ilk, demonstrating your program by solving the above examples. Extra credit for other interesting examples.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Solve a Numbrix puzzle step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program solves a Numbrix (R) puzzle, it also displays the puzzle and solution. */
maxR= 0; maxC= 0; maxX= 0; /*define maxR, maxC, and maxX. */
minR= 9e9; minC= 9e9; minX= 9e9; /* " minR, minC, " minX. */
cells= 0 /*the number of cells (so far). */
parse arg xxx /*get the cell definitions from the CL.*/
xxx=translate(xxx, ',,,,,' , "/\;:_") /*also allow other characters as comma.*/
@.=
do while xxx\=''; parse var xxx r c marks ',' xxx
do while marks\=''; _=@.r.c
parse var marks x marks
if datatype(x, 'N') then x= abs(x) / 1 /*normalize │x│ */
minR= min(minR, r); minC= min(minC, c) /*find min R and C*/
maxR= max(maxR, r); maxC= max(maxC, c) /* " max " " "*/
if x==1 then do; !r= r; !c= c /*the START cell. */
end
if _\=='' then call err "cell at" r c 'is already occupied with:' _
@.r.c= x; c= c +1; cells= cells + 1 /*assign a mark. */
if x==. then iterate /*is a hole? Skip*/
if \datatype(x,'W') then call err 'illegal marker specified:' x
minX= min(minX, x); maxX= max(maxX, x) /*min & max X.*/
end /*while marks\='' */
end /*while xxx \='' */
call show /* [↓] is used for making fast moves. */
Nr = '0 1 0 -1 -1 1 1 -1' /*possible row for the next move. */
Nc = '1 0 -1 0 1 -1 1 -1' /* " column " " " " */
pMoves= words(Nr) - 4 /*is this to be a Numbrix puzzle ? */
do i=1 for pMoves; Nr.i= word(Nr, i); Nc.i= word(Nc, i) /*for fast moves. */
end /*i*/
say
if \next(2, !r, !c) then call err 'No solution possible for this Numbrix puzzle.'
say 'A solution for the Numbrix puzzle exists.'; say; call show
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
err: say; say '***error*** (from Numbrix puzzle): ' arg(1); say; exit 13
/*──────────────────────────────────────────────────────────────────────────────────────*/
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; @.nr.nc= # /*let's try this move. */
if #==cells then return 1 /*is this the last move?*/
if next(##, nr, nc) then return 1
@.nr.nc=. /*undo the above move. */
iterate /*go & try another move.*/
end
if @.nr.nc==# then do /*this a fill─in move ? */
if #==cells then return 1 /*this is the last move.*/
if next(##, nr, nc) then return 1 /*a fill─in move. */
end
end /*t*/
return 0 /*this ain't working. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: 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 puzzle start'
w= max(2, length(cells) ); do r=maxR to minR by -1; _=
do c=minC to maxC; _= _ right(@.r.c, w)
end /*c*/
say _
end /*r*/
say; return
You may also check:How to resolve the algorithm Monty Hall problem step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the D programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Objeck programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the V (Vlang) programming language