Sudoku Solver, Leetcode 解题笔记

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.

250px-Sudoku-by-L2G-20050714.svg

250px-Sudoku-by-L2G-20050714_solution.svg

这题没什么好办法,直接暴力破解,用到递归。

public class Solution {
    public void solveSudoku(char[][] board) {
        if (board==null||board.length==0){
            return;
        }
        
        solved(board);
        
        
    }
    private boolean solved(char[][] board){
      
        for(int i=0; i<board.length; i++){
            for (int j=0; j<board[0].length; j++){
                if (board[i][j]=='.'){
                    for (char num='1'; num<='9'; num++){
                        
                        if(isValid(board, i, j, num)){
                            // no conflict
                            board[i][j]=num;
                            
                            if (solved(board)){
                                return true;
                            }
                            else{
                                board[i][j]='.';
                            }
                            
                        }
                      
                    }
                    // if no proper number found, return false
                    return false;
                }
            }
        }
        
        return true;
    }
    
    private boolean isValid(char[][] board, int i, int j, char c){
     
        // check column
        for (int row=0; row<9; row++){
            if (board[row][j]==c){
                return false;
            }
           
             
        }
        
       // check row
        for (int col=0; col<9; col++){
            if (board[i][col]==c){
                return false;
            }
            
        }
      
        // check block
        for(int row=i/3*3; row<i/3*3+3; row++){
            for (int col=j/3*3; col<j/3*3+3; col++){
                if (board[row][col]==c){
                    return false;
                }
                
            }
        }
       
        return true;
        
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s