扫雷c语言代码:代码战斗 扫雷 python 代码几乎工作

关于扫雷c语言代码的问题,在codefights中经常遇到,关于代码战斗 扫雷 python 代码几乎工作的编程代码示例如下。

I'm doing codefight's challange: minesweeper.
The description:
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]]

Output is:
[[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]

任何人都可以清楚地解释为什么会发生这种情况?
如果有人可以提出更好的方法来完成此任务,我将不胜感激。
提前感谢您。

1

我只是颠倒了你的逻辑:我走过输出字段并从 matrix 添加值。请注意异常的使用 (那是关于“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()步骤。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(160)
In spur:pythonspurssh-使用set命令
上一篇
As tr:theadtr>* 含义
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(60条)