Sudoku Solver, 求数独

问题描述:填充数独表中空元素。空元素为‘.‘

算法分析:没填充一个数,都要看这个数所在的行,列,小矩阵是否合法。然后还要看整个数独表是否正确,而判断整个数独表只能通过递归,因为前一个结果的判断要依赖后一个结果。这应该属于动态规划问题。要递归回溯。

public void solveSudoku(char[][] board) {
        solve(board);
    }

    public boolean solve(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] = (char) (k+‘0‘);
                        if(isValid(board, i, j) && solve(board))//当前有多个选择要递归回溯
                        {
                            return true;
                        }
                        board[i][j] = ‘.‘;
                    }
                    return false;
                }
            }
        }
        return true;
    }

    public boolean isValid(char[][] board, int i ,int j)
    {
        HashSet<Character> set = new HashSet<>();
        for(int k = 0; k < 9; k ++)
        {
            if(set.contains(board[i][k]))
            {
                return false;
            }
            if(board[i][k]!=‘.‘)
            {
                set.add(board[i][k]);
            }
        }
        set.clear();
        for(int k = 0; k < 9; k ++)
        {
            if(set.contains(board[k][j]))
            {
                return false;
            }
            if(board[k][j]!=‘.‘)
            {
                set.add(board[k][j]);
            }
        }
        set.clear();
        for(int m = 0; m < 3; m ++)
        {
            for(int n = 0; n < 3; n ++)
            {
                int x = i/3*3+m;
                int y = j/3*3+n;
                if(set.contains(board[x][y]))
                {
                    return false;
                }
                if(board[x][y]!=‘.‘)
                {
                    set.add(board[x][y]);
                }
            }
        }
        return true;
    }
时间: 2024-11-08 23:11:34

Sudoku Solver, 求数独的相关文章

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] 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] 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. Hide Tags B

[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 OJ: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,一直寻找,不行

【leetcode】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

[leetcode]算法题目 - Sudoku Solver

最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后心里总算有了点底.回溯法的代码一般都是长成下面这样子: void backtracking(int[] arr, int boundary, int current, int[] result) { if(current>=boundary) //到达终止条件 { //判断result是否符合要求

【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 037 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