Problem p03053 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

H,W = map(int,input().split())
INF = 10**18
q = []

visited = [[True]*(W+2)]
visited += [[True] + [x == '#' for x in input().rstrip()] + [True] for _ in range(H)]
visited.append([True]*(W+2))
for i in range(1,H+1):
  for j in range(1,W+1):
    if visited[i][j]:
      q.append((i,j))

moves = [(0,1),(0,-1),(1,0),(-1,0)] #全方向調べる

ans = -1
while q:
  qq = []
  ans += 1
  for x,y in q:
    if not visited[x-1][y]:
      visited[x-1][y] = True
      qq.append((x-1,y))
    if not visited[x+1][y]:
      visited[x+1][y] = True
      qq.append((x+1,y))
    if not visited[x][y-1]:
      visited[x][y-1] = True
      qq.append((x,y-1))
    if not visited[x][y+1]:
      visited[x][y+1] = True
      qq.append((x,y+1))
  q = qq
  
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
H, W = map(int, input().split())
a_list = []
dark = deque()
for h in range(H):
    a = input()
    a_list.append(a)
    for w in range(W):
        if a[w] == '#':
            dark.append([h, w])
ans = 0
distance = [[-1] * W for _ in range(H)]
while dark:
    h, w = dark.popleft()
    for x, y in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
        hx, wy = h + x, w + y
        if (0 <= hx < H) & (0 <= wy < W):
            if distance[hx][wy] == -1:
                distance[hx][wy] = distance[h][w] + 1
                ans = max(ans, distance[hx][wy])
                tmp.append([hx, wy])
    dark = tmp
print(ans + 1)

Buggy submission (verdict: Runtime Error):
from queue import Queue

def bfs(start):
    global visited, time
    while start.qsize() > 0:
        hh, ww = start.get()
        time[hh][ww] = calc_min(hh, ww)
        visited[hh][ww] = 1
        h_w_list = [[hh + 1, ww], [hh, ww + 1], [hh - 1, ww], [hh, ww - 1]]
        for h_, w_ in h_w_list:
            if (h_ in range(h)) & (w_ in range(w)):
                if visited[h_][w_] == 0:
                    start.put([h_, w_])

def calc_min(hh, ww):
    h_w_list = [[hh + 1, ww], [hh, ww + 1], [hh - 1, ww], [hh, ww - 1]]
    for h_, w_ in h_w_list:
        if (h_ in range(h)) & (w_ in range(w)):
            min_num = min(time[hh][ww], time[h_][w_] + 1)
    return min_num

inf = 10 ** 9
h, w = map(int, input().split())
a = []
for hh in range(h):
    a.append(list(input()))
time = [[inf for _ in range(w)] for _ in range(h)]
start = Queue()
visited = [[0 for _ in range(w)] for _ in range(h)]
for hh in range(h):
    for ww in range(w):
        if a[hh][ww] == "#":
            start.put([hh, ww])
            time[hh][ww] = 0
bfs(start)
ans = max([max(c) for c in time])
print(ans)

Buggy submission (verdict: Runtime Error):
from queue import Queue
h, w = map(int, input().split())
map_list = []
for _ in range(h):
    map_list.append(list(input()))
x_list = [1, 0, -1, 0]
y_list = [0, 1, 0, -1]
black_num = sum([map_list[i].count("#") for i in range(h)])
ans = 0
q = Queue()
q1 = Queue()
for y in range(h):
    for x in range(w):
        if map_list[y][x] == "#":
            q.put([y, x])
while (black_num < (h*w)):
    q_list = []
    for j in range(q.qsize()):
        y, x = q.get()
        if temp[y][x] == "#":
            for i in range(4):
                new_y = y + y_list[i]
                new_x = x + x_list[i]
                if (new_y >= 0) & (new_y < h) & (new_x >= 0) & (new_x < w):
                    if map_list[new_y][new_x] == ".":
                        map_list[new_y][new_x] = "#"
                        black_num += 1
                        q_list.append([new_y, new_x])
    q = Queue()
    for qq in q_list:
        q.put(qq)
    ans += 1
print(ans)