- Valid Sudoku My Submissions QuestionEditorial Solution
Total Accepted: 74295 Total Submissions: 241017 Difficulty: Easy
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.
数独游戏的介绍,http://sudoku.com.au/TheRules.aspx
本题不用填充表格,只要判定是否合法就行
所以只要满足三个规则即可:
1.行包含1到9中几个,且不重复
2.列包含1到9中几个,且不重复
3.9宫格每格包含1到9几个,且不重复
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
bool used[9];
for(int i =0;i<9;++i){
fill(used,used+9,false);
for(int j=0;j<9;++j) //判定行
if(!check(board[i][j],used))
return false;
fill(used,used+9,false);
for(int j=0;j<9;++j) //判定列
if(!check(board[j][i],used))
return false;
}
for(int r =0;r<3;++r) //9个模块
for(int c=0;c<3;++c){
fill(used,used+9,false);
for(int i=3*r;i<3*r+3;++i) //每个9宫格
for(int j=3*c;j<3*c+3;++j)
if(!check(board[i][j],used))
return false;
}
return true;
}
bool check(char c,bool used[9])
{
if(c=='.')return true;
if(used[c-'1']) return false;
return used[c-'1']=true;
}
};