已求解程序不计算野外钻机的编号

2024-05-20 05:28:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用python编程扫雷舰。计算被炸弹包围的区域的数量,我遇到了一个更糟糕的问题-没有错误

我的代码是:

for i in range(len(bombs)):
    numbers[bombs[i]] = -1

    if (bombs[i][0] > 0):
        if (numbers[(bombs[i][0]-step, bombs[i][1]+0)] != -1):
            numbers[(bombs[i][0]-step, bombs[i][1]+0)] = numbers[(bombs[i][0]-step, bombs[i][1]+0)] + 1
            if (bombs[i][1] > 0):
                if (numbers[(bombs[i][0]-step, bombs[i][1]-step)] != -1):
                    numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1
            if (bombs[i][1] < heigth-step-1):
                if (numbers[(bombs[i][0]-step, bombs[i][1]+step)] != -1):
                    numbers[(bombs[i][0]-step, bombs[i][1]+step)] = numbers[(bombs[i][0]-step, bombs[i][1]+step)] + 1

    if (bombs[i][0] < width-step-1):
        if (numbers[(bombs[i][0]+step, bombs[i][1]+0)] != -1):
            numbers[(bombs[i][0]+step, bombs[i][1]+0)] = numbers[(bombs[i][0]+step, bombs[i][1]+0)] + 1
            if (bombs[i][1] > 0):
                if (numbers[(bombs[i][0]+step, bombs[i][1]-step)] != -1):
                    numbers[(bombs[i][0]+step, bombs[i][1]-step)] = numbers[(bombs[i][0]+step, bombs[i][1]-step)] + 1
            if (bombs[i][1] < heigth-step-1):
                if (numbers[(bombs[i][0]+step, bombs[i][1]+step)] != -1):
                    numbers[(bombs[i][0]+step, bombs[i][1]+step)] = numbers[(bombs[i][0]+step, bombs[i][1]+step)] + 1

    if (bombs[i][1] > 0):
        if (numbers[(bombs[i][0]+0, bombs[i][1]-step)] != -1):
            numbers[(bombs[i][0]+0, bombs[i][1]-step)] = numbers[(bombs[i][0]+0, bombs[i][1]-step)] + 1

    if (bombs[i][1] < heigth-step-1):
        if (numbers[(bombs[i][0]+0, bombs[i][1]+step)] != -1):
            numbers[(bombs[i][0]+0, bombs[i][1]+step)] = numbers[(bombs[i][0]+0, bombs[i][1]+step)] + 1

step = size of the fields

bombs = all bombs in an array

谢谢 [1] :https://i.stack.imgur.com/FOxi4.png


Tags: 代码in区域for数量ifstep编程
1条回答
网友
1楼 · 发布于 2024-05-20 05:28:14

我认为这段代码中存在一个问题:

            if (bombs[i][0] > 0):
                # This bomb is not in the leftmost column of the grid
        #        print(numbers[(bombs[i][0]-step, bombs[i][1]+0)])
                if (numbers[(bombs[i][0]-step, bombs[i][1]+0)] != -1):
                    # The cell to the left is not a bomb, so add one to its count
                    numbers[(bombs[i][0]-step, bombs[i][1]+0)] = numbers[(bombs[i][0]-step, bombs[i][1]+0)] + 1

                    if (bombs[i][1] > 0):
                        # This bomb is not in the top row of the grid.
                        if (numbers[(bombs[i][0]-step, bombs[i][1]-step)] != -1):
                            # The cell above and to the left is not a bomb, so add one to its count
                            numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1

请注意,我们只查看炸弹上方和左侧的单元格,如果它不是炸弹则添加一个,如果左侧的单元格也不是炸弹。这是不正确的:对于任何炸弹,我们需要检查上面和左边的电池,看它左边的电池是否也是炸弹

您要做的是从上面的第三个if语句中删除一个缩进级别:

            if (bombs[i][0] > 0):
                # This bomb is not in the leftmost column of the grid
        #        print(numbers[(bombs[i][0]-step, bombs[i][1]+0)])
                if (numbers[(bombs[i][0]-step, bombs[i][1]+0)] != -1):
                    # The cell to the left is not a bomb, so add one to its count
                    numbers[(bombs[i][0]-step, bombs[i][1]+0)] = numbers[(bombs[i][0]-step, bombs[i][1]+0)] + 1

                # This line will now be reached if there is another bomb to the left of bombs[i].
                if (bombs[i][1] > 0):
                    # This bomb is not in the top row of the grid.
                    if (numbers[(bombs[i][0]-step, bombs[i][1]-step)] != -1):
                        # The cell above and to the left is not a bomb, so add one to its count
                        numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1

您还可以对其他三个if语句进行相同的更改,这些语句处理将一个添加到其他方向上对角相邻正方形的计数。


查看您的代码,我不确定您在if (bombs[i][1] < heigth-step-1):等条件下是否需要-1。显然,这些检查试图阻止您脱离网格边缘,但是额外的-1是不必要的。我也有点担心step:这等于1吗?您在检查是否从网格的右侧或底部掉落时包含了step,但在从网格的顶部或左侧掉落时没有包含step,因此如果step大于1,您可能会遇到问题。(例如,如果step是3,而bombs[i][0]是1,bombs[i][0] - step将是-2。)

另外,我可以从可读性的角度对您的代码提出一些建议吗?首先,您经常重复bombs[i][0]bombs[i][1]:如果您在循环的其余部分添加行x = bombs[i][0]y = bombs[i][1],您可以编写xy,而不是bombs[i][0]bombs[i][1]。第二,你可以写some_expression += 1,而不是写some_expression = some_expression + 1。进行这些更改将缩短一行,例如

numbers[(bombs[i][0]-step, bombs[i][1]-step)] = numbers[(bombs[i][0]-step, bombs[i][1]-step)] + 1

numbers[(x-step, y-step)] += 1

相关问题 更多 >