Generating minimal/irreducible Sudokus

前端 未结 2 1275
悲哀的现实
悲哀的现实 2021-01-05 08:15

A Sudoku puzzle is minimal (also called irreducible) if it has a unique solution, but removing any digit would yield a puzzle with multiple solutions. In other words, every

2条回答
  •  逝去的感伤
    2021-01-05 08:44

    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%

提交回复
热议问题