Hone logo
Hone
Problems

Sudoku Solver: A Constraint Satisfaction Problem

Constraint satisfaction problems (CSPs) are a powerful tool for solving problems with a set of variables, domains, and constraints. This challenge asks you to implement a Sudoku solver using a constraint satisfaction approach in JavaScript. Solving Sudoku puzzles is a classic example of a CSP, and this exercise will help you understand the core concepts of constraint propagation and search.

Problem Description

You are tasked with creating a Sudoku solver that takes a partially filled Sudoku grid as input and attempts to solve it. The grid will be represented as a 2D array (9x9) where empty cells are represented by 0. Your solver should use constraint satisfaction techniques to find a valid solution, filling in the empty cells with digits from 1 to 9.

What needs to be achieved:

  • Implement a function solveSudoku(board) that takes a 2D array representing the Sudoku board as input.
  • The function should modify the input board in-place to represent the solved Sudoku puzzle.
  • If the Sudoku puzzle is solvable, the function should return true.
  • If the Sudoku puzzle is unsolvable, the function should return false.

Key Requirements:

  • Constraint Propagation: Implement constraint propagation techniques (e.g., forward checking, arc consistency) to reduce the search space. While a full arc consistency implementation isn't strictly required, some form of constraint propagation is expected to improve efficiency.
  • Search Strategy: Employ a search strategy (e.g., backtracking search) to explore possible assignments of digits to empty cells.
  • Validity Checks: Ensure that each assignment of a digit to a cell satisfies the Sudoku constraints:
    • Each row must contain the digits 1-9 without repetition.
    • Each column must contain the digits 1-9 without repetition.
    • Each of the nine 3x3 subgrids (boxes) must contain the digits 1-9 without repetition.

Expected Behavior:

The solveSudoku function should modify the input board directly. After the function completes, the board should contain the solved Sudoku puzzle if a solution exists. If no solution exists, the board should remain unchanged (or reflect the state it was in before the function call).

Edge Cases to Consider:

  • Invalid Input: The input board might not be a valid 9x9 grid. While you don't need to explicitly validate the input format, your solver should handle cases where the input is malformed gracefully (e.g., by returning false).
  • Unsolvable Puzzles: Some Sudoku puzzles are designed to be unsolvable. Your solver should correctly identify these cases and return false.
  • Already Solved Puzzles: The input board might already be a solved Sudoku puzzle. Your solver should recognize this and return true without making any changes.
  • Partially Filled Puzzles: The input board will typically be partially filled, with some cells already containing digits.

Examples

Example 1:

Input:
[
  [5,3,0,0,7,0,0,0,0],
  [6,0,0,1,9,5,0,0,0],
  [0,9,8,0,0,0,0,6,0],
  [8,0,0,0,6,0,0,0,3],
  [4,0,0,8,0,3,0,0,1],
  [7,0,0,0,2,0,0,0,6],
  [0,6,0,0,0,0,2,8,0],
  [0,0,0,4,1,9,0,0,5],
  [0,0,0,0,8,0,0,7,9]
]
Output:
[
  [5,3,4,6,7,8,9,1,2],
  [6,7,2,1,9,5,3,4,8],
  [1,9,8,3,4,2,5,6,7],
  [8,5,9,7,6,1,4,2,3],
  [4,2,6,8,5,3,7,9,1],
  [7,1,3,9,2,4,8,5,6],
  [9,6,1,5,3,7,2,8,4],
  [2,8,7,4,1,9,6,3,5],
  [3,4,5,2,8,6,1,7,9]
]
Explanation: The input board is a partially filled Sudoku puzzle. The solver finds a valid solution by filling in the empty cells according to the Sudoku rules.

Example 2:

Input:
[
  [1,2,3,4,5,6,7,8,9],
  [4,5,6,7,8,9,1,2,3],
  [7,8,9,1,2,3,4,5,6],
  [2,3,4,5,6,7,8,9,1],
  [5,6,7,8,9,1,2,3,4],
  [8,9,1,2,3,4,5,6,7],
  [3,4,5,6,7,8,9,1,2],
  [6,7,8,9,1,2,3,4,5],
  [9,1,2,3,4,5,6,7,8]
]
Output:
[
  [1,2,3,4,5,6,7,8,9],
  [4,5,6,7,8,9,1,2,3],
  [7,8,9,1,2,3,4,5,6],
  [2,3,4,5,6,7,8,9,1],
  [5,6,7,8,9,1,2,3,4],
  [8,9,1,2,3,4,5,6,7],
  [3,4,5,6,7,8,9,1,2],
  [6,7,8,9,1,2,3,4,5],
  [9,1,2,3,4,5,6,7,8]
]
Explanation: The input board is already a solved Sudoku puzzle. The solver recognizes this and returns `true` without making any changes.

Constraints

  • The input board will be a 9x9 2D array of integers.
  • Each cell in the board will contain either a digit from 1 to 9 or 0 (representing an empty cell).
  • The solver must modify the input board in-place.
  • The solver should be reasonably efficient. While a brute-force approach might work for small puzzles, it will likely time out for more complex ones. Aim for a solution that utilizes constraint propagation and a smart search strategy.

Notes

  • Consider using recursion to implement the backtracking search.
  • Implement a function to check if a given assignment is valid (i.e., it doesn't violate any Sudoku constraints).
  • Think about how to efficiently find the next empty cell to assign a value to.
  • Constraint propagation can significantly reduce the search space. Consider techniques like forward checking or maintaining a list of possible values for each empty cell.
  • The order in which you try assigning values to empty cells can impact performance. Experiment with different heuristics (e.g., choosing the cell with the fewest possible values).
Loading editor...
javascript