栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

LeetCode-37. Sudoku Solver [C++][Java]

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode-37. Sudoku Solver [C++][Java]

LeetCode-37. Sudoku Solverhttps://leetcode.com/problems/sudoku-solver/

题目描述

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.Each of the digits 1-9 must occur exactly once in each column.Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

The '.' character indicates empty cells.

Example 1:

Input: 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"]]
Output: [["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"]]
Explanation: The input board is shown above and the only valid solution is shown below:


Constraints:

board.length == 9board[i].length == 9board[i][j] is a digit or '.'.It is guaranteed that the input board has only one solution.

解题思路

【C++】

class Solution {
public:
    void solveSudoku(vector>& board) {
        row = vector>(9, vector(10));
        col = vector>(9, vector(10));
        box = vector>(9, vector(10));
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                if (board[i][j] == '.') continue;
                else {
                    int num = board[i][j]-'0';
                    row[i][num] = 1;
                    col[j][num] = 1;
                    box[(i/3)*3+j/3][num] = 1;
                }
            }
        }
        backtracking(board, 0, 0);
    }
private:
    vector> row, col, box;
    bool backtracking(vector>& board, int x, int y){
        if (y == 9) return true;   
        int nx = (x + 1) % 9;
        int ny = (nx == 0) ? y + 1 : y;
        if (board[y][x] != '.') {return backtracking(board, nx, ny);}
        for (int i =1; i<=9; i++) {
            int b = (y/3)*3+x/3;
            if (!row[y][i] && !col[x][i] && !box[b][i]) {
                board[y][x] = i + '0';
                row[y][i] = col[x][i] = box[b][i] = 1;
                if (backtracking(board, nx, ny)) return true;
                board[y][x] = '.';
                row[y][i] = col[x][i] = box[b][i] = 0;
            }
        }
        return false;
    }
};

【Java】

class Solution {
    private boolean[][] row = new boolean[9][10];
    private boolean[][] col = new boolean[9][10];
    private boolean[][] box = new boolean[9][10];

    public void solveSudoku(char[][] board) {
        for (int i=0; i<9; i++){
            for (int j=0; j<9; j++){
                if (board[i][j] == '.') continue;
                else {
                    int num = board[i][j]-'0';
                    row[i][num] = true;
                    col[j][num] = true;
                    box[(i/3)*3+j/3][num] = true;
                }
            }
        }
        backtracking(board, 0, 0);
    }

    private boolean backtracking(char[][] board, int x, int y){
        if (y == 9) return true;   
        int nx = (x + 1) % 9;
        int ny = (nx == 0) ? y + 1 : y;
        if (board[y][x] != '.') {return backtracking(board, nx, ny);}
        for (int i =1; i<=9; i++) {
            int b = (y/3)*3+x/3;
            if (!row[y][i] && !col[x][i] && !box[b][i]) {
                board[y][x] = (char) ('0' + i);
                row[y][i] = col[x][i] = box[b][i] = true;
                if (backtracking(board, nx, ny)) return true;
                board[y][x] = '.';
                row[y][i] = col[x][i] = box[b][i] = false;
            }
        }
        return false;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/754803.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号