-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0036-valid-sudoku.c
38 lines (33 loc) · 1.08 KB
/
0036-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
bool isValidSudoku(char** board, int boardSize, int* boardColSize){
const int cnt = boardSize;
bool row[cnt][cnt];
bool col[cnt][cnt];
bool sub[cnt][cnt];
// initialize all the rows, cols, and sub-boxes to false
for (int r = 0; r < cnt; r++) {
for (int c = 0; c < cnt; c++) {
row[r][c] = false;
col[r][c] = false;
sub[r][c] = false;
}
}
for (int r = 0; r < cnt; r++) {
for (int c = 0; c < cnt; c++) {
// pass if not a number
if (board[r][c] == '.') {
continue;
}
// gets numerical index
int boardIndex = board[r][c] - '0' - 1;
int area = (r / 3) * 3 + (c / 3);
// if number exists
if (row[r][boardIndex] || col[c][boardIndex] || sub[area][boardIndex]) {
return false;
}
row[r][boardIndex] = true;
col[c][boardIndex] = true;
sub[area][boardIndex] = true;
}
}
return true;
}