Nas*_*ine 4 c algorithm sudoku
有人可以帮我理解这个解决方案:
Initialize 2D array with 81 empty grids (nx = 9, ny = 9)
Fill in some empty grid with the known values
Make an original copy of the array
Start from top left grid (nx = 0, ny = 0), check if grid is empty
if (grid is empty) {
assign the empty grid with values (i)
if (no numbers exists in same rows & same columns same as (i) & 3x3 zone (i) is currently in)
fill in the number
if (numbers exists in same rows & same columns same as (i) & 3x3 zone (i) is currently in)
discard (i) and repick other values (i++)
}
else {
while (nx < 9) {
Proceed to next row grid(nx++, ny)
if (nx equals 9) {
reset nx = 1
proceed to next column grid(nx,ny++)
if (ny equals 9) {
print solution
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 14
这是一个简单的蛮力解算器.它从左上角开始,逐行左右工作,尝试将每个可能的数字放入每个方块,然后继续使用递归调用.失败后,它会回溯并尝试不同的选择.
调用的函数通过检查已在行,列和框中放置了哪些值safe来确定将值n放在某个单元格中是否合法.
这是解决数独的最简单(也是最慢)的方法之一.
| 归档时间: |
|
| 查看次数: |
1148 次 |
| 最近记录: |