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.
这题的标签是哈希表,可是c++中没有现成的哈希表对象。
class Solution {
private:
bool check(bool used[],char c)
{
if(c=='.')
return false;
else if(used[c-'1'])
return true;
else
{
used[c-'1']=true;
return false;
}
}
public:
bool isValidSudoku(vector<vector<char> > &board) {
int i,j,k;
bool used[9];
for(i=0;i<9;i++)
{
memset(used,false,9);
for(j=0;j<9;j++)
{
if(check(used,board[i][j]))
{
return false;
}
}
memset(used,false,9);
for(j=0;j<9;j++)
{
if(check(used,board[j][i]))
{
return false;
}
}
}
for(k=0;k<3;k++)
{
memset(used,false,9);
for(i=k*3;i<k*3+3;i++)
{
for(j=0;j<3;j++)
{
if(check(used,board[j][i]))
{
return false;
}
}
}
memset(used,false,9);
for(i=k*3;i<k*3+3;i++)
{
for(j=3;j<6;j++)
{
if(check(used,board[j][i]))
{
return false;
}
}
}
memset(used,false,9);
for(i=k*3;i<k*3+3;i++)
{
for(j=6;j<9;j++)
{
if(check(used,board[j][i]))
{
return false;
}
}
}
}
return true;
}
};