我的代码如下:
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
" x"创建的边框,正如codefight用户所建议的那样,是为了确保如果我的是在矩阵的边界,炸弹数量不会转移到另一个侧。
此代码可正常工作,直到炸弹位于矩阵的最后一列,例如:
如果输入:
[[False, False, True],
[False, False, False],
[False, False, False]]
输出为:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
有人能解释清楚为什么会这样吗?
如果有人可以建议更好的方法来完成这项任务,我会很感激。
提前谢谢。
答案 0 :(得分:1)
我刚刚颠倒了你的逻辑:我遍历输出字段并从矩阵中添加值。请注意使用例外(这是关于“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() - 步骤。