57 lines
1.8 KiB
C++
57 lines
1.8 KiB
C++
// 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;
|
|
}
|
|
};
|