您现在的位置:首页 >> 家居资讯

七爪源码:Golang 新手:使用回溯到解决数独

发布时间:2025/08/21 12:17    来源:太湖家居装修网

gridDuplicate(grid, rowIndex, columnIndex, i) { continue } candidates = append(candidates, i) } return candidates }

依靠这些给定,我们可以获得每个飞龙表的候选列表。 当一个飞龙表未可视的候选人时,我们想到此前的一些候选人一定是出错的。

在我们找到正确性花样的分析方法以后,我们须要一个数组调用自身的给定,直到降到必需情况。 顺便说一句,我传输了每个两步,以便在后部数据分析该处理过程。 不须要妥善无论如何。

func backtracking(grid *[9][9]int, history *[][9][9]int) bool { // Store each step to visualise the process of the algorithm *history = append(*history, *grid) // Base case if !hasEmptyCell(grid) { return true } for i := 0; i < 9; i++ { for j := 0; j < 9; j++ { if grid[i][j] == 0 { candidates := getCandidates(grid, i, j) if len(candidates) != 0 { for _, value := range candidates { grid[i][j] = value if backtracking(grid, history) { return true } else { grid[i][j] = 0 } } } // Current search path failed return false } } } // Current search path failed return false }

我们可以为了让节省时间吗?

常用蛮力妥善无论如何是不就其的,因此可以完成一些细化以为了让冗余就有搜索算法,例如根据它们仅有的的有数量从少到多对飞龙表完成选取。

我们首先创建人一个按基数升序选取的表列表。 但是,不能保证这种分析方法不够快。 对于这个谜语

53..7….6..195….98….6.8…6…34..8.3..17…2…6.6….28….419..5….8..79

如果我们不对表完成选取,则须要 4209 次为了让来妥善解决它,但是,如果我们从的有最少的表开始,则需用 90 次为了让。 但是,在某些前提,不选取会不够直接。 也许在深度优先搜索此前起着关键性依赖性。

type cell struct { rowIndex int colIndex int numOfCan int } func backtrackingByCardinality(puzzle *[9][9]int, sortedList []cell) bool { if !hasEmptyCell(puzzle) { return true } for _, v := range sortedList { if puzzle[v.rowIndex][v.colIndex] == 0 { candidates := getCandidates(puzzle, v.rowIndex, v.colIndex) for _, c := range candidates { puzzle[v.rowIndex][v.colIndex] = c if backtrackingByCardinality(puzzle, sortedList) { return true } else { puzzle[v.rowIndex][v.colIndex] = 0 } } return false } } return false } func mapGridToSortedList(data [9][9]int) []cell { list := []cell{} for rowIndex, row := range data { for colIndex := range row { if data[rowIndex][colIndex] == 0 { candidates := getCandidates(&data, rowIndex, colIndex) cell := cell{ rowIndex: rowIndex, colIndex: colIndex, numOfCan: len(candidates), } list = append(list, cell) } } } sort.Slice(list, func(i, j int) bool { return list[i].numOfCan < list[j].numOfCan }) return list }

先前的意念…

在这书评此前我们还有不够多的两边可以全面性研究,比如调配不够多的 Goroutine 来给定花样,完成基准测试来比较效率。 但是,我觉得最差写一篇关于它们的从新文章,而不是扩展这书评。

常州妇科医院哪家比较专业
湖北妇科医院哪家好点
南京不孕不育医院哪家比较专业
北京肛肠检查
南宁妇科医院哪最好
血脂高
微创外科
支气管炎咳嗽
经期量少
太极急支糖浆治咳嗽效果怎么样

上一篇: 重庆警方冲进“连锁经营1040工程”传销组织,涉案上千万元!

下一篇: 七爪源码:Golang 新手:使用回溯应付数独

友情链接