forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 39
/
validSudoku.cpp
56 lines (50 loc) · 1.77 KB
/
validSudoku.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Source : https://oj.leetcode.com/problems/valid-sudoku/
// Author : Hao Chen
// Date : 2014-07-17
/**********************************************************************************
*
* 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.
*
* Note:
* > A valid Sudoku board (partially filled) is not necessarily solvable.
* Only the filled cells need to be validated.
*
*
**********************************************************************************/
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
const int cnt = 9;
bool row_mask[cnt][cnt] = {false};
bool col_mask[cnt][cnt] = {false};
bool area_mask[cnt][cnt] = {false};
//check each rows and cols
for(int r=0; r<board.size(); r++){
for (int c=0; c<board[r].size(); c++){
if (!isdigit(board[r][c])) continue;
int idx = board[r][c] - '0' - 1;
//check the rows
if (row_mask[r][idx] == true){
return false;
}
row_mask[r][idx] = true;
//check the cols
if (col_mask[c][idx] == true) {
return false;
}
col_mask[c][idx] = true;
//check the areas
int area = (r/3) * 3 + (c/3);
if (area_mask[area][idx] == true) {
return false;
}
area_mask[area][idx] = true;
}
}
return true;
}
};