Formatted question description: https://leetcode.ca/all/37.html

# 37. Sudoku Solver

Hard

## Description

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

A sudoku solution must satisfy all of the following rules:

1. Each of the digits 1-9 must 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-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.

A sudoku puzzle…

…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.

# Algorithm

Similar to this question, there are Permutations, Combinations, N-Queens, etc., especially the N-Queens’s problem-solving idea is very similar.

For each grid that needs to be filled with numbers, enter 1 to 9, and every time a number is substituted Determine whether it is legal. If it is legal, continue to the next recursion, and set the number back to’.’ at the end. When judging whether the newly added number is legal, you only need to determine whether the current number is legal, not whether the array is a Sudoku array. Because the numbers added before are all legal, this can make the program more efficient.

The overall idea is this, but it can be implemented in different forms. One form of implementation is to recursively bring the horizontal and vertical coordinates. Since it is filled with numbers line by line, and starts from line 0, when i reaches 9, it means that all the numbers have been successfully filled in and return to true directly. When j is greater than or equal to 9, the current line is filled, and you need to switch to the next line to continue filling, then continue to call the recursive function, and the abscissa will bring i+1. Otherwise, if the current number is not a point, it means that the current position does not need to fill in a number, so call recursion on the right position.

If you need to fill in a number at the current position, you should try to fill in all the numbers from 1 to 9 and let c traverse from 1 to 9. Whenever you try to fill in a number, you need to check whether there is a conflict, and use another sub-function isValid To check whether it is legal, if not legal, skip the current number.

• If it is valid, assign the current position to this number, and call recursion on the right position. If the recursive function returns true, it means that it can be filled successfully and returns true directly.
• If it doesn’t work, you need to reset the state to restore the current position to a point. If all numbers are tried, but still fails, it will eventually return false.

The principle of checking whether the current array is valid is very similar to the previous Valid Sudoku, but it is simpler, because here we only need to check whether the newly added number will conflict with other positions, and detect the ranks and positions of the newly added numbers. Whether there are repeated numbers in the interval.

Java