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.
#include<stdio.h>
int isValidArray(char arr[]){
int i,j,n;
int arr1[9]={0};
for(i=0;i<9;i++) printf("d:%c",arr[i]);
printf("\n");
for(i=0;i<9;i++){
if(arr[i]=='.') continue;
n=(arr[i]-'0')%9;
arr1[n]++;
if(arr1[n]>1) { printf("n=%d",n);return 0;}
}
return 1;
}
int isValidSudoku(char board[9][9]) {
int i,j,k,l;
int res=0;
char arr[9]={'0'};
char arr1[9]={'0'};
for(i=0;i<9;i++){
for(j=0;j<9;j++){
arr[j]=board[i][j];
arr1[j]=board[j][i];
}
if(isValidArray(arr)==0) return 0;
if(isValidArray(arr1)==0) return 0;
}
for(l=0;l<3;l++){
for(k=0;k<3;k++){
for(i=l*3;i<l*3+3;i++){
for(j=k*3;j<k*3+3;j++){
printf("w%c",board[i][j]);
arr[(i*3+j)%9]=board[i][j];
}
}
if(isValidArray(arr)==0) return 0;
}
}
return 1;
}
void main(){
char array[9][9]={"53..7....","6..195...",".98....6.","8...6...3","4..8.3..1","7...2...6",".6....28.","...419..5","....8..79"};
int res=isValidSudoku(array);
printf("%d\n",res);
}