Sudoku Solver
Description
https://leetcode.com/problems/sudoku-solver/description/
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
- Each of the digits
1-9
must occur exactly once in each row. - Each of the digits
1-9
must occur exactly once in each column. - Each of the the digits
1-9
must occur exactly once in each of the 93x3
sub-boxes of the grid.
Empty cells are indicated by the character '.'
.
Solution
This is a brutal-force implementation of Sudoku algorithm, The solution ism quite straightforward, we choose each empty cell, try values from 1 to 9, once validated, then we recursively call the solver function, until there is no other empty cell.
Code
1 | class Solution(object): |
Improvement
A little trick to make the backtracing faster is accelerate the validation process, by using hash table in each row, each column, each block.
1 | class Solution(object): |