-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_sudoku.go
46 lines (42 loc) · 928 Bytes
/
valid_sudoku.go
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
package dynamic_programming
func IsValidSudoku(board [][]byte) bool {
for i, row := range board {
for j, cell := range row {
if cell == '.' {
continue
}
if !validate(board, i, j) {
return false
}
}
}
return true
}
func validate(board [][]byte, row int, col int) bool {
indexes := [][]int{{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}}
v := board[row][col]
for i := 0; i < 9; i++ {
if i != row && board[i][col] == v {
return false
}
if i != col && board[row][i] == v {
return false
}
}
getSet := func(v int) int {
if v < 3 {
return 0
} else if v >= 3 && v < 6 {
return 3
}
return 6
}
rowSetStart := getSet(row)
coluntSetStart := getSet(col)
for _, item := range indexes {
if item[0]+rowSetStart != row && item[1]+coluntSetStart != col && board[item[0]+rowSetStart][item[1]+coluntSetStart] == v {
return false
}
}
return true
}