-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path36. Valid Sudoku.c
74 lines (56 loc) · 1.78 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
66
67
68
69
70
71
72
73
74
/*
36. Valid Sudoku
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.
*/
#define VERIFY_HASH(I, J) do { \
c = board[I][J]; \
if (c >= '1' && c <= '9') { \
b = 1 << (c - '1'); \
if (hash & b) return false; \
hash |= b; \
}\
} while (0)
#define IDX(I, J) ((I) / 3 * (boardColSize + 2) / 3 + (J) / 3)
bool isValidSudoku(char** board, int boardRowSize, int boardColSize) {
int hash;
int i, j, m, n;
char c;
int b;
int *hc, *hs, *buff;
buff = calloc((boardColSize + ((boardRowSize + 2) / 3 * (boardColSize + 2) / 3)), sizeof(int));
//assert(buff);
hc = &buff[0];
hs = &buff[boardColSize];
for (i = 0; i < boardRowSize; i ++) {
hash = 0;
for (j = 0; j < boardColSize; j ++) {
c = board[i][j];
if (c < '1' || c > '9') continue;
b = 1 << (c - '1');
if ((hash & b) || // verify failed on current line
(hc[j] & b) || // verify failed on current column
(hs[IDX(i, j)] & b)) { // failed on current nestled square
free(buff);
return false;
}
hash |= b;
hc[j] |= b;
hs[IDX(i, j)] |= b;
}
}
free(buff);
return true;
}
/*
Difficulty:Medium
Total Accepted:124.3K
Total Submissions:347.8K
Companies Snapchat Uber Apple
Related Topics Hash Table
Similar Questions
Sudoku Solver
*/