Problem p03053 (python)

Fixed submission:
from collections import deque

dyx = [(0, 1), (0, -1), (1, 0), (-1, 0)]
H, W = map(int, input().split())
edge = ['#']*(W+2)
field = [edge]
q = deque()

for i in range(H):
    field.append(['#'] + list(input()) + ['#'])
    for j in range(W):
        if field[i+1][j+1] == '#':
            q.append((i+1, j+1, 0))
field.append(edge)
res = 0

while q:
    y, x, lv = q.popleft()
    res = max(lv, res)

    for dy, dx in dyx:
        if field[y+dy][x+dx] == '.':
            field[y+dy][x+dx] = '#'
            q.append((y+dy, x+dx, lv+1))

print(res)

Buggy submission (verdict: Runtime Error):
from collections import deque
import sys
input = sys.stdin.readline

dyx = ((0, 1), (0, -1), (1, 0), (-1, 0))
H, W = map(int, input().split())
edge = ['#'] * (W + 2)
visited = [H+W]*(H*W)
field = [edge]
q = deque()

for i in range(H):
    line = input()
    field.append(['#'] + list(line) + ['#'])
    for j in range(W):
        if line[j] == '#':
            q.append((i + 1, j + 1, 0))

field.append(edge)
res = 0

while q:
    y, x, lv = q.popleft()

    if 0 <= y+1 < H and visited[(y+1)*W+x] > lv:
        visited[(y+1)*W+x] = lv
        q.append((y+1, x, lv+1))

    if 0 <= y-1 < H and visited[(y-1)*W+x] > lv:
        visited[(y-1)*W+x] = lv
        q.append((y - 1, x, lv + 1))

    if 0 <= x+1 < W and visited[y*W+x+1] > lv:
        visited[y*W+x+1] = lv
        q.append((y, x+1, lv + 1))

    if 0 <= x-1 < W and visited[y*W+x-1] > lv:
        visited[y*W+x-1] = lv
        q.append((y, x-1, lv + 1))

print(max(visited))

Buggy submission (verdict: Runtime Error):
from collections import deque

dyx = ((0, 1), (0, -1), (1, 0), (-1, 0))
H, W = map(int, input().split())
visited = [0]*(H*W)
q = deque()

for i in range(H):
    line = input()
    for j in range(W):
        if line[j] == '#':
            q.append((i, j, 0))
            visited[i*H+j] = 1

res = 0

while q:
    y, x, lv = q.popleft()
    if lv > res:
        res = lv

    for dy, dx in dyx:
        ny, nx = y+dy, x+dx
        if 0 <= ny < H and 0 <= nx < W and visited[ny*H+nx] == 0:
            visited[ny*H+nx] = 1
            q.append((ny, nx, lv+1))

print(res)

Buggy submission (verdict: Runtime Error):

H, W = map(int, input().split())
G = [list(input()) for _ in range(H)]
ans = 0


def change(x, y):
    if x < 0 or x >= W:
        return False
    if y < 0 or y >= H:
        return False

    if G[x][y] == ".":
        G[x][y] = "@"
        return False
    elif G[x][y] == "#":
        return True


def search(x, y):
    global ans
    count = 0
    sharp = 0

    while sharp == 0:
        if count == 0:
            G[x][y] = "@"
        else:
            for i in range(count+1):
                if change(x+count-i, y+1) or change(x-i, y+count-i) or \
                    change(x-count+i, y-i) or change(x+i, y-count+i):
                    sharp += 1

        if count > ans:
            ans = count

        count += 1


for i in range(H):
    for j in range(W):
        if G[i][j] == ".":
            search(i, j)

print(ans)

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


def change(x, y):
    if x < 0 or x >= W:
        return False
    if y < 0 or y >= H:
        return False

    if G[x][y] == ".":
        G[x][y] = "@"
        return False
    elif G[x][y] == "#":
        return True


def search(x, y):
    global ans
    count = 0
    sharp = 0

    while sharp == 0:
        if count == 0:
            G[x][y] = "@"
        else:
            for i in range(count+1):
                if change(x+count-i, y+1) or change(x-i, y+count-i) or \
                    change(x-count+i, y-i) or change(x+i, y-count+i):
                    sharp += 1

        if count > ans:
            ans = count

        count += 1


for i in range(H):
    for j in range(W):
        if G[i][j] == ".":
            search(j, i)

print(ans)