其他分享
首页 > 其他分享> > 37. 解数独

37. 解数独

作者:互联网

37. 解数独

编写一个程序,通过填充空格来解决数独问题。

数独的解法需 遵循如下规则

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)

数独部分空格内已填入了数字,空白格用 '.' 表示。

示例 1:

img

输入:board = [["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"]]
输出:[["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
解释:输入的数独如上图所示,唯一有效的解决方案如下所示:

提示:

思路:

​ 本题与上一题的有效数独类似,我认为都需要开出三个数组来记录行,列,3x3方格中数出现次数。

看代码理解即可

class Solution {
public:
    void solveSudoku(vector<vector<char>>& board) {
        int rows[9][9];//行中每个数出现的次数
        int columns[9][9];//列中每个数出现次数
        int subboxes[3][3][9];//3x3方格中每个数出现次数
        memset(rows,0,sizeof(rows));
        memset(columns,0,sizeof(columns));
        memset(subboxes,0,sizeof(subboxes));
        //初始化init
        init(board,rows,columns,subboxes);
        backtrack(board,0,0,rows,columns,subboxes);

    }
    bool backtrack(vector<vector<char>>& board,int i,int j,int rows[9][9],int columns[9][9],int subboxes[3][3][9]){
        if(j==9){
            //穷举到最后一列就换一行
            return backtrack(board,i+1,0,rows,columns,subboxes);
        }
        if(i==9){
            //穷举到最后一行说明找到答案
            return true;
        }
        if(board[i][j]!='.'){
            //当前位置不用填数
            return backtrack(board,i,j+1,rows,columns,subboxes);
        }
        //填数
        for(int k=1;k<10;k++){
            //只填入合法的数字
            if(rows[i][k-1]==0&&columns[j][k-1]==0&&subboxes[i/3][j/3][k-1]==0){
                rows[i][k-1]=1;
                columns[j][k-1]=1;
                subboxes[i/3][j/3][k-1]=1;
                board[i][j]=k+'0';
            }else{
                continue;
            }
            if(backtrack(board,i,j+1,rows,columns,subboxes)){
                return true;//能找到解, 就可以返回了
            }
            //撤销选择
            board[i][j]='.';
            rows[i][k-1]=0;
            columns[j][k-1]=0;
            subboxes[i/3][j/3][k-1]=0;
        }
        return false;//找不到解
    }


    void init(vector<vector<char>>& board,int rows[9][9],int columns[9][9],int subboxes[3][3][9]){
        for(int i=0;i<board.size();i++){
            for(int j=0;j<board[0].size();j++){
                if(board[i][j]=='.')continue;
                int temp=board[i][j]-'0';
                rows[i][temp-1]=1;
                columns[j][temp-1]=1;
                subboxes[i/3][j/3][temp-1]=1;
            }
        }
    }

};

标签:rows,subboxes,int,37,board,解数,columns,数独
来源: https://www.cnblogs.com/BailanZ/p/16275646.html