回溯 递归 的回退状态
func solveSudoku(board [][]byte) {
var line, column [9][9]bool
var block [3][3][9]bool
var spaces [][2]int
for 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] = true
column[j][digit] = true
block[i/3][j/3][digit] = true
}
}
}
var dfs func(int) bool
dfs = 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] = true
column[j][digit] = true
block[i/3][j/3][digit] = true
board[i][j] = digit + '1'
if dfs(pos + 1) {
return true
}
line[i][digit] = false
column[j][digit] = false
block[i/3][j/3][digit] = false
}
}
return false
}
dfs(0)
}
/*
ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)
. 46
/ 47
0 48
9 57
*/
/*
37. 解数独 - 力扣(LeetCode) https://leetcode.cn/problems/sudoku-solver/
解数独 - 解数独 - 力扣(LeetCode) https://leetcode.cn/problems/sudoku-solver/solution/jie-shu-du-by-leetcode-solution/
*/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2020-08-25 12 | 服务注册与发现如何满足服务治理
2020-08-25 阿里研究员:警惕软件复杂度困局
2018-08-25 交互设计师的任务是创造持续的、合理的、优雅的使用体验
2018-08-25 读写剪切板 系统 剪切板 navigator.clipboard.writeText(inputVal)
2018-08-25 小程序禁止下拉空白出现
2017-08-25 事件调度 单调时钟的值(以小数秒为单位),即不能倒退的时钟。时钟不受系统时钟更新的影响
2017-08-25 广告 竞价排名 import Levenshtein as Le seqratio_res = Le.seqratio(chk_name_lsit, cmp_)