LeetCode 37. Sudoku Solver

原题链接在此:https://leetcode.com/problems/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:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.

LeetCode 37. Sudoku Solver_第1张图片
A sudoku puzzle...

LeetCode 37. Sudoku Solver_第2张图片
...and its solution numbers marked in red.

Note:

    • The given board contain only digits 1-9 and the character '.'.
    • You may assume that the given Sudoku puzzle will have a single unique solution.
    • The given board size is always 9x9.

题解:

DFS needs state of board and current index.

DFS return value needs to indicate if we could fill the whole board.

For the current index, if if is '.', fill it with '1' to '9' and if it is valid, continue with next cell. If next cell return value is true, then we need to stop here, no backtracking, return true.

If it is not '.', then it is digit, return next one.

Note: 1. 递归时当j == board[0].length 时, 应该是 return helper, 而不是单单的helper, 若是没有return, 会继续向下运行,到20行时就会out of index.

  2. 检查完isValid后直接检查是否继续递归是否有一个solution, 若是有就应该停下return.

Time Complexity: exponential.

Space: 递归最多会用O(board.length ^ 2)层stack.

AC Java: 

 1 class Solution {
 2     public void solveSudoku(char[][] board) {
 3         if(board == null || board.length != 9 || board[0].length != 9){
 4             return;
 5         }
 6         
 7         dfsSolve(board, 0, 0);
 8     }
 9     
10     private boolean dfsSolve(char [][] board, int i, int j){
11         if(i == board.length){
12             return true;
13         }
14         
15         if(j == board[0].length){
16             // get to next row
17             return dfsSolve(board, i+1, 0);
18         }
19         
20         if(board[i][j] == '.'){
21             for(char c = '1'; c<='9'; c++){
22                 board[i][j] = c;
23                 if(isValid(board, i, j)){
24                     // check if there is a solution, 如果这里有一个正确的solution就应该停下了
25                     if(dfsSolve(board, i, j+1)){
26                         return true;
27                     }
28                 }
29             }
30             
31             board[i][j] = '.';
32         }else{
33             // 这里已有数字,直接填本行下一个
34             return dfsSolve(board, i, j+1);
35         }
36         
37         return false;
38     }
39     
40     private boolean isValid(char [][] board, int x, int y){
41         for(int i = 0; i<9; i++){
42             if(i != x && board[i][y] == board[x][y]){
43                 return false;
44             }
45         }
46         
47         for(int j = 0; j<9; j++){
48             if(j != y && board[x][j] == board[x][y]){
49                 return false;
50             }
51         }
52         
53         int boxR = x / 3 * 3;
54         int boxC = y / 3 * 3;
55         for(int k = 0; k<9; k++){
56             if((boxR + k / 3 != x || boxC + k % 3 != y) && board[boxR + k / 3][boxC + k % 3] == board[x][y]){
57                 return false;
58             }
59         }
60         
61         return true;
62     }
63 }

类似Valid Sudoku, Unique Paths III.

你可能感兴趣的:(LeetCode 37. Sudoku Solver)