Python 优化我的战舰战场验证器4x4,并修复错误

Python 优化我的战舰战场验证器4x4,并修复错误,python,Python,我是stackoverflow的新手,最近我试图用Python 3x解决codewar中的一个问题,它是关于在10x10网格上验证战舰位置的。我通过了33次测试,17次失败,这很糟糕,加上我有时会出现超时错误,所以我想我的代码一定有问题,我愿意听取任何意见,有人能帮我吗 def validate_battlefield(field): # important list comprehension !!!! to add element in a sublist infront and b

我是stackoverflow的新手,最近我试图用Python 3x解决codewar中的一个问题,它是关于在10x10网格上验证战舰位置的。我通过了33次测试,17次失败,这很糟糕,加上我有时会出现超时错误,所以我想我的代码一定有问题,我愿意听取任何意见,有人能帮我吗

def validate_battlefield(field):
    # important list comprehension !!!! to add element in a sublist infront and behind
    field = [[0] + k + [0] for k in field]
    extended_field = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
    extended_field.extend(field)
    extended_field.extend([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
    field = extended_field
    # print(extended_field)

    list_of_battleship = []
    for h in range(1, 11):
        for w in range(1, 11):
            if field[h][w] == 1:
                m, n = h, w
                print("*****************")
                print(h, n)
                length_ship = 1
                field[m][n] = -1
                if field[h + 1][w] == 0 and field[h][w + 1] == 0:
                    list_of_battleship.append(1)
                    break

                while field[m][n + 1] + field[m + 1][n] + field[m][n-1] != 2:
                    if field[m][n + 1] == 1:
                        print(m, n + 1)
                        length_ship += 1
                        field[m][n + 1] = -1
                        n += 1
                        if field[m][n + 1] == 0:
                            list_of_battleship.append(length_ship)
                            break
                    if field[m + 1][n] == 1:
                        print(m + 1, n)
                        length_ship += 1
                        field[m + 1][n] = -1
                        m += 1
                        if field[m + 1][n] == 0:
                            list_of_battleship.append(length_ship)
                            break

    list_of_battleship.sort()
    if list_of_battleship == [1, 1, 1, 1, 2, 2, 2, 3, 3, 4]:
        return True
    else:
        return False
我无法正确进行以下验证,我真的不知道哪里出了问题

print(validate_battlefield([
[1, 0, 0, 0, 0, 1, 1, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 1, 1, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],]
))
print(validate_battlefield(
    [
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0]
    ]
))

你能解释一下[m][n+1]+字段[m+1][n]+字段[m][n-1]时
的目的吗2
?这是一个似乎永远挂起的检查。因为我的算法是从左到右的,所以这意味着如果我遇到一个值为1的插槽,那么该点将是m,n。因为我不能让两个1从一边连接到另一边,它从m,n向上,从m,n向下加上值。如果它是2,这意味着有两个1连接在一起,这是不允许的,因为战列舰只能是一条直线。非常感谢。