-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsudoku-solver.go
160 lines (149 loc) · 2.81 KB
/
sudoku-solver.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import "fmt"
type Memo map[byte]bool
func toBlock(x, y int) int {
return (x/3)*3 + y/3
}
func fill(board [][]byte, lineMemo, columnMemo, blockMemo []Memo,
startX, startY int) bool {
for i := startX; i < 9; i++ {
var j int
if i == startX {
j = startY
} else {
j = 0
}
for ; j < 9; j++ {
if board[i][j] != '.' {
continue
}
for n := byte('1'); n <= byte('9'); n++ {
_, okLine := lineMemo[i][n]
_, okColumn := columnMemo[j][n]
_, okBlock := blockMemo[toBlock(i, j)][n]
if !okLine && !okColumn && !okBlock {
board[i][j] = n
lineMemo[i][n] = true
columnMemo[j][n] = true
blockMemo[toBlock(i, j)][n] = true
good := fill(board, lineMemo, columnMemo, blockMemo, i, j)
if good {
return true
}
board[i][j] = '.'
delete(lineMemo[i], n)
delete(columnMemo[j], n)
delete(blockMemo[toBlock(i, j)], n)
}
}
return false
}
}
return true
}
func solveSudoku(board [][]byte) {
lineMemo := make([]Memo, 9)
columnMemo := make([]Memo, 9)
blockMemo := make([]Memo, 9)
for i := 0; i < 9; i++ {
lineMemo[i] = make(Memo)
columnMemo[i] = make(Memo)
blockMemo[i] = make(Memo)
}
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
x := board[i][j]
if x == '.' {
continue
}
lineMemo[i][x] = true
columnMemo[j][x] = true
blockMemo[toBlock(i, j)][x] = true
}
}
fill(board, lineMemo, columnMemo, blockMemo, 0, 0)
}
func main() {
x := [][]byte{
[]byte("..9748..."),
[]byte("7........"),
[]byte(".2.1.9..."),
[]byte("..7...24."),
[]byte(".64.1.59."),
[]byte(".98...3.."),
[]byte("...8.3.2."),
[]byte("........6"),
[]byte("...2759..")}
solveSudoku(x)
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
fmt.Printf("%s", string(x[i][j]))
}
fmt.Printf("\n")
}
fmt.Println(isValidSudoku(x))
}
func isValid9(arr []byte) bool {
memo := make(map[byte]bool)
for _, b := range arr {
if b == '.' {
continue
}
if _, ok := memo[b]; ok {
return false
} else {
memo[b] = true
}
}
return true
}
func isValidSudoku(board [][]byte) bool {
for i := 0; i < 9; i++ {
if !isValid9(board[i]) {
return false
}
}
for i := 0; i < 9; i++ {
arr := make([]byte, 9)
for j := 0; j < 9; j++ {
arr[j] = board[j][i]
}
if !isValid9(arr) {
return false
}
}
base := [][]int{
[]int{0, 0},
[]int{0, 3},
[]int{0, 6},
[]int{3, 0},
[]int{3, 3},
[]int{3, 6},
[]int{6, 0},
[]int{6, 3},
[]int{6, 6},
}
shift := [][]int{
[]int{0, 0},
[]int{0, 1},
[]int{0, 2},
[]int{1, 0},
[]int{1, 1},
[]int{1, 2},
[]int{2, 0},
[]int{2, 1},
[]int{2, 2},
}
for _, b := range base {
arr := make([]byte, 0)
for _, s := range shift {
i := b[0] + s[0]
j := b[1] + s[1]
arr = append(arr, board[i][j])
}
if !isValid9(arr) {
return false
}
}
return true
}