Problem p02855 (python)

Fixed submission:
h,w,k = map(int, input().split())
s = []
Nm = [[0]*w for i in range(h)]
nm = 1
Q = []
empty =[]
for i in range(h):
    si = input()
    s.append(si)
    berry = False
    for j in range(w):
        if si[j]=="#":
            berry = True
            Nm[i][j]=nm
            nm+=1
            Q.append((i,j))
    if not berry:
        empty.append(i)

for i,j in Q:#横に伸ばす
    for t in range(j+1,w):
        if Nm[i][t]==0:
            Nm[i][t]=Nm[i][j]
        else:
            break
    for t in range(j-1,-1,-1):
        if Nm[i][t]==0:
            Nm[i][t]=Nm[i][j]
        else:
            break
if not empty:
    for i in range(h):
        print(*Nm[i])
    exit()
j=0###
if 0 in empty:#下から延ばす必要がある場合
    j = 0
    while empty[j]==j:
        j+=1
        if j<len(empty):
            continue
        else:
            break
    for t in range(j):
        for s in range(w):
            Nm[t][s] = Nm[j][s]
#print(j)
for i in empty[j:]:#縦下に伸ばす
    for j in range(w):
        Nm[i][j] = Nm[i-1][j]

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

Buggy submission (verdict: Runtime Error):
h,w,k = map(int, input().split())
s = []
Nm = [[0]*w for i in range(h)]
nm = 1
Q = []
empty =[]
for i in range(h):
    si = input()
    s.append(si)
    berry = False
    for j in range(w):
        if si[j]=="#":
            berry = True
            Nm[i][j]=nm
            nm+=1
            Q.append((i,j))
    if not berry:
        empty.append(i)

for i,j in Q:#横に伸ばす
    for t in range(j+1,w):
        if Nm[i][t]==0:
            Nm[i][t]=Nm[i][j]
        else:
            break
    for t in range(j-1,-1,-1):
        if Nm[i][t]==0:
            Nm[i][t]=Nm[i][j]
        else:
            break
if not empty:###
    for i in range(h):
        print(*Nm[i])
    exit()
for i in empty:#縦下に伸ばす
    for j in range(w):
        Nm[i][j] = Nm[i-1][j]
if 0 in empty:#下から延ばす必要がある場合
    j = 0
    while empty[j]==j:
        j+=1
    for t in range(j):
        for s in range(w):
            Nm[t][s] = Nm[j][s]

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

Buggy submission (verdict: Runtime Error):
h,w,k = map(int, input().split())
s = []
Nm = [[0]*w for i in range(h)]
nm = 1
Q = []
empty =[]
for i in range(h):
    si = input()
    s.append(si)
    berry = False
    for j in range(w):
        if si[j]=="#":
            berry = True
            Nm[i][j]=nm
            nm+=1
            Q.append((i,j))
    if not berry:
        empty.append(i)

for i,j in Q:#横に伸ばす
    for t in range(j+1,w):
        if Nm[i][t]==0:
            Nm[i][t]=Nm[i][j]
        else:
            break
    for t in range(j-1,-1,-1):
        if Nm[i][t]==0:
            Nm[i][t]=Nm[i][j]
        else:
            break
for i in empty:#縦下に伸ばす
    for j in range(w):
        Nm[i][j] = Nm[i-1][j]
if 0 in empty:#下から延ばす必要がある場合
    j = 0
    while empty[j]==j:
        j+=1
    for t in range(j):
        for s in range(w):
            Nm[t][s] = Nm[j][s]

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

Buggy submission (verdict: Runtime Error):
#貪欲に埋める
from collections import deque
h,w,k = map(int,input().split( ))

s = []
ichigo = []
for i in range(h):
    si = input()
    si+="."
    s.append(si)
    for j in range(w):
        if si[j]=="#":
            ichigo.append((i,j))

Nm = [[-1]*w for _ in range(h)]
ichigo.sort()
tmp = 1
for ich in ichigo:
    Nm[ich[0]][ich[1]]=tmp
    tmp += 1
