我在用python做数独,我想随机填充一个有空格的行。你知道吗
<> >我有一个函数来检测空白位置,还有一个函数来生成int来填充。你知道吗比如说,如果一行是[1 , 2 , ?, ?]
当position函数命中第一个'?'
时,random函数应该提供3或4的选择。然后是3,位置函数打第二个'?'
,随机函数应该只提供'4'
的选择。你知道吗
问题是我的结果是错误的:例如,-1
表示空格:
[[-1 -1 3 4]
[ 3 -1 -1 -1]
[ 2 3 -1 -1]
[-1 -1 -1 -1]]
[[1 4 3 4]
[3 4 1 1]
[2 3 4 3]
[3 1 4 2]]
零件代码为:
self.F1 = np.where(self.oriMatrix == -1)
def randgit(self, row):
while True:
digit = randint(1, self.shape)
for col in range(0, self.shape):
if digit == self.pMatrix[row,col]:
break
return digit
def randFill(self):
for mark in range(0, self.F1[0].size):
pos = [self.F1[0][mark], self.F1[1][mark]]
self.pMatrix[pos[0], pos[1]] = self.randgit(pos[0])
pMatrix
这是4x4矩阵,有人能找出我的错误还是有更好的方法?你知道吗
你的描述和不完整的代码很难说清楚你的问题是什么。你知道吗
我猜你在问:
如果这是对你的问题的适当重申,请考虑以下内容:
对我来说,刚刚印出来的是:
我试着在上面的代码中“慢行”,这样你就可以很容易地检查不同的变量是什么,等等。当然,有了很多这样的答案,我们可能会大大缩短它。你知道吗
相关问题 更多 >
编程相关推荐