Problem p02855 (python)

Fixed submission:
import numpy as np
inputs = open(0).readlines()

def main(inputs):
    h, w, k = map(int, inputs[0].split())
    cake, count, coords = np.zeros((h, w), dtype=np.uint), 0, {}
    for i, s in enumerate(inputs[1:]):
        for j, c in enumerate(s):
            if c == '#':
                count += 1
                cake[i, j] = count
                coords[count] = (j, i)
    empty_rows = {i for i, rows in enumerate(cake) if not np.any(rows)}

    for i in range(1, count+1):
        x, y = coords[i]
        x0, y0 = x-1, y-1
        x1, y1 = x+1, y+1
        while 0 <= x0 and cake[y, x0] == 0:
            cake[y, x0] = i
            x0 -= 1
        x0 += 1
        while x1 < w and cake[y, x1] == 0:
            cake[y, x1] = i
            x1 += 1
        while 0 <= y0 and y0 in empty_rows:
            cake[y0, x0:x1] = i
            y0 -= 1
        while y1 < h and y1 in empty_rows:
            cake[y1, x0:x1] = i
            y1 += 1

    for rows in cake:
        print(*rows)

main(inputs)

Buggy submission (verdict: Runtime Error):
import numpy as np
inputs = open(0).readlines()

def main(inputs):
    h, w, k = map(int, inputs[0].split())
    cake, count, coord = np.zeros((h, w), dtype=np.uint), 0, {}
    for i, s in enumerate(inputs[1:]):
        for j, c in enumerate(s):
            if c == '#':
                count += 1
                cake[i, j] = count
                coords[count] = (j, i)
    empty_rows = {i for i, rows in enumerate(cake) if not np.any(rows)}

    for i in range(1, count+1):
        x, y = coords[i]
        x0, y0 = x-1, y-1
        x1, y1 = x+1, y+1
        while 0 <= x0 and cake[y, x0] == 0:
            cake[y, x0] = i
            x0 -= 1
        x0 += 1
        while x1 < w and cake[y, x1] == 0:
            cake[y, x1] = i
            x1 += 1
        while 0 <= y0 and y0 in empty_rows:
            cake[y0, x0:x1] = i
            y0 -= 1
        while y1 < h and y1 in empty_rows:
            cake[y1, x0:x1] = i
            y1 += 1

    for rows in cake:
        print(*rows)

main(inputs)

Buggy submission (verdict: Runtime Error):
import numpy as np
inputs = open(0).readlines()

def main(inputs):
    h, w, k = map(int, inputs[0].split())
    cake, count, coord = np.zeros((h, w), dtype=np.int), 0, {}
    for i, s in enumerate(inputs[1:]):
        for j, c in enumerate(s):
            if c == '#':
                count += 1
                cake[i, j] = count
                coords[count] = (j, i)

    for i in range(1, count+1):
        x, y = coords[i]
        x0, y0 = x-1, y-1
        x1, y1 = x+1, y+1
        while 0 <= x0 and cake[y, x0] == 0:
            cake[y, x0] = i
            x0 -= 1
        x0 += 1
        while x1 < w and cake[y, x1] == 0:
            cake[y, x1] = i
            x1 += 1
        while 0 <= y0 and not np.any(cake[y0, x0:x1]):
            cake[y0, x0:x1] = i
            y0 -= 1
        while y1 < h and not np.any(cake[y1, x0:x1]):
            cake[y1, x0:x1] = i
            y1 += 1

    for rows in cake:
        print(*rows)

main(inputs)

Buggy submission (verdict: Runtime Error):
import numpy as np
inputs = open(0).readlines()

h, w, k = map(int, inputs[0].split())
cake = np.zeros((h, w), dtype=np.int)
count = 0
coords = {}
for i, s in enumerate(inputs[1:]):
    for j, c in enumerate(s):
        if c == '#':
            count += 1
            cake[i, j] = count
            coords[count] = (j, i)
            
for i in range(1, count+1):
    x, y = coords[i]
    x0, y0 = x-1, y-1
    x1, y1 = x+1, y+1
    while 0 <= x0 and cake[y, x0] == 0:
        cake[y, x0] = i
        x0 -= 1
    x0 += 1
    while x1 < w and cake[y, x1] == 0:
        cake[y, x1] = i
        x1 += 1
    while 0 <= y0 and np.all(cake[y0, x0:x1] == 0):
        cake[y0, x0:x1] = i
        y0 -= 1
    while y1 < h and np.all(cake[y1, x0:x1] == 0):
        cake[y1, x0:x1] = i
        y1 += 1

for rows in cake:
    print(*rows)

Buggy submission (verdict: Runtime Error):
import numpy as np
inputs = open(0).readlines()

for i in range(1, count+1):
    x, y = coords[i]
    x0, y0 = x-1, y-1
    x1, y1 = x+1, y+1
    while 0 < x0 and cake[y0, x0] == 0:
        cake[y, x0] = i
        x0 -= 1
    while x1 < w and cake[y0, x1] == 0:
        cake[y, x1] = i
        x1 += 1
    while 0 < y0 and np.all(cake[y0, x0:x1] == 0):
        cake[y0, x0:x1] = i
        y0 -= 1
    while y1 < h and np.all(cake[y1, x0:x1] == 0):
        cake[y1, x0:x1] = i
        y1 += 1

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
import numpy as np
inputs = sys.stdin.readlines()

