I tried to implement backtracking solution for a sudoku solver in swift. This is my code:
func solve(board: [[Int]]) -> (isSolved: Bool, board: [[Int]]){
var board = board
var empty_pos: (Int, Int)
var check_empty = findEmpty(board: board)
if check_empty.isEmpty == false{
return (true, board)
}else{
empty_pos = check_empty.pos
for num in 1..<10{
if isValid(board: board, num: num, pos: empty_pos){
board[empty_pos.0][empty_pos.1] = num
if solve(board: board).isSolved{
return (true, board)
}else{
board[empty_pos.0][empty_pos.1] = 0
}
}
}
}
return (false, board)}
When I run the code, the function returns true with the original board. However, when I print the board in the if solved block, I noticed that function solves the board, but it doesn't return it, and continues to call the function until it makes all of the 0 values 0 again. I think the function doesn't quit in the if solve(board: board).isSolved part. What should I do to solve this? Thank you!
SudokuBoardstruct to abstract over the[[Int]]. Then you could just make asolvemethod on it that returnsSudokuBoard?, eithernil(no solution), or a solved board. - Alexander