gpt4 book ai didi

python - Codefights, minesweeper, python, code almost working

转载 作者:行者123 更新时间:2023-11-28 18:15:49 24 4
gpt4 key购买 nike

我正在做 codefight 的挑战:扫雷。
描述:
enter image description here

我的代码如下:

def minesweeper(matrix):

for x in range(len(matrix)):
matrix[x].insert(0, "x")
#matrix[x].insert(len(matrix)+2, "x")
frame = ["x" for i in range(len(matrix[0]))]
matrix.insert(0, frame)
matrix.insert(len(matrix), frame)

output_matrix = [[0 for j in range(len(matrix[0]))] for i in range(len(matrix))]

for i in range(0,len(matrix[0])-1):
for j in range(0,len(matrix)-1):
if matrix[i][j] == True:
output_matrix[i][j+1] += 1 # one right
output_matrix[i+1][j] += 1 # one down
output_matrix[i][j-1] += 1 # one left
output_matrix[i-1][j] += 1 # one up
output_matrix[i+1][j+1] += 1 # one down, one right
output_matrix[i+1][j-1] += 1 # one down, one right
output_matrix[i-1][j+1] += 1 # one up, one right
output_matrix[i-1][j-1] +=1 # one up, one left

output_matrix.pop(0)
output_matrix.pop(len(output_matrix)-1)

for y in range(len(output_matrix)):
output_matrix[y].pop(0)
#output_matrix[y].pop(len(output_matrix))
return output_matrix

根据codefight用户的建议,由“x”创建的边界是为了确保如果我在矩阵的边界,炸弹计数不会转移到另一边。
此代码工作正常,直到炸弹位于矩阵的最后一列,例如:

如果输入:

[[False, False, True],
[False, False, False],
[False, False, False]]


输出是:

[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

谁能解释清楚为什么会这样?
如果有人可以建议更好的方法来完成这项任务,我将不胜感激。
提前谢谢你。

最佳答案

我只是颠倒了你的逻辑:我遍历输出字段并从矩阵中添加值。请注意异常的使用(这是关于“x”的提示)。使用此解决方案,您不必使用 pop() 缩小结果。

import itertools 

def minesweeper(matrix):

#create the output matrix first to preserve the size
#underscored variables to prevent warnings about unused variables
output_matrix = [[0 for _j in range(len(matrix[0]))] for _i in range(len(matrix))]

#unchanged
for x in range(len(matrix)):
matrix[x].insert(0, "x")
matrix[x].insert(len(matrix)+2, "x")
frame = ["x" for i in range(len(matrix[0]))]
matrix.insert(0, frame)
matrix.insert(len(matrix), frame)

#to the logics the other way round: count the bombs around the output fields.
#neighyours defines the offsets of all neighouring fields
neighbours = [(-1, -1), (-1, 0), (-1, 1),
( 0, -1), ( 0, 1),
( 1, -1), ( 1, 0), ( 1, 1)]
for i, j in itertools.product(range(len(output_matrix[0])), range(len(output_matrix))):
#get all indices; you could use two for-loops instead of itertools.product...
print(i, j) # just to see how it works... please remove for final version
for offset_i, offset_j in neighbours:
print(" ", offset_i, offset_j ) # just to see how it works... please remove for final version
# the exceptions do the magic here: If you add an "x", a TypeError is raised.
# So you don't do anythithing if this happens. Otherwise you'll add 0 or 1 (adding "True" adds 1, "False" adds 0)
try:
output_matrix[j][i] += matrix[j + offset_j + 1][i + offset_i + 1]
print("result = ", output_matrix[j][i]) # just to see how it works... please remove for final version
except TypeError:
print("pass") # just to see how it works... please remove for final version
pass

return output_matrix

matrix = [[False, False, True], #renamed input variable since "input" is a function name...
[False, False, False],
[False, False, False]]

print(minesweeper(matrix))

一般来说,您的解决方案是有效的(如果您取消注释行 #matrix[x].insert(len(matrix)+2, "x")),但是您在你的 pop() 序列。您可以只使用 2D 切片(请参阅 corresponding stackoverflow topic)并执行

output_matrix = [output_matrix[i][1:len(output_matrix)-1] for i in range(1, len(output_matrix)-1)]

而不是所有的 pop() 步骤。

关于python - Codefights, minesweeper, python, code almost working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481513/

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