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.
Subscribe to see which companies asked this question
数独游戏,检测是否合法, “.”代表空格,可以无视
bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
int i,j,row,col,n,nums[10],num;
row=boardRowSize;
col=boardColSize;
for(i=0;i<row;i++) //横向检测
{
for(j=0;j<10;j++)
nums[j]=0;
for(j=0;j<col;j++)
{
num=board[i][j]-'0';
if(num+'0'=='.')
continue;
if(++nums[num]>1)
return false;
}
}
for(j=0;j<col;j++) //纵向检测
{
for(i=0;i<10;i++)
nums[i]=0;
for(i=0;i<row;i++)
{
num=board[i][j]-'0';
if(num+'0'=='.')
continue;
if(++nums[num]>1)
return false;
}
}
for(n=0;n<(row/3)*(col/3);n++) //九宫格检测
{
for(j=0;j<10;j++)
nums[j]=0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
num=board[n/3*3+i][(n%3)*3+j]-'0';
if(num+'0'=='.')
continue;
if(++nums[num]>1)
return false;
}
}
return true;
}