八宫格填数游戏,把一到八填在格子里,横竖都不能重复
发布网友
发布时间:2022-04-22 09:21
我来回答
共2个回答
热心网友
时间:2023-10-25 01:37
<pre t="code" l="python">import itertools
def check(cells):
for r in range(1, 3):
for c in range(2):
if abs(cells[r][c] - cells[r][c + 1]) == 1:
return False
for c in range(3):
for r in range(3):
if abs(cells[r][c] - cells[r + 1][c]) == 1:
return False
for r in range(3):
for c in range(3):
if c != 0:
if abs(cells[r][c] - cells[r + 1][c - 1]) == 1:
return False
if c != 2:
if abs(cells[r][c] - cells[r + 1][c + 1]) == 1:
return False
return True
cells = [[-1 for i in range(3)] for j in range(4)]
for p in itertools.permutations(range(1, 9), 8):
cells[0][1] = p[0]
cells[1][0] = p[1]
cells[1][1] = p[2]
cells[1][2] = p[3]
cells[2][0] = p[4]
cells[2][1] = p[5]
cells[2][2] = p[6]
cells[3][1] = p[7]
if check(cells):
print(cells)暴力O(8!)
热心网友
时间:2023-10-25 01:37
我想要个大案