Leetcode 37: Sudoku Solver

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 37: Sudoku Solver相关的知识,希望对你有一定的参考价值。

ve 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.

技术分享

A sudoku puzzle...

 

技术分享

...and its solution numbers marked in red.

 

 1 public class Solution {
 2     public void SolveSudoku(char[,] board) {
 3         int rows = board.GetLength(0), cols = board.GetLength(1);
 4         if (rows != 9 || cols != 9) return;  
 5         
 6         CanSolveSudoku(board, 0, 0);
 7     }
 8     
 9     private bool CanSolveSudoku(char[,] board, int row, int col) 
10     { 
11         if (col >= 9) 
12         {
13             row += 1;
14             col = 0;
15         }
16         
17         if (row >= 9) return true;
18         
19         if (board[row, col] == .)
20         {
21             for (int k = 0; k < 9; k++)
22             {
23                 board[row, col] = (char)(k + (int)1);
24                 if (IsValidSudokuOption(board, row, col) && CanSolveSudoku(board, row, col + 1))
25                 {
26                     return true;
27                 }
28 
29                 board[row, col] = .;
30             }
31 
32             return false;
33         }
34         else
35         {
36             return CanSolveSudoku(board, row, col + 1);
37         }
38     }
39     
40     private bool IsValidSudokuOption(char[,] board, int row, int col) 
41     {   
42         var rused = new bool[9];
43         var cused = new bool[9];
44         var kused = new bool[9];
45         
46         for (int i = 0; i < 9; i++)
47         {
48             var c = board[row, i];
49             if (c != .)
50             {
51                 var index = (int)c - (int)1;
52                 if (rused[index]) return false;
53                 rused[index] = true;
54             }
55         }
56         
57         for (int i = 0; i < 9; i++)
58         {
59             var c = board[i, col];
60             if (c != .)
61             {
62                 var index = (int)c - (int)1;
63                 if (cused[index]) return false;
64                 cused[index] = true;
65             }
66         }
67         
68         for (int i = row / 3 * 3; i < row / 3 * 3 + 3; i++)
69         {
70             for (int j = col / 3 * 3; j < col / 3 * 3 + 3; j++)
71             {
72                 var c = board[i, j];
73                 if (c != .)
74                 {
75                     var index = (int)c - (int)1;
76                     if (kused[index]) return false;
77                     kused[index] = true;
78                 }
79             }
80         }
81         
82         return true;
83     }
84 }

 

There is something wrong with belwo solution, can you see?

 

 1 public class Solution {
 2     public void SolveSudoku(char[,] board) {
 3         int rows = board.GetLength(0), cols = board.GetLength(1);
 4         if (rows != 9 || cols != 9) return;  
 5         
 6         CanSolveSudoku(board, 0, 0);
 7     }
 8     
 9     private bool CanSolveSudoku(char[,] board, int row, int col) 
10     { 
11         if (col >= 9) 
12         {
13             row += 1;
14             col = 0;
15         }
16         
17         for (int i = row; i < 9; i++)
18         {
19             for (int j = col; j < 9; j++)
20             {
21                 if (board[i, j] == .)
22                 {
23                     for (int k = 0; k < 9; k++)
24                     {
25                         board[i, j] = (char)(k + (int)1);
26                         if (IsValidSudokuOption(board, i, j) && CanSolveSudoku(board, i, j + 1))
27                         {
28                             return true;
29                         }
30                         
31                         board[i, j] = .;
32                     }
33                     
34                     return false;
35                 }                        
36             }
37         }
38         
39         return true;
40     }
41     
42     private bool IsValidSudokuOption(char[,] board, int row, int col) 
43     {   
44         var rused = new bool[9];
45         var cused = new bool[9];
46         var kused = new bool[9];
47         
48         for (int i = 0; i < 9; i++)
49         {
50             var c = board[row, i];
51             if (c != .)
52             {
53                 var index = (int)c - (int)1;
54                 if (rused[index]) return false;
55                 rused[index] = true;
56             }
57         }
58         
59         for (int i = 0; i < 9; i++)
60         {
61             var c = board[i, col];
62             if (c != .)
63             {
64                 var index = (int)c - (int)1;
65                 if (cused[index]) return false;
66                 cused[index] = true;
67             }
68         }
69         
70         for (int i = row / 3 * 3; i < row / 3 * 3 + 3; i++)
71         {
72             for (int j = col / 3 * 3; j < col / 3 * 3 + 3; j++)
73             {
74                 var c = board[i, j];
75                 if (c != .)
76                 {
77                     var index = (int)c - (int)1;
78                     if (kused[index]) return false;
79                     kused[index] = true;
80                 }
81             }
82         }
83         
84         return true;
85     }
86 }

 

以上是关于Leetcode 37: Sudoku Solver的主要内容,如果未能解决你的问题,请参考以下文章

37. Sudoku Solver

LeetCode 37. Sudoku Solver —— 解数独

LeetCode 37 Sudoku Solver(求解数独)

[LeetCode] 37. Sudoku Solver 求解数独

LeetCode 37. Sudoku Solver

leetcode 37. Sudoku Solver