Skip to content

Commit 69791b5

Browse files
aQuaaQua
aQua
authored and
aQua
committed
adding Problem 37
改变了block的填写顺序,先填写比较满的block,还是失败
1 parent 689449e commit 69791b5

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

Algorithms/0037.sudoku-solver/sudoku-solver.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
package Problem0037
22

33
func solveSudoku(board [][]byte) {
4-
nums := []byte("519783426")
4+
nums := []byte("123456789")
5+
blocks := []int{1, 3, 5, 7, 0, 8, 4, 2, 6}
56
for _, n := range nums {
6-
if !fillBlock(board, n, 0) {
7+
if !fillBlock(board, n, blocks) {
78
panic("此题无解")
89
}
910
}
1011
}
1112

12-
func fillBlock(board [][]byte, n byte, block int) bool {
13-
print(board, n, block)
14-
15-
if block == 9 {
13+
func fillBlock(board [][]byte, n byte, blocks []int) bool {
14+
if len(blocks) == 0 {
1615
return true
1716
}
1817

18+
block := blocks[0]
19+
20+
print(board, n, block)
21+
1922
rowZero := (block / 3) * 3
2023
colZero := (block % 3) * 3
2124

@@ -32,7 +35,7 @@ func fillBlock(board [][]byte, n byte, block int) bool {
3235
}
3336

3437
if had() {
35-
return fillBlock(board, n, block+1)
38+
return fillBlock(board, n, blocks[1:])
3639
}
3740
// 检查(r,c)所在的行和列是否已经存在 b 了
3841
isClear := func(r, c int) bool {
@@ -48,7 +51,7 @@ func fillBlock(board [][]byte, n byte, block int) bool {
4851
for c := colZero; c < colZero+3; c++ {
4952
if board[r][c] == '.' && isClear(r, c) {
5053
board[r][c] = n
51-
if fillBlock(board, n, block+1) {
54+
if fillBlock(board, n, blocks[1:]) {
5255
return true
5356
}
5457
// 后面的填写不成功,所以需要还原这个格子

0 commit comments

Comments
 (0)