Dynamic generate sudoku board table using javascript

后端 未结 1 1553
我在风中等你
我在风中等你 2021-01-26 01:42

I am trying to to generate a Sudoku board using this script:

The problem is that I don\'t know how to validate to generate unique numbers on columns and squares.

相关标签:
1条回答
  • 2021-01-26 02:23

    You will need backtracking, because it is possible to add numbers to the Sudoku board which don't violate any rules immediately, but which will lead to a contradiction later on. If you take any unique-solution Sudoku problem and arbitrarily place any number anywhere, you are bound to experience just this.

    I suggest you investigate the Dancing Links algorithm. You can easily formulate Sudoku as a set cover problem, and that algorithm can find a solution if there exists one. For the completely empty board, there has to be a solution. Randomize the matrix if you want to obtain a random result.

    Also investigate all the other sudoku-tagged questions, since you are not the first trying to generate such boards, and translating from one language to another doesn't really change the game that much.

    0 讨论(0)
提交回复
热议问题