-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathleetcode0037.go
62 lines (51 loc) · 1.38 KB
/
leetcode0037.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
/*
LeetCode 37: https://leetcode.com/problems/sudoku-solver/
*/
package leetcode
func solveSudoku(board [][]byte) {
rows, cols, blocks := make([]map[byte]bool, 9), make([]map[byte]bool, 9), make([]map[byte]bool, 9)
for i := 0; i < 9; i++ {
rows[i], cols[i], blocks[i] = make(map[byte]bool), make(map[byte]bool), make(map[byte]bool)
}
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
if board[i][j] != '.' {
blockIndex, n := (i/3)*3+j/3, board[i][j]
rows[i][n], cols[j][n], blocks[blockIndex][n] = true, true, true
}
}
}
helper37(board, 0, 0, rows, cols, blocks)
}
func helper37(board [][]byte, i, j int, rows, cols, blocks []map[byte]bool) bool {
if i == 9 {
return true
}
nextI, nextJ := i, j+1
if j == 8 {
nextI, nextJ = i+1, 0
}
if board[i][j] != '.' {
return helper37(board, nextI, nextJ, rows, cols, blocks)
}
done := false
blockIndex := (i/3)*3 + j/3
for n := byte('1'); n <= byte('9') && !done; n++ {
_, existRows := rows[i][n]
_, existCols := cols[j][n]
_, existBlocks := blocks[blockIndex][n]
if !existRows && !existCols && !existBlocks {
rows[i][n], cols[j][n], blocks[blockIndex][n] = true, true, true
board[i][j] = n
done = helper37(board, nextI, nextJ, rows, cols, blocks)
if done {
return true
}
board[i][j] = '.'
delete(rows[i], n)
delete(cols[j], n)
delete(blocks[blockIndex], n)
}
}
return false
}