您的位置:首页 > 编程语言 > Java开发

[leetcode-37]Sudoku Solver(java)

2015-09-01 22:20 363 查看
问题描述:

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.





分析:这道题使用的是回溯算法,回溯本质是深搜+剪枝。深搜又常常利用递归,然后当替换每个“.”时都判断是否有效。如果有效的话,就继续递归下去。

注意,一般递归函数都在开头位置判断是否结束,但是对于该问题而言,不大容易判断叶节点。所以这里采用的是利用返回值true或false来对树的深度进行控制。如果为solve到false时,就回溯。回溯的手段就是使用更改函数主体复位,并return。

代码如下:328ms

[code]public class Solution {
    public void solveSudoku(char[][] board) {
        solve(board);
    }
    private boolean solve(char[][] board){
         for(int row = 0;row<9;row++){
             for(int col = 0;col<9;col++){
                 if(board[row][col] ==  '.'){
                     for(char i = '1';i<='9';i++){
                         board[row][col] = i;
                         if(isValid(board,row,col) && solve(board))
                            return true;
                         board[row][col] = '.';
                     }
                     return false;
                 }
             }
         }
         return true;
    }
    private boolean isValid(char[][] board,int row,int col){
        for(int i = 0;i<9;i++){
            if(i!=col && board[row][i] == board[row][col])
                return false;
        }
        for(int i = 0;i<9;i++){
            if(i!=row && board[i][col] == board[row][col])
                return false;
        }
        int beginRow = 3*(row/3);
        int beginCol = 3*(col/3);
        for(int i = beginRow;i<beginRow+3;i++){
            for(int j = beginCol;j<beginCol+3;j++){
                if(i!=row && j!=col && board[i][j] == board[row][col])
                    return false;
            }
        }
        return true;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: