Skip to content

Latest commit

 

History

History
75 lines (52 loc) · 1.77 KB

0037-sudoku-solver.adoc

File metadata and controls

75 lines (52 loc) · 1.77 KB

37. Sudoku Solver

{leetcode}/problems/sudoku-solver/[LeetCode - Sudoku Solver^]

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 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.

{image_attr}

A sudoku puzzle…​

{image_attr}

…​and its solution numbers marked in red.

Note:

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

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

  • The given board size is always 9x9.

解题分析

这道题特别需要关注的点是:只要找到一个解就可以了,不需要求全部解。所以,需要重点思考的是,如何在找到第一个解时,就立即停止搜索。

递归树

{image_attr}
一刷
link:{sourcedir}/_0037_SudokuSolver.java[role=include]
二刷
link:{sourcedir}/_0037_SudokuSolver_2.java[role=include]
二刷(引入步进)
link:{sourcedir}/_0037_SudokuSolver_21.java[role=include]