Sudoku

Sudoku

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

A sudoku solution must satisfyall of the following rules:

  1. Each of the digits 1-9must occur exactly once in each row.

  2. Each of the digits 1-9 must occur exactly once in each column.

  3. Each of the the digits 1-9must occur exactly once in each of the 93x3sub-boxes of the grid.

Empty cells are indicated by the character'.'.

Example

Example 1:

Input:

[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]

Output:
 true

Example 2:

A sudoku puzzle...

...and its solution numbers marked in red.

Note:

  • The given board contain only digits1-9and the character'.'.

  • You may assume that the given Sudoku puzzle will have a single unique solution.

  • The given board size is always9x9

Note

Judge is Valid - set version: Time O(9*9) space O(3*9)

Judge is Valid - trick version 不推荐:

Solver:

暴力去添加1到9,如果合适(valid)就继续dfs,search函数是boolean,不行就改回去,都不行这一层就是false

Code

Last updated