h, w, k = map(int, inputs[0].split())
r = iter(range(1, k+1))
A = [list(map(lambda y: 0 if y == '.' else next(r), x)) for x in inputs[1:]]
B = np.array(A)

def f(x, y, val, dx0, dx1, dy0, dy1, search=True):
    if search and 0 <= x+dx0-1 < w and B[y, x+dx0-1] == 0:
        B[y, x+dx0-1] = val
        search = f(x, y, val, dx0-1, dx1, dy0, dy1)
    elif search and 0 <= x+dx1+1 < w and B[y, x+dx1+1] == 0:
        B[y, x+dx1+1] = val
        search = f(x, y, val, dx0, dx1+1, dy0, dy1)
    elif search and 0 <= y+dy0-1 < h and np.all(B[y+dy0-1, x+dx0:x+dx1+1] == 0):
        B[y+dy0-1, x+dx0:x+dx1+1] = val
        search = f(x, y, val, dx0, dx1, dy0-1, dy1)
    elif search and 0 <= y+dy1+1 < h and np.all(B[y+dy1+1, x+dx0:x+dx1+1] == 0):
        B[y+dy1+1, x+dx0:x+dx1+1] = val
        search = f(x, y, val, dx0, dx1, dy0, dy1+1)
    return False

traveled = set()
for y in range(h):
    for x in range(w):
        if B[y, x] != 0 and B[y, x] not in traveled:
            dummy = f(x, y, B[y, x], 0, 0, 0, 0)
            traveled.add(B[y, x])
for b in B:
    print(*b)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
inputs = sys.stdin.readlines()

h, w, k = map(int, inputs[0].split())
r = iter(range(1, k+1))
A = [list(map(lambda y: 0 if y == '.' else next(r), x)) for x in inputs[1:]]
B = np.array(A)

def f(x, y, val, dx0, dx1, dy0, dy1, search=True):
    if search and 0 <= x+dx0-1 < w and B[y, x+dx0-1] == 0:
        B[y, x+dx0-1] = val
        search = f(x, y, val, dx0-1, dx1, dy0, dy1)
    elif search and 0 <= x+dx1+1 < w and B[y, x+dx1+1] == 0:
        B[y, x+dx1+1] = val
        search = f(x, y, val, dx0, dx1+1, dy0, dy1)
    elif search and 0 <= y+dy0-1 < h and np.all(B[y+dy0-1, x+dx0:x+dx1+1] == 0):
        B[y+dy0-1, x+dx0:x+dx1+1] = val
        search = f(x, y, val, dx0, dx1, dy0-1, dy1)
    elif search and 0 <= y+dy1+1 < h and np.all(B[y+dy1+1, x+dx0:x+dx1+1] == 0):
        B[y+dy1+1, x+dx0:x+dx1+1] = val
        search = f(x, y, val, dx0, dx1, dy0, dy1+1)
    return False

traveled = set()
for y in range(h):
    for x in range(w):
        if B[y, x] != 0 and B[y, x] not in traveled:
            dummy = f(x, y, B[y, x], 0, 0, 0, 0)
            traveled.add(B[y, x])
for b in B:
    print(*b)

Buggy submission (verdict: Runtime Error):
import sys
inputs = sys.stdin.readlines()

h, w, k = map(int, inputs[0].split())
r = iter(range(1, k+1))
A = [list(map(lambda y: 0 if y == '.' else next(r), x)) for x in inputs[1:]]
B = np.array(A)

def f(x, y, val, dx0, dx1, dy0, dy1, search=True):
    if search and 0 <= x+dx0-1 < w and B[y, x+dx0-1] == 0:
        B[y, x+dx0-1] = val
        search = f(x, y, val, dx0-1, dx1, dy0, dy1)
    elif search and 0 <= x+dx1+1 < w and B[y, x+dx1+1] == 0:
        B[y, x+dx1+1] = val
        search = f(x, y, val, dx0, dx1+1, dy0, dy1)
    elif search and 0 <= y+dy0-1 < h and np.all(B[y+dy0-1, x+dx0:x+dx1+1] == 0):
        B[y+dy0-1, x+dx0:x+dx1+1] = val
        search = f(x, y, val, dx0, dx1, dy0-1, dy1)
    elif search and 0 <= y+dy1+1 < h and np.all(B[y+dy1+1, x+dx0:x+dx1+1] == 0):
        B[y+dy1+1, x+dx0:x+dx1+1] = val
        search = f(x, y, val, dx0, dx1, dy0, dy1+1)
    return False

traveled = set()
for y in range(h):
    for x in range(w):
        if B[y, x] != 0 and B[y, x] not in traveled:
            dummy = f(x, y, B[y, x], 0, 0, 0, 0)
            traveled.add(B[y, x])
for b in B:
    print(*b)