-
Notifications
You must be signed in to change notification settings - Fork 1
/
36_valid_sudoku.c
65 lines (58 loc) · 1.61 KB
/
36_valid_sudoku.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <assert.h>
#include <stdio.h>
int isValidSudoku(char** board, int boardRowSize, int boardColSize) {
int row = 0, col = 0;
for(row = 0; row < boardRowSize; row++) {
int statics[10] = {0};
for(col = 0; col < boardColSize; col++) {
int num = board[row][col]-'0';
if(num >= 0 && num <=9) {
statics[num]++;
if(statics[num] > 1)
return 0;
}
}
}
for(col = 0; col < boardColSize; col++) {
int statics[10] = {0};
for(row = 0; row < boardRowSize; row++) {
int num = board[row][col] - '0';
if(num >=0 && num <=9) {
statics[num]++;
if(statics[num] > 1)
return 0;
}
}
}
for(row = 0; row < boardRowSize; row+=3) {
for(col = 0; col < boardColSize; col+=3) {
int statics[10] = {0}, i = 0, j = 0;
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++) {
int num = board[row+i][col+j]-'0';
if(num >= 0 && num <=9) {
statics[num]++;
if(statics[num] > 1)
return 0;
}
}
}
}
}
return 1;
}
int main() {
char *board[9] = {
"53..7....",
"6..195...",
".98....6.",
"8...6...3",
"4..8.3..1",
"7...2...6",
".6....28.",
"...419..5",
"....8..79"
};
assert(isValidSudoku(board, 9, 9) == 1);
return 0;
}