gpt4 book ai didi

python - 检查矩阵中 "b"周围的值

转载 作者:行者123 更新时间:2023-11-30 23:39:24 25 4
gpt4 key购买 nike

还是扫雷。我找到了一种方法来做到这一点,但我知道必须有一种简化的方法来做到这一点。我必须在矩阵中放置一个数字来表示它周围有多少炸弹(“b”)。这就是我所拥有的,我知道必须有更短的方法。

def check(y,x):
if ((y < 0) or (y >= len(mat1)) or (x < 0) or (x >= len(mat1))):
return (False)
else:
return mat1[y][x]

def addscores():
for x in range(len(mat1)):
for y in range(len(mat1)):
if mat1[y][x] != "b":
if check(y-1,x-1) == "b" or check(y,x-1) == "b" or check(y+1,x-1) == "b" or check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) =="b":
mat1[y][x] = 1
if check(y-1,x-1) == "b":
if check(y,x-1) == "b" or check(y+1,x-1) == "b" or check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y,x-1) == "b":
if check(y+1,x-1) == "b" or check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y+1,x-1) == "b":
if check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y+1,x) == "b":
if check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y+1,x+1) == "b":
if check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y,x+1) == "b":
if check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 2
if check(y-1,x+1) == "b":
if check(y-1,x) == "b":
mat1[y][x] = 2
if check(y-1,x-1) == "b":
if check(y,x-1) == "b":
if check(y+1,x-1) == "b" or check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y+1,x-1) == "b":
if check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y+1,x) == "b":
if check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y+1,x+1) == "b":
if check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y,x+1) == "b":
if check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 3
if check(y-1,x-1) == "b":
if check(y,x-1) == "b":
if check(y+1,x-1) == "b":
if check(y+1,x) == "b" or check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 4
if check(y+1,x-1) == "b":
if check(y+1,x) == "b":
if check(y+1,x+1) == "b" or check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 4
if check(y+1,x) == "b":
if check(y+1,x+1) == "b":
if check(y,x+1) == "b" or check(y-1,x+1) == "b" or check(y-1,x) == "b":
mat1[y][x] = 4
#ETC

最佳答案

def check(y,x):
if ((y < 0) or (x < 0) or (y >= len(mat1)) or (x >= len(mat1))):
return False
else:
if mat1[y][x] == 'b':
return 1
else:
return 0

def check_all(y,x):
if mat1[y][x] != 'b':
return sum([check(y + yy, x + xx) for xx in range(-1,2) for yy in range(-1,2)])
else:
return 'b'

def addscores():
for x in range(len(mat1)):
for y in range(len(mat1)):
mat1[y][x] = check_all(y,x)

如果我正确理解了您所做的事情,那么这段代码(尤其是 check_all 函数)应该可以解决您的问题。你是对的,肯定有一种更短的方法来做到这一点,使用循环(或在本例中是列表推导式),而不是必须单独写出每个检查。

我尽可能保留了您的代码,因为我没有足够的上下文来知道进行更改是否会破坏任何内容。

关于python - 检查矩阵中 "b"周围的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13572866/

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