Generating minimal/irreducible Sudokus

心已入冬 提交于 2019-11-30 20:41:09

I have an idea on the 2nd option your had suggested will be better for that provided you add 3 extra checks for the 1st 17 numbers

  • find a list of 17 random numbers between 1-9
  • add each item at random location provided

    1. these new number added dont fail the 3 basic criteria of sudoku

      • there is no same number in same row
      • there is no same number in same column
      • there is no same number in same 3x3 matrix
    2. if condition 1 fails move to the next column or row and check for the 3 basic criteria again.

    3. if there is no next row (ie at 9th column or 9th row) add to the 1st column once the 17 characters are filled, run you solver logic on this and look for your unique solution.
1''

Here are the main optimizations I implemented with (highly approximate) percentage increases in speed:

  • Using bitmasks to keep track of which constraints (row, column, box) are satisfied in each cell. This makes it much faster to look up whether a placement is legal, but slower to make a placement. A complicating factor in generating puzzles with bitmasks, rather than just solving them, is that digits may have to be removed, which means you need to keep track of the three types of constraints as distinct bits. A small further optimization is to save the masks for each digit and each constraint in arrays. 40%
  • Timing out the generation and restarting if it takes too long. See here. The optimal strategy is to increase the timeout period after each failed generation, to reduce the chance that it goes on indefinitely. 30%, mainly from reducing the worst-case runtimes.
  • mbeckish and user295691's suggestions (see the comments to the original post). 25%
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!