Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.解数独问题可以做优化 #2392

Open
DeadAndLife opened this issue Jan 3, 2024 · 0 comments
Open

21.解数独问题可以做优化 #2392

DeadAndLife opened this issue Jan 3, 2024 · 0 comments

Comments

@DeadAndLife
Copy link

数独由于是个规则矩阵,所以可以使用/%来实现降维,并且因为降成一维了,我们就可以使用startIndex来实现剪枝(之前填写过的肯定有数字就不需要进循环了)
以下是swift的代码

func backtrace(_ start:Int) -> Bool {
    for i in start..<81 {
        let row = i/9
        let col = i%9
        guard board[row][col] == "." else { continue }
        for j in 1...9 {
            let val = Character("\(j)")
            if isVaild(row, col, val, board) {
                board[row][col] = val
                if backtrace(i+1) { return true }
                board[row][col] = "."
            }
        }
        return false
    }
    return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant