Skip to main content

Sudoku Solver

HardPremium

Write a function sudokuSolve that checks whether a given sudoku board is solvable. If so, the function returns true. Otherwise (i.e. there is no valid solution to the given sudoku board), it returns false.

Context:

In sudoku, the objective is to fill a 9x9 board with digits so that each column, each row, and each of the nine 3x3 sub-boards that compose the board contains all of the digits from 1 to 9. The board setter provides a partially completed board, which for a well-posed board has a unique solution.

As explained above, for this problem, it suffices to calculate whether a given sudoku board has a solution. No need to return the actual numbers that make up a solution.

A sudoku board is represented as a two-dimensional 9x9 array of the characters '1', '2', ..., '9' and the '.' character, which represents a blank space. The function should fill the blank spaces with characters such that the following rules apply:

  1. In every row of the array, all characters '1', '2', ..., '9' appear exactly once.
  2. In every column of the array, all characters '1', '2', ..., '9' appear exactly once.
  3. In every 3x3 sub-board that is illustrated below, all characters '1', '2', ..., '9' appear exactly once.

A solved sudoku is a board with no blank spaces, i.e. all blank spaces are filled with characters that abide by the constraints above. If the function succeeds in solving the sudoku board, it'll return true (false, otherwise).

Example (more examples can be found here):

A typical Sudoku board

A typical Sudoku board setter

The same board with solution numbers marked in red

The same board with solution numbers marked in red

Be sure to write readable and efficient code, explain how your solution is built and why you chose to build it that way.

Try a brute force solution first and then optimize.

Let's restate the question. At a high level, your function should choose an empty cell and place values inside. If some placed value makes sudokuSolve(board) true, then the answer is true - otherwise, the answer is false.

If your solution is choosing the empty cell naively, consider which choice of empty cell would be best. We want to minimize work later.

Have you figured out how to identify the best choices for empty cells? The best choice is that with the fewest possible answers because in the worst case, we'll have to check sudokuSolve(board) the least number of times. You might consider writing a helper function getCandidates that gets the possible values some cell board[r][c] could be.

If you're still stuck, here's a skeleton to get you started.

function getCandidates(board, row, col): # What values can be placed in empty cell board[row][col] ? function sudokuSolve(board): # For each empty cell (row, col): # If (row, col) has fewer candidate values that can be placed # in board[row][col]than we've seen, remember it # If there's no empty cell: # return true # For candidate values v of our remembered row, col: # board[row][col] = v # if solve(board): # return true # return false