LeetCode 37 The LeetCode Road of Solving Sudoku Heroding

Write a program to solve the Sudoku problem through the filled spaces.

A Sudoku solution must follow the following rules:

数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

Blank cells are represented by'.'.

Insert picture description here

The answer is marked in red.

Note:

给定的数独序列只包含数字 1-9 和字符 '.' 。
你可以假设给定的数独只有唯一解。
给定数独永远是 9x9 形式的。

Problem-solving ideas:
First define three data structures, rows and cols are two-dimensional arrays, grid is three-dimensional arrays, two-dimensional array rows, cols indicates whether there is j in the ith row, and grid indicates the number of rows Whether there is a certain number in the Jiugong grid of several columns, the initialization is false, a vector<pair<int,int>> is used to store the position of the space, and then the initial Sudoku is input into the row and column Jiugong grid, the dfs code idea is also very clear, Find an empty position, cycle 1-9, determine whether there is a certain number in the row and column of the position, directly put it on the space, then update the three arrays, and then search, the code is as follows:

class Solution {
    
    
private:
    bool rows[9][9];//rows[i][j] 表示第 i 行是否存在 j + 1
    bool cols[9][9];//cols[i][j] 表示第 i 列是否存在 j + 1
    bool grid[3][3][9];//grid[i][j][k] 表示第 {i, j} 个九宫格是否存在 k
    vector<vector<char>> ans;
    vector<pair<int,int>> spaces;//记录下空白格的坐标
public:
    void dfs(vector<vector<char>>& board, int index){
    
    
        if(index == spaces.size()){
    
    
            ans = board;
            return;
        }
        //找到一个空白的位置
        pair<int,int> site = spaces[index];
        //确定行列
        int row = site.first;
        int col = site.second;
        for(int i = 0; i < 9; i ++){
    
    
            if(!rows[row][i] && !cols[col][i] && !grid[row / 3][col / 3][i]){
    
    
                board[row][col] = i + '1';
                rows[row][i] = true;
                cols[col][i] = true;
                grid[row / 3][col / 3][i] = true;
                dfs(board, index + 1);
                //回溯
                rows[row][i] = false;
                cols[col][i] = false;
                grid[row / 3][col / 3][i] = false;
            }
        }
    }

    void solveSudoku(vector<vector<char>>& board) {
    
    
        //初始化 
        memset(rows, false, sizeof(rows));
        memset(cols, false, sizeof(cols));
        memset(grid, false, sizeof(grid));
        //把初始数独输入到行列九宫格中
        for(int i = 0; i < 9; i ++){
    
    
            for(int j = 0; j < 9; j ++){
    
    
                if(board[i][j] == '.'){
    
    
                    spaces.push_back({
    
    i,j});
                }else{
    
    
                    rows[i][board[i][j] - '1'] = true;
                    cols[j][board[i][j] - '1'] = true;
                    grid[i / 3][j / 3][board[i][j] - '1'] = true;
                }
            }
        }
        dfs(board, 0);
        board = ans;
    }
};

Guess you like

Origin blog.csdn.net/HERODING23/article/details/108611303