Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
此题要求判断一个数独是否有效?
规则:
同一行,同一列,同一个33小方块都只含1-9中的不重复数字。解法也按照规则来,判断同行同列同33小方块内是否有重复数字,若无则ok,否则不行。
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
set<string> s;
string s1="in row";
string s2="in col";
string s3="in square";
for (int i=0;i<board.size();i++)
{
for (int j=0;j<board[0].size();j++)
{
char ch=board[i][j];
if(ch!='.')
{
cout<<ch<<endl;
string row=ch+s1+to_string(i),col=ch+s2+to_string(j),square=ch+s3+to_string(i/3)+to_string(j/3);
//cout<<row<<col<<square<<endl;
if(s.count(row)||s.count(col)||s.count(square))
{
return false;
}
else
{
s.insert(row);
s.insert(col);
s.insert(square);
}
}
}
}
return true;
}
};