首页 > 代码库 > LeetCode:Sudoku Solver && Valid Sudouku

LeetCode:Sudoku Solver && Valid Sudouku

其实数独还是我挺喜欢的一个游戏。原来有本数独的书。

其实Sudoku是基于Valid Sudouku.其实一开始有点想太多。基于平常玩数独的经验,有很多解数独的规则。貌似这个人为判断因素比较多。

而且一开始理解的valid是有解无解,其实这里要求的是给定的board里的数字是否符合规则,不一定能解。

其实我这里有3个函数,一个是判断行除该位置外是否有相同值的,一个是列,最后一个是小正方形里。

而求解使用的是暴力,一个个尝试。


public class SudokuSolver {
   
    public void solveSudoku(char[][] board)
    {
        if(board == null) return;
        suduku(board);
    }
   
    public boolean suduku(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)(‘0‘ + k);
                        if(isValid(board,i,j)&&suduku(board))
                        {
                            return true;
                        }
                        board[i][j] = ‘.‘;
                    }
                    return false;
                }
            }
        }
        return true;
    }
   
    public boolean isValid(char[][] board,int row,int column)
    {
        if(board == null)return false;
        boolean valid = true;
        if(rowContainsValue(board,row,column,board[row][column]))
        {
            valid = false;
            return valid;
        }
        if(columnContainsValue(board,row,column,board[row][column]))
        {
            valid = false;
            return valid;
        }
        if(squareContainsValue(board,row,column,board[row][column]))
        {
            valid = false;
            return valid;
        }
        return valid;
    }
   
    public boolean isValidSudoku(char[][] board) {
       
        if(board == null) return false;
        boolean valid = true;
        boolean flag = true;
        for(int i = 0;i<9&&flag;i++)
        {
            for(int j = 0;j<9;j++)
            {
                if(board[i][j] == ‘.‘)
                    continue;
                if(rowContainsValue(board,i,j,board[i][j]))
                {
                    valid = false;
                    flag = false;
                    break;
                }
                if(columnContainsValue(board,i,j,board[i][j]))
                {
                    valid = false;
                    flag = false;
                    break;
                }
                if(squareContainsValue(board,i,j,board[i][j]))
                {
                    valid = false;
                    flag = false;
                    break;
                }
            }
        }
       
        return valid;
       
    }
   
    public boolean rowContainsValue(char[][] board,int row,int column,char value)
    {
        boolean val =false;
        for(int i = 0;i<9;i++)
        {
            if(board[row][i] == value&&i!=column)
            {
                val = true;
                break;
            }
        }
        return val;
    }
    public boolean columnContainsValue(char[][] board,int row,int column,char value)
    {
        boolean val =false;
        for(int i = 0;i<9;i++)
        {
            if(board[i][column] == value&&i!=row)
            {
                val = true;
                break;
            }
        }
        return val;
    }
    public boolean squareContainsValue(char[][] board,int row,int column,char value)
    {
        boolean val =false;
        boolean flag = false;
        for(int i = (row/3)*3;i<(row/3)*3+3&&!flag;i++)
        {
            for(int j=(column/3)*3;j<(column/3)*3+3;j++)
            {
                if(board[i][j] == value&&i!=row&&j!=column)
                {
                    val = true;
                    flag = true;
                    break;
                }
            }
        }
        return val;
    }

}