Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles – The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character \'.\'.

A partially filled sudoku which is valid.

思路:

思路很简单,只要检查每行、每列、每个子区域中没有重复的数字即可。

代码:

 1     bool isValidSudoku(vector<vector<char> > &board) {
 2         // Note: The Solution object is instantiated only once and is reused by each test case.
 3         map<char, int> table;
 4         for(int i = 0; i < 9; i++){
 5             table.clear();
 6             for(int j = 0; j < 9; j++){
 7                 if(board[i][j] != \'.\'){
 8                     if(table.find(board[i][j]) != table.end())
 9                         return false;
10                     else
11                         table[board[i][j]] = 1;
12                 }
13             }
14         }
15         for(int i = 0; i < 9; i++){
16             table.clear();
17             for(int j = 0; j < 9; j++){
18                 if(board[j][i] != \'.\'){
19                     if(table.find(board[j][i]) != table.end())
20                         return false;
21                     else
22                         table[board[j][i]] = 1;
23                 }
24             }
25         }
26         for(int i = 0; i < 9; i++){
27             table.clear();
28             for(int j = 0; j < 3; j++){
29                 for(int k = 0; k < 3; k++){
30                     if(board[3*(i/3)+j][3*(i%3)+k] != \'.\'){
31                         if(table.find(board[3*(i/3)+j][3*(i%3)+k]) != table.end())
32                             return false;
33                         else
34                             table[board[3*(i/3)+j][3*(i%3)+k]] = 1;
35                     }
36                 }
37             }
38         }
39         return true;
40     }

 

版权声明:本文为waruzhi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/waruzhi/p/3415534.html