• 回溯 递归 的回退状态


    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/


    */
  • 相关阅读:
    索引,约束
    C# Dictionary 的几种遍历方法
    唯一性约束和唯一性索引的区别
    JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
    Dictionary学习笔记嵌套Dictionary的遍历与排序(按Key值)(二)
    Dictionary学习笔记Dictionary定义与输出(一)
    集体智慧编程笔记搜索和排序
    emacs键盘映射
    集体智慧编程笔记推荐系统
    使用SRILM训练大的语言模型
  • 原文地址:https://www.cnblogs.com/rsapaper/p/16625773.html
Copyright © 2020-2023  润新知