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.
</pre><pre name="code" class="cpp">/*class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
vector<bool> visited(10,false);
//判断行
for(int i=0;i<9;i++){
fill(visited.begin(),visited.end(),false);
for(int j=0;j<9;j++){
if(!check(visited,board[i][j]))
return false;
}
}
//判断列
for(int j=0; j<9;j++){
fill(visited.begin(),visited.end(),false);
for(int i=0;i<9;i++){
if(!check(visited,board[i][j]))
return false;
}
}
//判断sub matrix
for(int i=0; i<9; i+=3){
for(int j=0; j<9; j+=3){
fill(visited.begin(),visited.end(),false);
for(int k=0; k<9; k++){
if(!check(visited,board[i+k/3][j+k%3]))
return false;
}
}
}
return true;
}
bool check(vector<bool>& visited, char c){
if(c=='.') return true;
int k = c-'0';
if(visited[k]==true) return false;
else{
visited[k]=true;
return true;
}
}
};*/
class Solution{
public:
bool isValidSudoku(vector<vector<char>>& board) {
bool check1[9][9]={false},check2[9][9]={false},check3[9][9]={false};
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(board[i][j]!='.'){
int num = board[i][j]-'0'-1;
int k = i/3*3+j/3;
if(check1[i][num]||check2[num][j]||check3[k][num])//分块
return false;
check1[i][num]=check2[num][j]=check3[k][num]=true;
}
}
}
return true;
}
};