Unique methods to generate sudoku puzzle [duplicat

2019-03-26 12:45发布

问题:

This question already has an answer here:

  • How to generate Sudoku boards with unique solutions 7 answers

How many possible unique ways are there to generate a Sudoku Puzzle?? I can think of only two possible ways 1) Take a solved Sudoku puzzle and shuffle the rows and columns 2) Generate a random number and check if it violates any Sudoku constraints, repeat untill number does not violate any Sudoku constraint for every square(theoretically possible but normally it leads to deadlocking )

Are there any other ways?

回答1:

Here is a 20-page PDF, titled "Sudoku Puzzles Generating: from Easy to Evil", that you'd probably find useful in your quest.

To answer your question:

Are there any other ways?

Yes. Yes there are.



回答2:

Simple way to generate up to Take a Solved Sudoko puzzle, step 1 ) replace all 1 with A , 2 with B till 9 with I, Step 2) do a shuffle in each horizontal and vertical block block of using a random between 1 and 3, here in each there can only be 3 possible combinations. step 3) now shuffle the block there can only be 3 vertical and 3 horizontal shuffle step 4) rotate the block 1 to 4 time.. step 5) mirror the puzzle vertically and horizontally using a random between 1 and 2. step 6) replace all A with any number 1 to 9..

guessing this will produce around 38,093,690,880 combos....



回答3:

This would work.

void genSudokuBoard(int grid[ ], int display[ ]){
int i,c, j, rowNum, colNum, blockNum;

for(c=0; c<N*N; c++) {
   blockNum = colNum = 1;
   //rowNum = c / N;
     //colNum = c % N;
     //blockNum = (rowNum / 3) * 3 + (colNum / 3);
     for (j=0; j<N; j++)
     printf("%d", grid[((blockNum/3)*N*3) + (colNum/3)*3 + (j/3)*N + j%3]);
   }


printf("\n");
for(i=0; i<N*N; i++) {  /* displaying all N*N numbers in the 'grid' array */

  if(i%N==0 && i!=0) { /* printing a newline for every multiple of N */
     printf("\n");
  }
  printf("%d ", grid[i]);
}
printf("\n");

return 0;

}