Problem p02803 (python)

Fixed submission:
# import bisect
# import copy
# import fractions
# import math
import numpy as np
from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    H, W = map(int, input().split())
    maze = [input() for _ in range(H)]

    def bfs(maze, visited, sy, sx):
        queue = deque([[sy, sx]])

        visited[sy][sx] = 0
        while queue:
            y, x = queue.popleft()
            for dy, dx in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                ny, nx = y + dy, x + dx
                if 0<=ny<H and 0<=nx<W and \
                        maze[ny][nx] == "." and visited[ny][nx] == -1:
                    visited[ny][nx] = visited[y][x] + 1
                    queue.append([ny, nx])
        return np.amax(visited)

    ans=-1
    for i in range(H):
        for j in range(W):
            visited = [[-1] * W for _ in range(H)]

            sy,sx=i,j
            if maze[sy][sx]=='.':
                ans = max(ans,bfs(maze, visited, sy, sx))


    print(ans)
resolve()

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
maze = [input() for _ in range(H)]

# 方針
# まずはスタート地点を固定し、移動回数を最小にして歩いたときの最大移動回数をbfs関数で求める
# ついでスタート地点を動かして、当の最大移動回数の最大値をとる
def bfs(maze, visited, sy, sx):

    visited[sy][sx] = 0
    # queueの要素について、そこから周囲4方向を模索する
    # queueには、これから周囲4方向を模索すべき地点が格納されている
    queue = deque([[sy, sx]])

    while queue:
        y, x = queue.popleft()
        for dy, dx in ([1, 0], [0, 1], [-1, 0], [0, -1]):
            ny, nx = y + dy, x + dx
            if 0 <= ny < H and 0 <= nx < W and \
                    maze[ny][nx] == "." and visited[ny][nx] == -1:
                visited[ny][nx] = visited[y][x] + 1
                queue.append([ny, nx])

    return np.amax(visited)

ans = -1
for i in range(H):
    for j in range(W):
        sy, sx = i, j
        # (i,j)地点に到達するのにかかった歩数を(i,j)成分に記録する用のリストvisited
        visited = [[-1] * W for _ in range(H)]
        if maze[sy][sx] == '.':
            ans = max(ans, bfs(maze, visited, sy, sx))

print(ans)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
maze = [input() for _ in range(H)]

# 方針
# まずはスタート地点を固定し、移動回数を最小にして歩いたときの最大移動回数をbfs関数で求める
# ついでスタート地点を動かして、当の最大移動回数の最大値をとる
def bfs(maze, visited, sy, sx):

    visited[sy][sx] = 0
    # queueの要素について、そこから周囲4方向を模索する
    # queueには、これから周囲4方向を模索すべき地点が格納されている
    queue = deque([[sy, sx]])

    while queue:
        y, x = queue.popleft()
        for dy, dx in ([1, 0], [1, 1], [0, 1], [0, -1]):
            ny, nx = y + dy, x + dx
            if 0 <= ny < H and 0 <= nx < W and \
                    maze[ny][nx] == "." and visited[ny][nx] == -1:
                visited[ny][nx] = visited[y][x] + 1
                queue.append([ny, nx])

    return np.amax(visited)

ans = -1
for i in range(H):
    for j in range(W):
        sy, sx = i, j
        # (i,j)地点に到達するのにかかった歩数を(i,j)成分に記録する用のリストvisited
        visited = [[-1] * W for _ in range(H)]
        if maze[sy][sx] == '.':
            ans = max(ans, bfs(maze, visited, sy, sx))

print(ans)

Buggy submission (verdict: Runtime Error):
# import bisect
# import copy
# import fractions
# import math
import numpy as np
from collections import Counter, deque
# from itertools import accumulate,permutations, combinations,combinations_with_replacement,product

def resolve():
    H, W = map(int, input().split())
    maze = [input() for _ in range(H)]

    def bfs(maze, visited, sy, sx):
        queue = deque([[sy, sx]])

        visited[sy][sx] = 0
        while queue:
            y, x = queue.popleft()
            for dy, dx in ([1, 0], [-1, 0], [0, 1], [0, -1]):
                ny, nx = y + dy, x + dx
                if 0<=ny<H and 0<=nx<W and \
                        maze[ny][nx] == "." and visited[ny][nx] == -1:
                    visited[ny][nx] = visited[y][x] + 1
                    queue.append([ny, nx])
        return np.amax(visited)

    ans=-1
    for i in range(H):
        for j in range(W):
            visited = [[-1] * W for _ in range(H)]

            sy,sx=i,j
            if maze[sy][sx]=='.':
                ans = max(ans,bfs(maze, visited, sy, sx))


    print(ans)
resolve()