Problem p02855 (python)

Fixed submission:
h,w,k = map(int,input().split())

cake = []
for i in range(h):
    x = list(input())
    cake.append(x)

ans = [[0]*w for _ in range(h)]

c = 1
for i in range(h):
    if cake[i].count('#') == 1:
        for j in range(w):
            ans[i][j] += c
        c = min(c+1,k)
    elif '#' in cake[i]:
        ss = c
        s = cake[i].count('#')
        for j in range(w):
            if cake[i][j] =='.':
                ans[i][j] = c
            else:
                ans[i][j] = c
                c = min(c+1,ss+s-1)
        c = c+1

j = 0
while not '#' in cake[j]:
    j += 1

for l in range(j):
    ans[l] = ans[j]

for i in range(h):
    if min(ans[i]) == 0:
        ans[i] = ans[i-1]

for i in range(h):
    print(*(ans[i]))

Buggy submission (verdict: Runtime Error):
h,w,k = map(int,input().split())

cake = []
for i in range(h):
    x = list(input())
    cake.append(x)

ans = [[0]*w for _ in range(h)]

raw_ind = []

c = 1
for i in range(h):
    if cake[i].count('#') == 1:
        for j in range(w):
            ans[i][j] += c
        c = min(c+1,k)
    elif '#' in cake[i]:
        for j in range(w):
            if cake[i][j] =='.':
                ans[i][j] = c
            else:
                ans[i][j] = c
                c = min(c+1,k)
    else:
        raw_ind.append(i)

for x in raw_ind:
    j = 0
    while (x+j) in raw_ind and x+j <= h-1:
        j += 1
    if x+j == h:
        break
    for l in range(j):
        ans[x+l] = ans[x+j]

raw_ind = sorted(raw_ind, reverse =True)

for x in raw_ind:
    j = 0
    while (x-j) in raw_ind and x+j >= 0:
        j +=1
    if x+j == -1:
        break
    for l in range(j):
        if x-j >= 0:
            ans[x+l] = ans[x-j]


for i in range(h):
    print(*(ans[i]))

Buggy submission (verdict: Runtime Error):
h,w,k = map(int,input().split())

cake = []
for i in range(h):
    x = list(input())
    cake.append(x)

ans = [[0]*w for _ in range(h)]

raw_ind = []

c = 1
for i in range(h):
    if cake[i].count('#') == 1:
        for j in range(w):
            ans[i][j] += c
        c += 1
    elif '#' in cake[i]:
        for j in range(w):
            if cake[i][j] =='.':
                ans[i][j] = c
            else:
                ans[i][j] = c
                c += 1
    else:
        raw_ind.append(i)

for x in raw_ind:
    j = 0
    while (x+j) in raw_ind and x+j <= h-1:
        j += 1
    if x+j == h:
        break
    for l in range(j):
        ans[x+l] = ans[x+j]

raw_ind = sorted(raw_ind, reverse =True)

for x in raw_ind:
    j = 0
    while (x-j) in raw_ind and x+j >= 0:
        j +=1
    if x+j == -1:
        break
    for l in range(j):
        if x-j >= 0:
            ans[x+l] = ans[x-j]

print(ans)

Buggy submission (verdict: Runtime Error):
h,w,k = map(int,input().split())

cake = []
for i in range(h):
    x = list(input())
    cake.append(x)

ans = [[0]*w for _ in range(h)]

raw_ind = []

c = 0
for i in range(h):
    if cake[i].count('#') == 1:
        c += 1
        for j in range(w):
            ans[i][j] += c
    elif '#' in cake[i]:
        for j in range(w):
            if cake[i][j] =='.':
                ans[i][j] = c
            else:
                ans[i][j] = c
    else:
        raw_ind.append(i)

for x in raw_ind:
    j = 0
    while (x+j) in raw_ind and x+j <= h-1:
        j += 1
    if x+j == h:
        break
    for l in range(j):
        ans[x+l] = ans[x+j]

raw_ind = sorted(raw_ind, reverse =True)

for x in raw_ind:
    j = 0
    while (x-j) in raw_ind and x+j >= 0:
        j +=1
    if x+j == -1:
        break
    for l in range(j):
        if x-j >= 0:
            ans[x+l] = ans[x-j]

print(ans)