Solve a Hidato puzzle: Difference between revisions
→{{header|Julia}}
Line 1,913:
function hidatoconfigure(str)
lines = split(str, "\n")
nrows, ncols = length(lines), length(split
board = fill(-1, (nrows, ncols))
presets = Vector{Int}()
starts = Vector{CartesianIndex{2}}()
for (i, line) in enumerate(lines), (j, s) in enumerate(split(strip(line), r"\s+"))
c = s[1]
if c == '_' || (c == '0' && length(s) == 1)
board[i, j] = 0
elseif c == '.'
continue
Line 1,931:
push!(starts, CartesianIndex(i, j))
end
maxmoves += 1
end
end
board,
end
function hidatosolve(board, maxmoves, movematrix, fixed, row, col, sought, next
if sought >
return true
elseif (0 != board[row, col] != sought) || (board[row, col] == 0 && sought in fixed)
Line 1,951 ⟶ 1,952:
i, j = row + move[1], col + move[2]
if (0 < i <= size(board)[1]) && (0 < j <= size(board)[2]) &&
hidatosolve(board, maxmoves, movematrix, fixed, i, j, sought + 1, next
return true
end
Line 1,965 ⟶ 1,966:
end
end #
</lang><lang julia>using .Hidato▼
▲using .Hidato
hidat = """
Line 1,981:
const kingmoves = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
board,
printboard(board)
hidatosolve(board, maxmoves, kingmoves, fixed, starts[1][1], starts[1][2], 1, 1
printboard(board)
</lang>{{output}}<pre>
|