Problem p03053 (python)

Fixed submission:
from collections import deque

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

Map = [[] for _ in range(H)]
reached = [[False] * W for _ in range(H)]
q = deque()
cnt = 0
count = 0
tempList = []
for y in range(H):
    for i, x in enumerate(input()):
        Map[y].append(x)
        if x == "#":
            cnt += 1
            reached[y][i] = True
            if i + 1 < W and not reached[y][i + 1]:
                reached[y][i + 1] = True
                tempList.append((i + 1, y))

            if i > 0 and not reached[y][i - 1]:
                reached[y][i - 1] = True
                tempList.append((i - 1, y))

            if y + 1 < H and not reached[y + 1][i]:
                reached[y + 1][i] = True
                tempList.append((i, y + 1))

            if y > 0 and not reached[y - 1][i]:
                reached[y - 1][i] = True
                tempList.append((i, y - 1))
if cnt==H*W:
    print(0)
    exit()

q.append(tempList)


def BFS(blist):
    global count
    tList = []

    count += 1

    for (x, y) in blist:
        if x+1<W and not reached[y][x + 1]:
            reached[y][x + 1] = True
            tList.append((x + 1, y))

        if x-1>=0 and not reached[y][x - 1]:
            reached[y][x - 1] = True
            tList.append((x - 1, y))

        if y+1<H and not reached[y + 1][x]:
            reached[y + 1][x] = True
            tList.append((x, y + 1))

        if y-1>=0 and not reached[y - 1][x]:
            reached[y - 1][x] = True
            tList.append((x, y - 1))

    if tList:
        q.append(tList)


while q:
    BFS(q.popleft())

print(count)

Buggy submission (verdict: Runtime Error):

from collections import deque
import numpy as np
import sys

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

Map = [[] for _ in range(H)]
reached = [[False] * W for _ in range(H)]
q = deque()
cnt = 0
count = 0
tempList = []
for y in range(H):
    for i, x in enumerate(input()):
        Map[y].append(x)
        if x == "#":
            cnt += 1
            reached[y][i] = True
            if i + 1 < W and not reached[y][i + 1]:
                reached[y][i + 1] = True
                tempList.append((i + 1, y))

            if i - 1 >= 0 and not reached[y][i - 1]:
                reached[y][i - 1] = True
                tempList.append((i - 1, y))

            if y + 1 < H and not reached[y + 1][i]:
                reached[y + 1][i] = True
                tempList.append((i, y + 1))

            if y - 1 >= 0 and not reached[y - 1][i]:
                reached[y - 1][i] = True
                tempList.append((i, y - 1))

q.append(tempList)


def BFS(blist):
    global count
    global cnt
    tList = []

    count += 1

    for (x, y) in blist:
        reached[y][x] = True
        Map[y][x] = "#"

        if x+1<W and not reached[y][x + 1]:
            tList.append((x + 1, y))

        if x-1>=0 and not reached[y][x - 1]:
            tList.append((x - 1, y))

        if y+1<H and not reached[y + 1][x]:
            tList.append((x, y + 1))

        if y-1>=0 and not reached[y - 1][x]:
            tList.append((x, y - 1))

    # if np.array(reached).all():
    #     print(count)
    #     sys.exit()
    if tList:
        q.append(tList)


while q:
    BFS(q.popleft())

print(count)

Buggy submission (verdict: Runtime Error):

from collections import deque
import numpy as np
import sys

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

Map = [[] for _ in range(H)]
reached = [[-1] * W for _ in range(H)]
q = deque()
cnt = 0
count = 0
tempList = []
for y in range(H):
    for i, x in enumerate(input()):
        Map[y].append(x)
        if x == "#":
            cnt += 1
            reached[y][i] = 0
            tempList.append((i + 1, y, -1))
            tempList.append((i - 1, y, -1))
            tempList.append((i, y + 1, -1))
            tempList.append((i, y - 1, -1))

q.append(tempList)


def BFS(blist):
    global count
    global cnt
    tList = []

    count += 1

    for (x, y, pre) in blist:
        if x >= W or y >= H or x < 0 or y < 0 or reached[y][x]>=0:
            continue

        reached[y][x] = pre + 1
        Map[y][x] = "#"

        tList.append((x + 1, y, pre + 1))
        tList.append((x - 1, y, pre + 1))
        tList.append((x, y + 1, pre + 1))
        tList.append((x, y - 1, pre + 1))

    if np.where(np.array(reached)>=0, True, False).all():
        print(count)
        sys.exit()

    q.append(tList)


