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
'.'
.Solution:
bool isValidSudoku(vector<vector<char> > &board) {
vector<int> row(9, 0);
vector<int> column(9, 0);
vector<int> row1 = row;
vector<int> column1 = column;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(board[i][j]!='.')
{
if(row[board[i][j]-49]!=0)
return false;
else
row[board[i][j]-49] = 1;
}
if(board[j][i]!='.')
{
if(column[board[j][i]-49]!=0)
return false;
else
column[board[j][i]-49] = 1;
}
}
row = row1;
column = column1;
}
for(int i=0;i<9;i+=3)
{
for(int j=0;j<9;j+=3)
{
for(int m=0;m<3;m++)
{
for(int n=0;n<3;n++)
{
if(board[m+i][n+j]!='.')
{
if(row[board[m+i][n+j]-49]!=0)
return false;
else
row[board[m+i][n+j]-49] = 1;
}
}
}
row = row1;
}
}
return true;
}
No comments:
Post a Comment