Problem p02855 (python)

Fixed submission:
h, w, k = map(int, input().split())
num = 0
cakes = []
for _ in range(h):
    row = input()
    a_row = []
    for r in row:
        if r=='.':
            a_row.append(r)
        elif r=='#':
            num+=1
            a_row.append(num)
    cakes.append(a_row)

for x in range(h):
    for y in range(w):
        if cakes[x][y] == '.':
            continue
        else:
            val = cakes[x][y]
            nx = x-1
            while(nx>=0)and(cakes[nx][y] == '.') :
                cakes[nx][y] = val
                nx -= 1
            nx = x+1
            while (nx<=h-1)and(cakes[nx][y] == '.'):
                cakes[nx][y] = val
                nx += 1

for x in range(h):
    for y in range(w):
        if cakes[x][y] == '.':
            continue
        else:
            val = cakes[x][y]
            ny = y-1
            while (ny>=0)and(cakes[x][ny]=='.'):
                cakes[x][ny] = val
                ny -= 1
            ny = y+1
            while(ny<=w-1)and(cakes[x][ny] == '.'):
                cakes[x][ny] = val
                ny += 1

for c in cakes:
    for s in c:
        print(str(s) + ' ', end='')
    print('')

Buggy submission (verdict: Runtime Error):
import numpy as np
h, w, k = map(int, input().split())
num = 0
cakes = []
for _ in range(h):
    row = input()
    a_row = []
    for r in row:
        if r=='.':
            a_row.append(r)
        elif r=='#':
            num+=1
            a_row.append(num)
    cakes.append(a_row)

cakes = np.array(cakes)

for x in range(h):
    for y in range(w):
        if cakes[x, y] == '.':
            continue
        else:
            val = cakes[x, y]
            nx = x-1
            while(nx>=0)and(cakes[nx, y] == '.') :
                cakes[nx, y] = val
                nx -= 1
            nx = x+1
            while (nx<=h-1)and(cakes[nx, y] == '.'):
                cakes[nx, y] = val
                nx += 1

for x in range(h):
    for y in range(w):
        if cakes[x, y] == '.':
            continue
        else:
            val = cakes[x, y]
            ny = y-1
            while (ny>=0)and(cakes[x, ny]=='.'):
                cakes[x, ny] = val
                ny -= 1
            ny = y+1
            while(ny<=w-1)and(cakes[x, ny] == '.'):
                cakes[x, ny] = val
                ny += 1

for c in cakes:
    for s in c:
        print(str(s) + ' ', end='')
    print('')

Buggy submission (verdict: Runtime Error):
import numpy as np
h, w, k = map(int, input().split())
num = 0
cakes = []
for _ in range(h):
    row = input()
    a_row = []
    for r in row:
        if r=='.':
            a_row.append(r)
        elif r=='#':
            num+=1
            a_row.append(num)
    cakes.append(a_row)

cakes = np.array(cakes)
#print(cakes)
#inds = []
#for i in range(1, k+1):
    #ind = np.where(cakes==str(i))
    #inds.append((ind[0][0], ind[1][0]))
#print(inds)

for x in range(h):
    for y in range(w):
        if cakes[x, y] == '.':
            continue
        else:
            val = cakes[x, y]
            nx = x-1
            while(nx>=0)and(cakes[nx, y] == '.') :
                cakes[nx, y] = val
                nx -= 1
            nx = x+1
            while (nx<=h-1)and(cakes[nx, y] == '.'):
                cakes[nx, y] = val
                nx += 1

for x in range(h):
    for y in range(w):
        if cakes[x, y] == '.':
            continue
        else:
            val = cakes[x, y]
            ny = y-1
            while (ny>=0)and(cakes[x, ny]=='.'):
                cakes[x, ny] = val
                ny -= 1
            ny = y+1
            while(ny<=w-1)and(cakes[x, ny] == '.'):
                cakes[x, ny] = val
                ny += 1
for c in cakes:
    for s in c:
        print(s + ' ', end='')
    print('')