leetcode Java二刷:37. 解数独

23 篇文章 0 订阅
11 篇文章 0 订阅

题目:37. 解数独

思路:回溯。

在每一个空格处尝试放入1~9,需要注意的是,如果找到解就不回溯了,因为我们要把解存在原来的数据结构里。

代码:

class Solution {
    private boolean[][] row = new boolean[9][9];
    private boolean[][] col = new boolean[9][9];
    private boolean[][] box = new boolean[9][9];
    
    public void solveSudoku(char[][] board) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    int n = board[i][j] - '0';
                    row[i][n - 1] = true;
                    col[j][n - 1] = true;
                    box[i / 3 * 3 + j / 3][n - 1] = true;
                }
            }
        }
        backtrack(board, 0, 0);

    }
    public boolean backtrack(char[][] board, int a, int b) {
        while (board[a][b] != '.') {
            b++;
            if (b >= 9) {
                a++;
                b = 0;
            }
            if (a >= 9) {
                return true;
            }
        }

        for (int i = 0; i < 9; i ++) {
            if (!row[a][i] && !col[b][i] && !box[a / 3 * 3 + b / 3][i]) {
                board[a][b] = (char) (i + '0' + 1);
                row[a][i] = true;
                col[b][i] = true;
                box[a / 3 * 3 + b / 3][i] = true;
                // 找到解就不回溯。
                if (backtrack(board, a, b)) {
                    return true;
                }
                else {
                    board[a][b] = '.';
                    row[a][i] = false;
                    col[b][i] = false;
                    box[a / 3 * 3 + b / 3][i] = false;
                }
            }
        }
        return false;
    }
}
class Solution {
    private boolean[][] row = new boolean[9][9];
    private boolean[][] col = new boolean[9][9];
    private boolean[][] box = new boolean[9][9];
    
    public void solveSudoku(char[][] board) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    int n = board[i][j] - '0';
                    row[i][n - 1] = true;
                    col[j][n - 1] = true;
                    box[i / 3 * 3 + j / 3][n - 1] = true;
                }
            }
        }
        backtrack(board, 0, 0);

    }
    public boolean backtrack(char[][] board, int a, int b) {
        if (b >= 9) {
            return backtrack(board, a+1, 0);
        }
        if (a >= 9) {
            return true;
        }
        if (board[a][b] != '.') {
            return backtrack(board, a, b+1);
        }
        for (int i = 0; i < 9; i ++) {
            if (!row[a][i] && !col[b][i] && !box[a / 3 * 3 + b / 3][i]) {
                board[a][b] = (char) (i + '0' + 1);
                row[a][i] = true;
                col[b][i] = true;
                box[a / 3 * 3 + b / 3][i] = true;
                // 找到解就不回溯。
                if (backtrack(board, a, b + 1)) {
                    return true;
                }
                else {
                    board[a][b] = '.';
                    row[a][i] = false;
                    col[b][i] = false;
                    box[a / 3 * 3 + b / 3][i] = false;
                }
            }
        }
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值