leetcode第36题--Sudoku Solver

题目:

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character ‘.‘.

You may assume that there will be only one unique solution.

A sudoku puzzle...

...and its solution numbers marked in red.

这题相对上题值判断是否合法来说就是更难了。

网上有很多解法,有的复杂有的简洁。我把我理解的记录如下:

思路应该要很明确。利用回溯法。这里是用递归的回溯。我特意复习了下回溯法。就是利用dfs。一直遍历找到符合的解,这个过程有相应的剪枝,剪就通过下面的isValid来进行。需要注意的地方已经给了注释。诉说千字不如代码一行。

class Solution {
    private:
    bool isValid(vector<vector<char> > &board, int x, int y)
    {
        for (int i = 0; i < 9; ++i) // 判断行是否有重复字符
        {
            if (board[i][y] == board[x][y] && i != x)
                return false;
        }
        for (int i = 0; i < 9; ++i) // 列
        {
            if (board[x][i] == board[x][y] && i != y)
                return false;
        }
        for (int i = 3*(x/3); i < 3*(x/3)+3; ++i) // 子九宫是否有重复
            for (int j = 3*(y/3); j < 3*(y/3)+3; ++j) // 纸上画一个框就能推出其中每个格子的起点和终点与xy的关系
            {
                if (board[i][j] == board[x][y] && i != x && j != y)
                    return false;
            }
        return true; // 都没有就返回合法
    }
public:
    bool solveSudoku(vector<vector<char> > &board)
    {
        // backtracking
        for (int i = 0; i < 9; ++i)
            for (int j = 0; j < 9; ++j)
            {
                if (‘.‘ == board[i][j])
                {
                    for (int k = 1; k <= 9; ++k) // 注意了是1到9,别误以为0到8
                    {
                        board[i][j] = ‘0‘+ k; // = 别写成 == 了,调式了十分钟发现我写成==,泪崩
                        if (isValid(board, i, j) && solveSudoku(board)) // 如果加入的数合法,并且,加入后的有解,那就true
                            return true;
                        board[i][j] = ‘.‘; // if没有返回那就是k取值不合法,所以要重新把元素恢复为‘.‘
                    }
                    return false;
                }
            }
        return true; // 最后一次所有的都填了数字了,解成功了就返回正确
    }
};
时间: 2024-10-14 12:17:19

leetcode第36题--Sudoku Solver的相关文章

[Leetcode][Python]36: Valid Sudoku

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 36: Valid Sudokuhttps://oj.leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty

LeetCode Medium: 36. Valid Sudoku

一.题目 Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of t

【LeetCode】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N

【leetcode刷题笔记】Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 题解:递归.在每个空位

LeetCode 36/37. Valid Sudoku/ Sudoku Solver

1. 题目分析 36 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board

[LeetCode] 37. 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.

[LeetCode][JavaScript]Sudoku Solver

Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in re

Sudoku Solver leetcode java

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 题解: 第一反

Leetcode | Valid Sudoku &amp; Sudoku Solver

判断valid,没有更好的方法,只能brute force. 1 class Solution { 2 public: 3 bool isValidSudoku(vector<vector<char> > &board) { 4 5 int n; 6 for (int i = 0; i < 9; ++i) { 7 vector<bool> contained(9, false); 8 for (int j = 0; j < 9; ++j) { 9 i