简体   繁体   English

创建一个新的数独谜题

[英]Creating a new sudoku puzzle

I have a board of a solved sudoku and I'm trying to randomly replace some of the elements with 0 to make a new puzzle.我有一块已解决的数独棋盘,我正在尝试用 0 随机替换一些元素以制作新的拼图。 How do I do it?我该怎么做?

board = [[2, 1, 3, 4], 
         [3, 4, 1, 2], 
         [1, 2, 4, 3],  
         [4, 3, 2, 1]]

You can use random.sample to find n random coordinates to remove from the board.您可以使用random.sample找到要从板上删除的n 个随机坐标。 For example, the following will set 5 random cells to 0:例如,以下将设置 5 个随机单元格为 0:

>>> import random
>>> empty_cells = random.sample([(i, j) for i in range(4) for j in range(4)], 5)
>>> for row, col in empty_cells:
...     board[row][col] = 0
... 
>>> board
[[2, 0, 3, 0], [3, 4, 0, 2], [1, 2, 0, 3], [4, 0, 2, 1]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM