-
Notifications
You must be signed in to change notification settings - Fork 0
/
37.go
75 lines (70 loc) · 1.28 KB
/
37.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
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
75
package main
func solveSudoku(board [][]byte) {
backTracking(board, 0, 0)
}
func backTracking(board [][]byte, i, j int) bool {
if i > 8 {
return true
}
if board[i][j] != '.' {
if j == 8 {
return backTracking(board, i+1, 0)
} else {
return backTracking(board, i, j+1)
}
}
for x := 1; x <= 9; x++ {
if checkValid(board, byte('0'+x), i, j) {
board[i][j] = byte('0' + x)
if j == 8 {
if backTracking(board, i+1, 0) {
return true
} else {
board[i][j] = '.'
continue
}
} else {
if backTracking(board, i, j+1) {
return true
} else {
board[i][j] = '.'
continue
}
}
} else {
continue
}
}
return false
}
func checkValid(board [][]byte, num byte, i, j int) bool {
// check Row
hashMap := make(map[byte]bool)
for x := 0; x < 9; x++ {
hashMap[board[i][x]] = true
}
if _, ok := hashMap[num]; ok {
return false
}
// check Col
hashMap = make(map[byte]bool)
for x := 0; x < 9; x++ {
hashMap[board[x][j]] = true
}
if _, ok := hashMap[num]; ok {
return false
}
// check box
h := i / 3
k := j / 3
hashMap = make(map[byte]bool)
for x := 0; x < 3; x++ {
for y := 0; y < 3; y++ {
hashMap[board[h*3+x][k*3+y]] = true
}
}
if _, ok := hashMap[num]; ok {
return false
}
return true
}