Generating minimal/irreducible Sudokus

前端 未结 2 1274
悲哀的现实
悲哀的现实 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%
    0 讨论(0)
  • 2021-01-05 08:48

    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.
    0 讨论(0)
提交回复
热议问题