Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Given a 9*9 table, solve for Sudoku. With the back-depth depth-first search algorithm, the focus is on searching from left to right, and jumping to the next line at the end of the line. First give the template pseudo code of DFS
/**
* DFS core pseudo code
* The precondition is that the visit array is all set to false
* @param n node where the current search starts
* @param d the current depth of arrival
* @return Is there a solution?
*/
bool DFS(Node n, int d){
If (isEnd(n, d)){//true if the search depth reaches an end state
return true;
}
For (Node nextNode in n){// traversing n adjacent nodes nextNode
if (!visit[nextNode]){//
Visit[nextNode] = true;//nextNode cannot appear again in the next search
If (DFS(nextNode, d+1)){//If the search has a solution
//Do something else, such as recording the depth of the results, etc.
return true;
}
/ / Reset to false, because it may appear in the other path of the next search
visit[nextNode] = false;
}
}
Return false; / / this search has no solution
}
The code of the question
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
Sudoku(board, 0, 0);
}
bool Sudoku(vector<vector<char>>& board, int i, int j){
if(i==9) return true;
if(j==9) return Sudoku(board, i+1, 0);
if(board[i][j] == '.'){
for(int k=0; k<9; k++){
if(check(board, i, j, k+'1')){
board[i][j] = k+'1';
if(Sudoku(board, i,j+1))
return true;
board[i][j] = '.';
}
}
}
else
return Sudoku(board,i,j+1);
return false;
}
bool check(vector<vector<char>>& board, int i, int j, char ch){
for(int row=0;row<9;row++)
if(board[row][j] == ch)
return false;
for(int col=0;col<9;col++)
if(board[i][col] == ch)
return false;
for(int row=(i/3)*3; row<(i/3)*3+3; row++)
for(int col = (j/3)*3; col < (j/3)*3+3; col++)
if(board[row][col] == ch)
return false;
return true;
}
};
https://leetcode.com/problems/sudoku-solver/discuss/15853/Simple-and-Clean-Solution-C%2B%2B
This question is also a classic backtracking question, very similar to the Eight Queens. note: 1) It is most convenient to use bit operation. 2) One index is enough for the dfs parameter, and then con...
Ideas: The idea is actually very easy to think of, which is to simply and rudely try every possibility. Each grid has 9 possibilities. So here we use backtracking. Traverse the matrix, and insert 1-9 ...
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. ...
Topic It is an advanced version of leetcode36, to solve Sudoku answers Problem solving ideas Recursion was used, but it was too scumbag, and the boundary conditions were wrong, and the idea was not cl...
Problem A sudoku puzzle… …and its solution numbers marked in red. AC...
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. A sudoku puzzle......
Question: Solve 9*9 Sudoku Practice: backtracking For each unfilled grid, try 9 numbers from 1 to 9, if it is legal, continue to fill the next grid, otherwise go back. Judgment is legal: only need to ...
A small program acquires the data on the previous page...
Dynamic Programming CurSum rightmost position of the maximum recording sequence, res record the actual maximum. After a partition to do, first of all about the array is divided into three parts, to fi...
1 problem A collection of a range, find the minimum number of intervals to remove, so that the remaining intervals do not overlap. note: It can be considered that the end point of the interval is alwa...