while q:
    BFS(q.popleft())

Buggy submission (verdict: Runtime Error):
# from collections import deque
# import numpy as np
# import sys
#
# H, W = map(int, input().split())
#
# Map = [[] for _ in range(H)]
# reached = [[False]*W for _ in range(H)]
# q = deque()
# cnt = 0
# count = 0
# tempList = []
# for y in range(H):
#     for i, x in enumerate(input()):
#         Map[y].append(x)
#         if x=="#":
#             reached[y][i] = True
#             if i + 1 < W and not reached[y][i + 1]:
#                 tempList.append((i + 1, y))
#
#             if i - 1 >= 0 and not reached[y][i - 1]:
#                 tempList.append((i - 1, y))
#
#             if y + 1 < H and not reached[y + 1][i]:
#                 tempList.append((i, y + 1))
#
#             if y - 1 >= 0 and not reached[y - 1][i]:
#                 tempList.append((i, y - 1))
#
# q.append(tempList)
#
# def BFS(blist):
#     global count
#     tList = []
#
#     count += 1
#
#     for (x, y) in blist:
#         reached[y][x] = True
#         Map[y][x] = "#"
#         if x+1<W and not reached[y][x + 1]:
#             tList.append((x + 1, y))
#
#         if x-1>=0 and not reached[y][x - 1]:
#             tList.append((x - 1, y))
#
#         if y+1<H and not reached[y + 1][x]:
#             tList.append((x, y + 1))
#
#         if y-1>=0 and not reached[y - 1][x]:
#             tList.append((x, y - 1))
#
#     if np.array(reached).all():
#         print(count)
#         sys.exit()
#
#     q.append(tList)
#
# while q:
#     BFS(q.popleft())

from collections import deque
import numpy as np
import sys

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

Map = [[] for _ in range(H)]
reached = [[False] * W for _ in range(H)]
q = deque()
cnt = 0
count = 0
tempList = []
for y in range(H):
    for i, x in enumerate(input()):
        Map[y].append(x)
        if x == "#":
            cnt += 1
            reached[y][i] = True
            tempList.append((i + 1, y))
            tempList.append((i - 1, y))
            tempList.append((i, y + 1))
            tempList.append((i, y - 1))

q.append(tempList)


def BFS(blist):
    global count
    global cnt
    tList = []

    count += 1

    for (x, y) in blist:
        if x >= W or y >= H or x < 0 or y < 0 or reached[y][x]:
            continue

        reached[y][x] = True
        Map[y][x] = "#"
        cnt += 1

        tList.append((x + 1, y))
        tList.append((x - 1, y))
        tList.append((x, y + 1))
        tList.append((x, y - 1))

    if cnt == H * W:
        print(count)
        sys.exit()

    q.append(tList)


while q:
    BFS(q.popleft())

Buggy submission (verdict: Runtime Error):
from collections import deque
import numpy as np
import sys

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

Map = [[] for _ in range(H)]
reached = [[False]*W for _ in range(H)]
q = deque()
cnt = 0
count = 0
tempList = []
for y in range(H):
    for i, x in enumerate(input()):
        Map[y].append(x)
        if x=="#":
            reached[y][i] = True
            if i + 1 < W and not reached[y][i + 1]:
                tempList.append((i + 1, y))

            if i - 1 >= 0 and not reached[y][i - 1]:
                tempList.append((i - 1, y))

            if y + 1 < H and not reached[y + 1][i]:
                tempList.append((i, y + 1))

            if y - 1 >= 0 and not reached[y - 1][i]:
                tempList.append((i, y - 1))

q.append(tempList)

def BFS(blist):
    global count
    tList = []

    count += 1

    for (x, y) in blist:
        reached[y][x] = True
        Map[y][x] = "#"
        if x+1<W and not reached[y][x + 1]:
            tList.append((x + 1, y))

        if x-1>=0 and not reached[y][x - 1]:
            tList.append((x - 1, y))

        if y+1<H and not reached[y + 1][x]:
            tList.append((x, y + 1))

        if y-1>=0 and not reached[y - 1][x]:
            tList.append((x, y - 1))

    if np.array(reached).all():
        print(count)
        sys.exit()

    q.append(tList)

while q:
    BFS(q.popleft())