Sudoku Solver & Valid Sudoku

方法:采用递归的方式

class Solution {
public:
    void solveSudoku(vector<vector<char>>& board) {
        solve(board);
    }

    bool solve(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=0; k<9; ++k)
                    {
                        board[i][j] = k + ‘1‘;
                        if(isValid(board, i, j) && solve(board))
                            return true;
                        board[i][j] = ‘.‘;
                    }

                    return false;
                }
            }
        }

        return true;
    }

    bool isValid(vector<vector<char>> &board, int i, int j)
    {
        for(int k=0; k<9; ++k)
            if(i != k && board[i][j] == board[k][j])
                return false;
        for(int k=0; k<9; ++k)
            if(j != k && board[i][j] == board[i][k])
                return false;

        for(int m=3*(i/3); m<3*(i/3+1); ++m)
            for(int n=3*(j/3); n<3*(j/3+1); ++n)
                if(!(i == m && j == n) && board[i][j] == board[m][n])
                    return false;

        return true;
    }
};

Valid Sudoku

判别方法同上

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        for(int i=0; i<9; ++i)
            for(int j=0; j<9; ++j)
                if(board[i][j] != ‘.‘ && !isValid(board, i, j))
                    return false;

        return true;
    }

    bool isValid(vector<vector<char>> &board, int i, int j)
    {
        for(int k=0; k<9; ++k)
            if(i != k && board[i][j] == board[k][j])
                return false;

        for(int k=0; k<9; ++k)
            if(j != k && board[i][j] == board[i][k])
                return false;

        for(int m = 3*(i/3); m<3*(i/3+1); ++m)
            for(int n = 3*(j/3); n<3*(j/3+1); ++n)
                if(!(m == i && n == j) && board[i][j] == board[m][n])
                    return false;

        return true;
    }
};
时间: 2024-11-02 16:20:28

Sudoku Solver & Valid Sudoku的相关文章

LeetCode:Sudoku Solver &amp;&amp; Valid Sudouku

其实数独还是我挺喜欢的一个游戏.原来有本数独的书. 其实Sudoku是基于Valid Sudouku.其实一开始有点想太多.基于平常玩数独的经验,有很多解数独的规则.貌似这个人为判断因素比较多. 而且一开始理解的valid是有解无解,其实这里要求的是给定的board里的数字是否符合规则,不一定能解. 其实我这里有3个函数,一个是判断行除该位置外是否有相同值的,一个是列,最后一个是小正方形里. 而求解使用的是暴力,一个个尝试. public class SudokuSolver {        

LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

Valid Sudoku 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 boa

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

Valid Sudoku 和 Sudoku Solver

题目一:Valid Sudoku 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

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 36&amp;37] Valid Sudoku &amp; Sudoku Solver (数独问题)

题目链接:valid-sudoku import java.util.Arrays; /** * 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 whic

leetcode笔记: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 '.'. The following figure: A partially f

【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] 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.