#print(Nm)
for ich in ichigo:
    Q = deque()
    Q.append(ich)
    #print(ich)
    now = Nm[ich[0]][ich[1]]
    while Q:
        b,c = Q.popleft()
        for i in range(c+1,w):
            if Nm[b][i]<0:
                Nm[b][i] = now
            else:
                i-=1##
                break

        for j in range(c-1,-1,-1):
            if Nm[b][j]<0:
                Nm[b][j] = now
            else:
                j+=1##
                break
        for u in range(b+1,h):
            flag = True
            for t in range(j,i+1):
                if Nm[u][t]>=0:
                    flag = False
                    break
            if flag:
                for t in range(j,i+1):
                    Nm[u][t] = now
            else:
                break
        for u in range(b-1,-1,-1):
            flag = True
            for t in range(j,i+1):
                if Nm[u][t]>=0:
                    flag = False
                    break
            if flag:
                for t in range(j,i+1):
                    Nm[u][t] = now
            else:
                break
            
for i in range(h):
    print(*Nm[i])

Buggy submission (verdict: Runtime Error):
#貪欲に埋める
from collections import deque
h,w,k = map(int,input().split( ))

s = []
ichigo = []
for i in range(h):
    si = input()
    si+="."
    s.append(si)
    for j in range(w):
        if si[j]=="#":
            ichigo.append((i,j))

Nm = [[-1]*w for _ in range(h)]

tmp = 1
for ich in ichigo:
    Nm[ich[0]][ich[1]]=tmp
    tmp += 1
#print(Nm)
for ich in ichigo:
    Q = deque()
    Q.append(ich)
    #print(ich)
    now = Nm[ich[0]][ich[1]]
    while Q:
        b,c = Q.popleft()
        for i in range(c+1,w):
            if Nm[b][i]<0:
                Nm[b][i] = now
            else:
                i-=1
                break

        for j in range(c-1,-1,-1):
            if Nm[b][j]<0:
                Nm[b][j] = now
            else:
                j+=1##
                break
        for u in range(b+1,h):
            flag = True
            for t in range(j,i+1):
                if Nm[u][t]>=0:
                    flag = False
                    break
            if flag:
                for t in range(j,i+1):
                    Nm[u][t] = now
            else:
                break
        for u in range(b-1,-1,-1):
            flag = True
            for t in range(j,i+1):
                if Nm[u][t]>=0:
                    flag = False
                    break
            if flag:
                for t in range(j,i+1):
                    Nm[u][t] = now
            else:
                break
            
for i in range(h):
    print(*Nm[i])

Buggy submission (verdict: Runtime Error):
#貪欲に埋める
from collections import deque
h,w,k = map(int,input().split( ))

s = []
ichigo = []
for i in range(h):
    si = input()
    si+="."
    s.append(si)
    for j in range(w):
        if si[j]=="#":
            ichigo.append((i,j))

Nm = [[-1]*w for _ in range(h)]

tmp = 1
for ich in ichigo:
    Nm[ich[0]][ich[1]]=tmp
    tmp += 1
#print(Nm)
for ich in ichigo:
    Q = deque()
    Q.append(ich)
    #print(ich)
    now = Nm[ich[0]][ich[1]]
    while Q:
        b,c = Q.popleft()
        for i in range(c+1,w):
            if Nm[b][i]<0:
                Nm[b][i] = now
            else:
                i-=1##
                break

        for j in range(c-1,-1,-1):
            if Nm[b][j]<0:
                Nm[b][j] = now
            else:
                j+=1##
                break
        for u in range(b+1,h):
            flag = True
            for t in range(j,i+1):
                if Nm[u][t]>=0:
                    flag = False
                    break
            if flag:
                for t in range(j,i+1):
                    Nm[u][t] = now
            else:
                break
        for u in range(b-1,-1,-1):
            flag = True
            for t in range(j,i+1):
                if Nm[u][t]>=0:
                    flag = False
                    break
            if flag:
                for t in range(j,i+1):
                    Nm[u][t] = now
            else:
                break
            
for i in range(h):
    print(*Nm[i])