Leetcode c语言-Valid Sudoku
Title:
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.
这道题就是判断一个数独是否合理,也就是每一行每一列,和每一个小九宫格里的数字都不能重复,1-9.
这道题分三步,分别判断每一行,每一列和每一个九宫格。
Solution:
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;
num=board[i][j]-'0';
if(map[num]!=0)return false;
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){
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;
}
posted on 2017-09-27 23:37 sichenzhao 阅读(131) 评论(0) 编辑 收藏 举报