How to resolve the algorithm Solve a Numbrix puzzle step by step in the zkl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Solve a Numbrix puzzle step by step in the zkl 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 zkl programming language
Source code in the zkl programming language
// Solve Hidato/Hopido/Numbrix puzzles
class Puzzle{ // hold info concerning this puzzle
var board, nrows,ncols, cells,
start, // (r,c) where 1 is located, Void if no 1
terminated, // if board holds highest numbered cell
given, // all the pre-loaded cells
adj, // a list of (r,c) that are valid next cells
;
fcn print_board{
d:=Dictionary(-1," ", 0,"__");
foreach r in (board){
r.pump(String,'wrap(c){ "%2s ".fmt(d.find(c,c)) }).println();
}
}
fcn init(s,adjacent){
adj=adjacent;
lines:=s.split("\n");
ncols,nrows=lines[0].split().len(),lines.len();
board=nrows.pump(List(), ncols.pump(List(),-1).copy);
given,start=List(),Void;
cells,terminated=0,True;
foreach r,row in (lines.enumerate()){
foreach c,cell in (row.split().enumerate()){
if(cell=="X") continue; // X == not in play, leave at -1
cells+=1;
val:=cell.toInt();
board[r][c]=val;
given.append(val);
if(val==1) start=T(r,c);
}
}
println("Number of cells = ",cells);
if(not given.holds(cells)){ given.append(cells); terminated=False; }
given=given.filter().sort();
}
fcn solve{ //-->Bool
if(start) return(_solve(start.xplode()));
foreach r,c in (nrows,ncols){
if(board[r][c]==0 and _solve(r,c)) return(True);
}
False
}
fcn [private] _solve(r,c,n=1, next=0){
if(n>given[-1]) return(True);
if(not ( (0<=r
if(board[r][c] and board[r][c]!=n) return(False);
if(terminated and board[r][c]==0 and given[next]==n) return(False);
back:=0;
if(board[r][c]==n){ next+=1; back=n; }
board[r][c]=n;
foreach i,j in (adj){ if(self.fcn(r+i,c+j,n+1, next)) return(True) }
board[r][c]=back;
False
}
} // Puzzle
hi1:= // 0==empty cell, X==not a cell
#<<<
"0 0 0 0 0 0 0 0 0
0 0 46 45 0 55 74 0 0
0 38 0 0 43 0 0 78 0
0 35 0 0 0 0 0 71 0
0 0 33 0 0 0 59 0 0
0 17 0 0 0 0 0 67 0
0 18 0 0 11 0 0 64 0
0 0 24 21 0 1 2 0 0
0 0 0 0 0 0 0 0 0";
#<<<
hi2:= // 0==empty cell, X==not a cell
#<<<
"0 0 0 0 0 0 0 0 0
0 11 12 15 18 21 62 61 0
0 6 0 0 0 0 0 60 0
0 33 0 0 0 0 0 57 0
0 32 0 0 0 0 0 56 0
0 37 0 1 0 0 0 73 0
0 38 0 0 0 0 0 72 0
0 43 44 47 48 51 76 77 0
0 0 0 0 0 0 0 0 0";
#<<<
adjacent:=T( T(-1,0),
T( 0,-1), T( 0,1),
T( 1,0) );
foreach hi in (T(hi1,hi2)){
puzzle:=Puzzle(hi); puzzle.adjacent=adjacent;
puzzle.print_board();
puzzle.solve();
println();
puzzle.print_board();
println();
}
You may also check:How to resolve the algorithm Permutations/Derangements step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the X86 Assembly programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Nim programming language
You may also check:How to resolve the algorithm Simple windowed application step by step in the Web 68 programming language
You may also check:How to resolve the algorithm Create an HTML table step by step in the Snobol4 programming language