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.

LeetCode: solveSudoku 解题报告

SOLUTION:

ref: http://blog.csdn.net/fightforyourdream/article/details/16916985

采用递归+回溯模板来解决此问题:

1. 判定DFS的退出条件。

    (1) y越界,应该往下一行继续解。

  (2)x越界,代表数独解完,应该返回。

2. DFS的主体部分:

    把9个可能的值遍历一次,如果是OK的(使用独立 的Valid函数来判定行,列,BLOCK是否符合),继续DFS,否则回溯。

3. 最后的返回值:

    如果所有的可能性遍历都没有找到TRUE,最后应该返回FALSE,也就是当前情况无解。这个时候DFS会自动回溯到上一层再查找别的可能解。

 1 public static void main(String[] args) {  
 2         char[][] board = {  
 3                 {'.','.','9','7','4','8','.','.','.'},  
 4                 {'7','.','.','.','.','.','.','.','.'},  
 5                 {'.','2','.','1','.','9','.','.','.'},  
 6                 {'.','.','7','.','.','.','2','4','.'},  
 7                 {'.','6','4','.','1','.','5','9','.'},  
 8                 {'.','9','8','.','.','.','3','.','.'},  
 9                 {'.','.','.','8','.','3','.','2','.'},  
10                 {'.','.','.','.','.','.','.','.','6'},  
11                 {'.','.','.','2','7','5','9','.','.'},  
12         };  
13         solveSudoku(board);  
14         for(int i=0; i<9; i++){  
15             for(int j=0; j<9; j++){  
16                 System.out.print(board[i][j]);  
17             }  
18             System.out.println();  
19         }  
20     }  
21     
22     public void solveSudoku1(char[][] board) {
23         dfs1(board, 0, 0);
24     }
25     
26     public boolean dfs1(char[][] board, int x, int y) {
27         // go to next row.
28         if (y == 9) {
29             y = 0;
30             x++;
31         }
32         
33         // done
34         if (x >= 9) {
35             return true;
36         }
37         
38         // Skip the solved point.
39         if (board[x][y] != '.') {
40             return dfs1(board, x, y + 1);
41         }
42         
43         // Go throught all the possibilities.
44         for (int k = 0; k < 9; k++) {
45             board[x][y] = (char)('1' + k);    
46             // SHOULD RETURN HERE IF INVALID.                
47             if (isValid1(board, x, y) && dfs1(board, x, y  + 1)) {
48                 return true;
49             }
50             board[x][y] = '.';
51         }                
52 
53         // because all the possibility is impossiable.                
54         return false;
55     }
56     
57     public boolean isValid1(char[][] board, int x, int y) {
58         // Judge the column.
59         for (int i = 0; i < 9; i++) {
60             if (i != x && board[i][y] == board[x][y]) {
61                 return false;
62             }
63         }
64         
65         // Judge the row.
66         for (int i = 0; i < 9; i++) {
67             if (i != y && board[x][i] == board[x][y]) {
68                 return false;
69             }
70         }
71         
72         // Judge the block.
73         int i = x / 3 * 3;
74         int j = y / 3 * 3;
75         for (int k = 0; k < 9; k++) {
76             int xIndex = i + k / 3;
77             int yIndex = j + k % 3;
78             if (xIndex == x && yIndex == y) {
79                 continue;
80             }
81             
82             if (board[xIndex][yIndex] == board[x][y]) {
83                 return false;
84             }
85         }
86         
87         return true;
88     }
View Code

相关文章:

  • 2021-12-28
  • 2021-08-14
  • 2022-02-26
  • 2022-02-27
  • 2022-12-23
  • 2021-06-27
  • 2022-03-04
  • 2021-07-30
猜你喜欢
  • 2021-08-09
  • 2021-12-01
  • 2021-07-17
  • 2021-05-26
  • 2021-08-12
  • 2021-11-29
  • 2021-09-02
相关资源
相似解决方案