【问题标题】:Codefights, minesweeper, python, code almost workingCodefights,扫雷,python,代码几乎可以工作
【发布时间】:2018-01-27 23:10:58
【问题描述】:

我正在做 codefight 的挑战:扫雷。
说明:

我的代码如下:

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]]

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

【问题讨论】:

  • 您停在len(matrix[0])-1 以避免出现OOB 错误,但这样做也可以避免最后一列。您需要更细粒度的范围控制,以便您仍然可以检查最后一列,但在这样做时不会更新其右侧的任何内容。
  • 不,他并没有提前停下来,因为他在...周围添加了一个 x 边框
  • 我得到 IndexError 与此代码。好像三边加了边框,右边没有加边框。
  • 如果我修复了无法正确添加和删除边框单元格的代码,这将正常工作。您可以通过使用 append 来避免一些数学运算来找到最后一项,并且不给 pop 提供任何参数。
  • 我在回答中举了一个例子,说明如何在不使用 pop() 的情况下移除边框。

标签: python minesweeper


【解决方案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() 步骤。

【讨论】:

  • 不,因为 [-1] 是一个有效的索引(从右数)... ;-)
  • 您修复了坏边框,但 OP 有一个简单的 if,您可以将异常处理用作“常规”代码逻辑。我不喜欢这样,但在专门为它设计的 python 中它并没有那么糟糕。
  • @KennyOstrom 我认为它没有问题。与每次检查边界相比,它会产生更易读的代码和更符合逻辑的流程。它也不太容易出现错误。你不喜欢它的什么?
  • 正如我所说,在大多数语言中使用异常作为正常控制是一个坏主意,python 是一个异常。我试图提出这个警告点。
  • 我添加了一条注释,即只有 pop() 应该被修复并提供 2D 切片作为替代...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多