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.
根据数独游戏的规则,一个9行9列的棋盘,每行每列和每一个3*3的小方格都不能有重复的数字,并且数字只能是0-9,逐一判断这些规则是否满足即可
bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
int map[10],i,j,k,l,num;
if(boardRowSize!=9 || boardColSize!=9)return false;
for(i=0;i<9;i++){ //每行不能有重复的数字
memset(map,0,sizeof(map));
for(j=0;j<9;j++){
if(board[i][j]=='.')continue; // 允许格子为空,空的格子默认填充点
if(board[i][j]<'0' || board[i][j]>'9')return false; //每个格子必须是0-9
num=board[i][j]-'0';
if(map[num]!=0)return false; //如果不等于0,说明已经被填充过1
map[num]=1;
}
}
for(j=0;j<9;j++){ //每列不能有重复的数字
memset(map,0,sizeof(map));
for(i=0;i<9;i++){
if(board[i][j]=='.')continue;
num=board[i][j]-'0';
if(map[num]!=0)return false;
map[num]=1;
}
}
for(i=0;i<9;i+=3){
for(j=0;j<9;j+=3){ //每9个格子不能有重复的数字
memset(map,0,sizeof(map));
for(k=i;k<i+3;k++){
for(l=j;l<j+3;l++){
if(board[k][l]=='.')continue;
num=board[k][l]-'0';
if(map[num]!=0)return false;
map[num]=1;
}
}
}
}
return true;
}