Leetcode面试经典150题-36-有效数独升级版-37.解数独

  解法都在代码里,不懂就留言或者私信,比第一题稍微难点

public static void solveSudoku(char[][] board) {
        /**定义三个二维数组分别代表行、列、桶(每9个格子)*/
        boolean[][] rowExists = new boolean[9][10];
        boolean[][] colExists = new boolean[9][10];
        boolean[][] bucketExists = new boolean[9][10];
        /**根据原始board初始化三个是否存在的二维数组*/
        initArrays(board, rowExists, colExists, bucketExists);
        process(board, rowExists, colExists, bucketExists, 0, 0);
    }

    private static boolean process(char[][] board, boolean[][] rowExists, boolean[][] colExists, boolean[][] bucketExists, int curRow, int curCol) {
        /**我们是按照行从下到上填的,一行填完才填下一行,如果行越界了,说明正常范围内的行都填完了*/
        if(curRow == 9) {
            return true;
        }
        /**如果当前的位置可以填,那下个位置填啥,nextRow和nextCol表示下个要填的位置的行和列*/
        int nextRow = curCol == 8? curRow + 1 : curRow;
        int nextCol = curCol == 8? 0 : curCol + 1;
        /**当前位置填过了就继续填下一个*/
        if(board[curRow][curCol] != '.') {
            return process(board, rowExists, colExists, bucketExists, nextRow, nextCol);
        } else {
            /**当前位置可以填就填,那我们填哪些呢?所有的目前符合的(行列桶都不违规)都可以试试*/
            for(char c = '1'; c <= '9'; c++) {
                int num = c - '0';
                int bucketNum = (curRow/3) * 3 + curCol / 3;
                /**行、列、桶任意一个存在就不能填*/
                if(rowExists[curRow][num] || colExists[curCol][num] || bucketExists[bucketNum][num]) {
                    continue;
                }
                board[curRow][curCol] = c;
                /**填完之后行、列、桶做标记*/
                rowExists[curRow][num] = true;
                colExists[curCol][num] = true;
                bucketExists[bucketNum][num] = true;
                /**如果后面的过程尝试正确就直接返回,否则不处理*/
                if(process(board, rowExists, colExists, bucketExists, nextRow, nextCol)) {
                    return true;
                }
                /**不成功一定要恢复现场*/
                board[curRow][curCol] = '.';
                rowExists[curRow][num] = false;
                colExists[curCol][num] = false;
                bucketExists[bucketNum][num] = false;

            }
        }
        /**如果中间过程都没有返回,返回false*/
        return false;
    }

    /**遍历board初始化是否存在的数组*/
    private static void initArrays(char[][] board, boolean[][] rowExists, boolean[][] colExists, boolean[][] bucketExists) {
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[i].length; j++) {
                if(board[i][j] == '.') {
                    continue;
                }
                int num = board[i][j] - '0';
                int bucketNum = (i / 3) * 3 + j / 3;
                rowExists[i][num] = true;
                colExists[j][num] = true;
                bucketExists[bucketNum][num] = true;
            }
        }
    }

运行结果:

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值