Problem p02855 (python)

Fixed submission:
import sys
sys.setrecursionlimit(500000)

h, w, k = map(int, input().split())
s = []
ans = [[0] * w for _ in range(h)]
cnt = 0
#prev = 1

for i in range(h):
    s.append(input())

#print(s)

for i in range(h):
    if '#' not in s[i]:
        continue
    shouldCover = False

    for j in range(w):
        if j == 0 and s[i][j] == '.':
            cnt += 1
            ans[i][j] = cnt
            shouldCover = True
        elif s[i][j] == '#' and shouldCover:
            ans[i][j] = cnt
            shouldCover = False
        elif s[i][j] == '#':
            cnt += 1
            ans[i][j] = cnt
            shouldCover = False
        else:
            ans[i][j] = cnt

def solve(cur, nxt, down):
    if nxt < 0 or nxt > h-1:
        return
    if down:
        if '#' in s[nxt]:
            for k in range(cur, nxt):
                for j in range(w):
                    ans[k][j] = ans[nxt][j]
        else:
            solve(cur, nxt+1, down)
    else:
        if '#' in s[nxt]:
            for k in range(cur, nxt, -1):
                for j in range(w):
                    ans[k][j] = ans[nxt][j]
        else:
            solve(cur, nxt-1, down)


for i in range(h):
    if all(ans[i][j] == 0 for j in range(w)):
        if i == h - 1:
            solve(i, i-1, False)
        else:
            #print('down!')
            solve(i, i+1, True)

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

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(500000)

h, w, k = map(int, input().split())
s = []
ans = [[0] * w for _ in range(h)]
cnt = 0
#prev = 1

for i in range(h):
    s.append(input())

#print(s)

for i in range(h):
    if '#' not in s[i]:
        continue
    shouldCover = False

    for j in range(w):
        if j == 0 and s[i][j] == '.':
            cnt += 1
            ans[i][j] = cnt
            shouldCover = True
        elif s[i][j] == '#' and shouldCover:
            ans[i][j] = cnt
            shouldCover = False
        elif s[i][j] == '#':
            cnt += 1
            ans[i][j] = cnt
            shouldCover = False
        else:
            ans[i][j] = cnt

def solve(cur, nxt, down):
    if down:
        if '#' in s[nxt]:
            for j in range(w):
                ans[cur][j] = ans[nxt][j]
        else:
            solve(cur, nxt+1, down)
    else:
        if '#' in s[nxt]:
            for j in range(w):
                ans[cur][j] = ans[nxt][j]
        else:
            solve(cur, nxt-1, down)


for i in range(h):
    if all(ans[i][j] == 0 for j in range(w)):
        if i == h - 1:
            solve(i, i-1, False)
        else:
            #print('down!')
            solve(i, i+1, True)

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

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)

h, w, k = map(int, input().split())
s = []
ans = [[0] * w for _ in range(h)]
cnt = 0
#prev = 1

for i in range(h):
    s.append(input())

#print(s)

for i in range(h):
    if '#' not in s[i]:
        continue
    shouldCover = False

    for j in range(w):
        if j == 0 and s[i][j] == '.':
            cnt += 1
            ans[i][j] = cnt
            shouldCover = True
        elif s[i][j] == '#' and shouldCover:
            ans[i][j] = cnt
            shouldCover = False
        elif s[i][j] == '#':
            cnt += 1
            ans[i][j] = cnt
            shouldCover = False
        else:
            ans[i][j] = cnt

def solve(cur, nxt, down):
    if down:
        if '#' in s[nxt]:
            for j in range(w):
                ans[cur][j] = ans[nxt][j]
        else:
            solve(cur, nxt+1, down)
    else:
        if '#' in s[nxt]:
            for j in range(w):
                ans[cur][j] = ans[nxt][j]
        else:
            solve(cur, nxt-1, down)


for i in range(h):
    if all(ans[i][j] == 0 for j in range(w)):
        if i == h - 1:
            solve(i, i-1, False)
        else:
            #print('down!')
            solve(i, i+1, True)

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

Buggy submission (verdict: Runtime Error):
h, w, k = map(int, input().split())
s = []
ans = [[0] * w for _ in range(h)]
cnt = 0
#prev = 1

for i in range(h):
    s.append(input())

#print(s)

for i in range(h):
    if '#' not in s[i]:
        continue
    shouldCover = False

    for j in range(w):
        if j == 0 and s[i][j] == '.':
            cnt += 1
            ans[i][j] = cnt
            shouldCover = True
        elif s[i][j] == '#' and shouldCover:
            ans[i][j] = cnt
            shouldCover = False
        elif s[i][j] == '#':
            cnt += 1
            ans[i][j] = cnt
            shouldCover = False
        else:
            ans[i][j] = cnt

def solve(cur, nxt, down):
    if down:
        if '#' in s[nxt]:
            for j in range(w):
                ans[cur][j] = ans[nxt][j]
        else:
            solve(cur, nxt+1, down)
    else:
        if '#' in s[nxt]:
            for j in range(w):
                ans[cur][j] = ans[nxt][j]
        else:
            solve(cur, nxt-1, down)


for i in range(h):
    if all(ans[i][j] == 0 for j in range(w)):
        if i == h - 1:
            solve(i, i-1, False)
        else:
            #print('down!')
            solve(i, i+1, True)

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