关于数独求解

2024-03-09

有人可以帮我理解吗这个解决方案 http://en.wikipedia.org/wiki/Algorithmics_of_Sudoku#Example_of_a_brute_force_Sudoku_solver_.28in_C.29:

 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
       }
     }
   }
 }

这是一个简单的暴力求解器。它从左上角开始,从左到右逐行工作,尝试将每个可能的数字放入每个方块中,然后使用递归调用继续。一旦失败,它会回溯并尝试不同的替代方案。

该函数称为safe确定当前放置该值是否合法n在某个单元格中,通过检查哪些值已放置在行、列和框中。

这是解决数独的最简单(也是最慢)的方法之一。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关于数独求解 的相关文章

随机推荐