本文主要是介绍Golang | Leetcode Golang题解之第37题解数独,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
func solveSudoku(board [][]byte) {var line, column [9][9]boolvar block [3][3][9]boolvar spaces [][2]intfor i, row := range board {for j, b := range row {if b == '.' {spaces = append(spaces, [2]int{i, j})} else {digit := b - '1'line[i][digit] = truecolumn[j][digit] = trueblock[i/3][j/3][digit] = true}}}var dfs func(int) booldfs = func(pos int) bool {if pos == len(spaces) {return true}i, j := spaces[pos][0], spaces[pos][1]for digit := byte(0); digit < 9; digit++ {if !line[i][digit] && !column[j][digit] && !block[i/3][j/3][digit] {line[i][digit] = truecolumn[j][digit] = trueblock[i/3][j/3][digit] = trueboard[i][j] = digit + '1'if dfs(pos + 1) {return true}line[i][digit] = falsecolumn[j][digit] = falseblock[i/3][j/3][digit] = false}}return false}dfs(0)
}
这篇关于Golang | Leetcode Golang题解之第37题解数独的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!