gpt4 book ai didi

python - 试图找出一种使用 3x3 矩阵比较 9x9 矩阵中数字的方法

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:15 25 4
gpt4 key购买 nike

因此,我正在尝试解决一个问题,该问题让您比较 9x9 数独网格中的数字,以便查看数独游戏是否具有有效/可解的数字网格(这意味着数独规则适用于给定网格)。我几乎已经解决了这个问题,但我仍然停留在最后一部分。我已经弄清楚如何对每列/行上的每个元素求和,但我无法完全解决这个问题,直到我可以实际检查每个 3x3 网格以查看它们中是否没有重复数字。这就是我卡住的地方,因为我似乎无法获得正确的算法以 3x3 的方式遍历矩阵。

我试图通过使用一系列增加特定索引号的 for 循环来完全控制迭代,以便它沿着矩阵移动。让我知道我做错了什么——或者是否有任何其他可能的、更优雅的方法来解决这个问题(使用这么多 for 循环使我的代码看起来又长又难看/效率低下)。

def sudoku(grid):

grid = [[1,3,2,5,4,6,9,8,7],
[4,6,5,8,7,9,3,2,1],
[7,9,8,2,1,3,6,5,4],
[9,2,1,4,3,5,8,7,6],
[3,5,4,7,6,8,2,1,9],
[6,8,7,1,9,2,5,4,3],
[5,7,6,9,8,1,4,3,2],
[2,4,3,6,5,7,1,9,8],
[8,1,9,3,2,4,7,6,5]]
duplicate = set()
numHolder = 0
for a in range(0,9):
for b in range(0,9):
numHolder+=grid[b][a]
if numHolder!=45:
return False
numHolder=0
for b in range(0,9):
for x in range(0, 9):
numHolder += grid[b][x]
if numHolder != 45:
return False
numHolder = 0

for b in range(0,3):
for c in range(0,3):
if grid[b][c] in duplicate:
return False
else:
duplicate.add(grid[b][c])
duplicate.clear()
for d in range(0,3):
for e in range(0,3):
if grid[d][c+3] in duplicate:
return False
else:
duplicate.add(grid[d][c+3])

duplicate.clear()
for d in range(0,3):
for e in range(0,3):
if grid[b][c+6] in duplicate:
return False
else:
duplicate.add(grid[d][c+6])
duplicate.clear()
for d in range(0,3):
for e in range(0,3):
if grid[d+3][c] in duplicate:
return False
else:
duplicate.add(grid[d+3][c])

duplicate.clear()
for d in range(0,3):
for e in range(0,3):
if grid[d+3][c+3] in duplicate:
return False
else:
duplicate.add(grid[d+3][c+3])
duplicate.clear()
for d in range(0,3):
for e in range(0,3):
if grid[d+3][c+6] in duplicate:
return False
else:
duplicate.add(grid[d+3][c+6])
duplicate.clear()
for d in range(0,3):
for e in range(0,3):
if grid[d+6][c] in duplicate:
return False
else:
duplicate.add(grid[d+6][c])

duplicate.clear()
for d in range(0,3):
for e in range(0,3):
if grid[d+6][c+3] in duplicate:
return False
else:
duplicate.add(grid[d+6][c+3])

duplicate.clear()
for d in range(0,3):
for e in range(0,3):
if grid[d+6][c+6] in duplicate:
return False
else:
duplicate.add(grid[d+6][c+6])
return True

最佳答案

这在 NumPy 中可以很容易地解决:

import numpy as np
import skimage

grid = [[1,3,2,5,4,6,9,8,7],
[4,6,5,8,7,9,3,2,1],
[7,9,8,2,1,3,6,5,4],
[9,2,1,4,3,5,8,7,6],
[3,5,4,7,6,8,2,1,9],
[6,8,7,1,9,2,5,4,3],
[5,7,6,9,8,1,4,3,2],
[2,4,3,6,5,7,1,9,8],
[8,1,9,3,2,4,7,6,5]]

# Create NumPy array
grid = np.array(grid)
# Cut into 3x3 sublocks, and flatten each subblock
subgrids = skimage.util.view_as_blocks(grid, (3, 3)).reshape(3, 3, -1)

# Sort each subblock then compare each one with [1, 2, ..., 9]. If all are equal, it is a valid subblock
valid_blocks = np.all(np.sort(subgrids, axis=-1) == np.arange(1, 10), axis=-1)
# array([[ True, True, True],
# [ True, True, True],
# [ True, True, True]])

# Sort rows, then compare each one
valid_rows = np.all(np.sort(grid, axis=1) == np.arange(1, 10), axis=1)
# array([ True, True, True, True, True, True, True, True, True])

# Sort columns, then compare each one
valid_columns = np.all(np.sort(grid, axis=0) == np.arange(1, 10)[:, None], axis=0)
# array([ True, True, True, True, True, True, True, True, True])

# Check if all comparisons were all true
all_valid = np.all(valid_blocks) & np.all(valid_rows) & np.all(valid_columns)
# True

关于python - 试图找出一种使用 3x3 矩阵比较 9x9 矩阵中数字的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51114445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com