37 Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

  • The Algorithm:

    • For every row and column element, check for the possibility of a solution

    • A solution exist if there is single existing solution

      • We check this by using a hash set; set it up to contain elements 1 through 9. For every matrix element, remove all the elements in the hash set that are in said row, column, and 3x3 square of the matrix element

      • If the hash set contains 1 element, return said element.

      • Otherwise, move on to the next matrix element.

    • Continue looping through the board matrix using the solutions of previous solutions until the entirity of the matrix is filled.

  • Flaws:

    • This algorithm doesnt not work with all sudoku boards! It only works when the algorithm itself eliminates all possible elements on the board with 100% certainty. I need to modify it to handle the cases when there are 2 or 3 elements remaining in the hash set, and pick a route to see if it works. Or something like that.

Via Command Line

Last updated

Was this helpful?