Problem p03053 (python)

Fixed submission:
from collections import deque



def bfs(maze, visited, queue, H, W):
    count = 0
    while queue:
        y, x = queue.popleft()
        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_y, new_x = y + i, x + j
            if -1 < new_y < H and -1 < new_x < W:
                if maze[new_y][new_x] == "." and visited[new_y][new_x] == -1:
                    visited[new_y][new_x] = visited[y][x] + 1
                    queue.append([new_y, new_x])

    ans = -1
    for i in range(H):
        for j in range(W):
            ans = max(ans,visited[i][j])
    return ans

H, W = map(int,input().split())

maze = [input() for i in range(H)]
visited = [[-1] * W for i in range(H)]

queue = deque([])
for i in range(H):
    for j in range(W):
        if maze[i][j] == "#":
            visited[i][j] = 0
            queue.append([i, j])

print(bfs(maze, visited, queue, H, W))

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

from collections import deque

queue = deque()
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            queue.append([j, i, 0])
            seen[j][i] = 0

def bfs(maze, seen):
    while queue:
        y, x, c = queue.popleft()
        for i, j in [1, 0], [-1, 0], [0, 1], [0, -1]:
            dy = y + i
            dx = x + j
            if dy < 0 or H <= dy or dx < 0 or W <= dx:
                continue
            if seen[dy][dx] > -1:
                continue
            if maze[dy][dx] == '#':
                continue
            seen[dy][dx] = c + 1
            queue.append([dy, dx, c+1])

def check_max(seen):
    ans = 0
    for i in range(H):
        for j in range(W):
            ans = max(ans, seen[i][j])
    return ans
    
bfs(maze, seen)
print(check_max(seen))

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

from collections import deque

queue = deque()
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            queue.append([j, i, 0])
            seen[j][i] = 0

def bfs(maze, seen):
    while queue:
        y, x, c = queue.popleft()
        for i, j in [1, 0], [-1, 0], [0, 1], [0, -1]:
            dy = y + i
            dx = x + j
            if dy < 0 or H <= dy or dx < 0 or W <= dx:
                continue
            if seen[dy][dx] > -1:
                continue
            if maze[dy][dx] == '#':
                continue
            seen[dy][dx] = c + 1
            queue.append([dy, dx, c+1])

def check_max(seen):
    ans = 0
    for i in range(H):
        for j in range(W):
            ans = max(ans, seen[i][j])
    return ans
    
bfs(maze, seen)
print(check_max(seen))

Buggy submission (verdict: Runtime Error):
H, W = map(int,input().split())

tile = []

for i in range(H):
    tile.append(list(input()))

ans = 0

def check_black(arr, H, W):
    for i in range(H):
        for j in range(W):
            if arr[i][j] != "#":
                return 0
    return 1

def black_list(arr, H, W):
    black_arr = []
    for i in range(H):
        for j in range(W):
            if arr[i][j] == "#":
                black_arr.append([i,j])
    return black_arr

while(check_black(tile,H,W) == 0):
    black_arr = black_list(tile, H, W)
    for i in range(len(black_arr)):
        if 0 < black_arr[i][0] and tile[black_arr[i][0]-1][black_arr[i][1]] != "#":
            tile[black_arr[i][0]-1][black_arr[i][1]] = "#"
        if black_arr[i][0] < H - 1 and tile[black_arr[i][0]+1][black_arr[i][1]] != "#":
            tile[black_arr[i][0]+1][black_arr[i][1]] = "#"
        if 0 < black_arr[i][1] and tile[black_arr[i][0]][black_arr[i][1]-1] != "#":
            tile[black_arr[i][0]][black_arr[i][1]-1] = "#"
        if black_arr[i][1] < H - 1 and tile[black_arr[i][0]][black_arr[i][1]+1] != "#":
            tile[black_arr[i][0]][black_arr[i][1]+1] = "#"
    ans+=1
    #print("rep = " + str(ans))
    #for x in range(H):
    #    for y in range(W):
    #        print(tile[x][y],end="")
    #    print("")

print(ans)