超时优化呜呜呜

P11228 [CSP-J 2024] 地图探险

ShiShen_hu @ 2024-11-01 20:39:42

例9 超时,其他都过了,有大佬看看还能怎么优化吗

def P11228():
    step_number = 1
    n, m, cs = map(int, input().split())
    x, y, d = map(int, input().split())
    matrix = []
    for i in range(n):
        matrix.append([i for i in input()])

    matrix[x-1][y-1] = None
    while cs > 0:
        cs -= 1
        x_ = (x+1) if d == 1 else (x-1) if d == 3 else x
        y_ = (y+1) if d == 0 else (y-1) if d == 2 else y

        if 1 <= x_ <= n and 1 <= y_ <= m :
            if matrix[x_-1][y_-1] == ".":
                step_number += 1
                x,  y = x_, y_
                matrix[x-1][y-1] = None
                continue

            elif not matrix[x_-1][y_-1]:
                x, y = x_, y_
                continue

        d = (d + 1) % 4

    return step_number

num = int(input())
n = []
while num > 0:
    n.append(P11228())
    num -= 1

for i in n:
    print(i)

|