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 }

 

转载于:https://www.cnblogs.com/liangmou/p/7791480.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值