LeetCode Sudoku Solver 回溯

原创 2015年07月08日 11:20:37

思路:

DFS。时间复杂度O(9^4),空间复杂度O(1)。
下棋添子,下完一步下另一步,不行就回溯。

class Solution {
private:
    bool isValid(vector<vector<char>> &board, int i, int j) {
        //check col
        for(int row = 0; row < 9; ++row) {
            if(row != i && board[row][j] == board[i][j]) {
                return false;
            }
        }
        //check row
        for(int col = 0; col < 9; ++col) {
            if(col != j && board[i][col] == board[i][j]) {
                return false;
            }
        }
        //check cube
        for(int row = i - i%3; row < i + (3 - i%3); ++row) {
            for(int col = j - j%3; col < j + (3 - j%3); ++col) {
                if(row == i && col == j) continue;
                if(board[row][col] == board[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }

    bool dfs(vector<vector<char>>& board) {
        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) {
                        board[i][j] = '0' + k;
                        if(isValid(board, i, j) && dfs(board)) {
                            return true;
                        }
                        board[i][j] = '.';
                    }
                    return false;
                }
            }
        }
        return true;
    }
public:
    void solveSudoku(vector<vector<char>>& board) {
        dfs(board);
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode系列(53)Sudoku Solver

Write aprogram to solve a Sudoku puzzle by filling the empty cells. Emptycells are indicated by the...

LeetCode---Sudoku Solver

题目大意:编写一个程序解决数独问题,要求填满表中的空白位置,使得其所在的行和列以及所在的宫没有相同元素。 算法思想: 1.遍历该表格,寻找空白位置。 2.对于每个空白位置,枚举放置1~9这9个数...

37. Sudoku Solver Leetcode Python

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

Leetcode224: Sudoku Solver

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

[LeetCode][Java] Sudoku Solver

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are ind...

LeetCode 37. Sudoku Solver|DFS算法

题目简介Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by...

LeetCode--Sudoku Solver

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

LeetCode 37 Sudoku Solver(求解数独)(*)

翻译写一个程序来通过填充空格求解数独。空格用'.'表示。你可以假定这里只有唯一解。(示例图片看下文)原文代码这道题我没写……不过为了博客的连续性,先凑一篇占个位置,以后再修改。class Soluti...
  • NoMasp
  • NoMasp
  • 2015-12-02 12:47
  • 1974

[LeetCode]037-Sudoku Solver

题目: Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated b...

LeetCode 37 Sudoku Solver (C,C++,Java,Python)

Problem: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ar...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)