Solve a Hidato puzzle: Difference between revisions

Content added Content deleted
Line 1,906: Line 1,906:


=={{header|Julia}}==
=={{header|Julia}}==
This solution utilizes a module which is loosely based on the Python version, and is also used for the Hopido task.
This solution utilizes a Hidato puzzle solver module which is also used for the Hopido and knight move tasks.
<lang julia>module Hidato
<lang julia>module Hidato


Line 1,937: Line 1,937:
end
end


function hidatosolve(board, maxmoves, movematrix, fixed, row, col, sought, next)
function hidatosolve(board, maxmoves, movematrix, fixed, row, col, sought)
if sought > maxmoves
if sought > maxmoves
return true
return true
Line 1,943: Line 1,943:
return false
return false
end
end
backnum = 0
backnum = board[row, col] == sought ? sought : 0
if board[row, col] == sought
backnum = sought
next += 1
end
board[row, col] = sought # try board with this cell set to next number
board[row, col] = sought # try board with this cell set to next number
for move in movematrix
for move in movematrix
i, j = row + move[1], col + move[2]
i, j = row + move[1], col + move[2]
if (0 < i <= size(board)[1]) && (0 < j <= size(board)[2]) &&
if (0 < i <= size(board)[1]) && (0 < j <= size(board)[2]) &&
hidatosolve(board, maxmoves, movematrix, fixed, i, j, sought + 1, next)
hidatosolve(board, maxmoves, movematrix, fixed, i, j, sought + 1)
return true
return true
end
end
Line 1,983: Line 1,979:
board, maxmoves, fixed, starts = hidatoconfigure(hidat)
board, maxmoves, fixed, starts = hidatoconfigure(hidat)
printboard(board)
printboard(board)
hidatosolve(board, maxmoves, kingmoves, fixed, starts[1][1], starts[1][2], 1, 1)
hidatosolve(board, maxmoves, kingmoves, fixed, starts[1][1], starts[1][2], 1)
printboard(board)
printboard(board)
</lang>{{output}}<pre>
</lang>{{output}}<pre>