How to resolve the algorithm Solve a Numbrix puzzle step by step in the AutoHotkey programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Solve a Numbrix puzzle step by step in the AutoHotkey 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 AutoHotkey programming language

Source code in the autohotkey programming language

SolveNumbrix(Grid, Locked, Max, row, col, num:=1, R:="", C:=""){
	if (R&&C)							; if neighbors (not first iteration)
	{
		Grid[R, C] := ">" num 					; place num in current neighbor and mark it visited ">"
		row:=R, col:=C						; move to current neighbor
	}
 
	num++								; increment num
	if (num=max)							; if reached end
		return map(Grid)					; return solution
 
	if locked[num]							; if current num is a locked value
	{
		row := StrSplit((StrSplit(locked[num], ",").1) , ":").1	; find row of num
		col := StrSplit((StrSplit(locked[num], ",").1) , ":").2	; find col of num
		if SolveNumbrix(Grid, Locked, Max, row, col, num)	; solve for current location and value
			return map(Grid)				; if solved, return solution
	}
	else
	{
		for each, value in StrSplit(Neighbor(row,col), ",")
		{
			R := StrSplit(value, ":").1
			C := StrSplit(value, ":").2
 
			if (Grid[R,C] = "")				; a hole or out of bounds
			|| InStr(Grid[R, C], ">")			; visited
			|| Locked[num+1] && !(Locked[num+1]~= "\b" R ":" C "\b") ; not neighbor of locked[num+1]
			|| Locked[num-1] && !(Locked[num-1]~= "\b" R ":" C "\b") ; not neighbor of locked[num-1]
			|| Locked[num]					; locked value
			|| Locked[Grid[R, C]]				; locked cell
				continue
 
			if SolveNumbrix(Grid, Locked, Max, row, col, num, R, C)	; solve for current location, neighbor and value
				return map(Grid)			; if solved, return solution
		}
	}
	num--								; step back
	for i, line in Grid
		for j, element in line
			if InStr(element, ">") && (StrReplace(element, ">") >= num)
				Grid[i, j] := 0
}
;--------------------------------
;--------------------------------
;--------------------------------
Neighbor(row,col){
	return row-1 ":" col
	. "," row+1 ":" col
	. "," row ":" col+1
	. "," row ":" col-1
}
;--------------------------------
map(Grid){
	for i, row in Grid
	{
		for j, element in row
			line .= (A_Index > 1 ? "`t" : "") . element 
		map .= (map<>""?"`n":"") line
		line := ""
	}
	return StrReplace(map, ">")
}


;--------------------------------
Grid := [[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]]
;--------------------------------
; find locked cells, find row and col of first value "1" and max value 
Locked := []
max := 1
for i, line in Grid
	for j, element in line
	{
		max ++
		if element = 1
			row :=i , col := j
		if (element > 0)
			Locked[element] := i ":" j "," Neighbor(i, j)	; save locked elements position and neighbors
			
	}
;--------------------------------
MsgBox, 262144, ,% SolveNumbrix(Grid, Locked, Max, row, col)
return


  

You may also check:How to resolve the algorithm A+B step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Generate Chess960 starting position step by step in the Phix programming language
You may also check:How to resolve the algorithm Factorions step by step in the C++ programming language
You may also check:How to resolve the algorithm Jacobi symbol step by step in the Haskell programming language
You may also check:How to resolve the algorithm Sorting algorithms/Merge sort step by step in the Liberty BASIC programming language