Problem p02855 (python)

Fixed submission:
h,w,k=map(int,input().split())
G=[["."]*w for i in range(h)]
for i in range(h):
  G[i]=list(input())
ans=[[0]*w for i in range(h)]
GG=[[0] for i in range(h)]
for i in range(h):
  for j in range(w):
    if G[i][j]=="#":
      GG[i].append(j)
  GG[i].append(w)
B=[0]
a=1
for i in range(h):
  if len(GG[i])==2:
    continue
  B.append(i)
  ans[i][GG[i][0]:GG[i][2]]=[a]*(GG[i][2]-GG[i][0])
  a=a+1
  for j in range(len(GG[i])-3):
    ans[i][GG[i][j+2]:GG[i][j+3]]=[a]*(GG[i][j+3]-GG[i][j+2])
    a=a+1
B.append(h)
for i in range(B[2]-B[0]):
  ans[i]=ans[B[1]]
for i in range(B[2],h):
  if i not in B:
    ans[i]=ans[i-1]
for i in range(h):
  for j in range(w):
    print(ans[i][j],end=" ")
  print()

Buggy submission (verdict: Runtime Error):
h,w,k=map(int,input().split())
G=[["."]*w for i in range(h)]
for i in range(h):
  G[i]=list(input())
ans=[[0]*w for i in range(h)]
GG=[[0] for i in range(h)]
for i in range(h):
  for j in range(w):
    if G[i][j]=="#":
      GG[i].append(j)
  GG[i].append(w)
B=[0]
a=1
for i in range(h):
  if len(GG[i])==2:
    continue
  B.append(i)
  ans[i][GG[i][0]:GG[i][2]]=[a]*(GG[i][2]-GG[i][0])
  a=a+1
  for j in range(len(GG[i])-3):
    ans[i][GG[i][j+2]:GG[i][j+3]]=[a]*(GG[i][j+3]-GG[i][j+2])
    a=a+1
B.append(h)
for i in range(B[2]-B[0]):
  ans[i]=ans[B[1]]
for i in range(B[2],h):
  if B[i]==0:
    ans[i]=ans[i-1]
for i in range(h):
  for j in range(w):
    print(ans[i][j],end=" ")
  print()

Buggy submission (verdict: Runtime Error):
h,w,k=map(int,input().split())
G=[["."]*w for i in range(h)]
for i in range(h):
  G[i]=list(input())
ans=[[0]*w for i in range(h)]
GG=[[0] for i in range(h)]
for i in range(h):
  for j in range(w):
    if G[i][j]=="#":
      GG[i].append(j)
  GG[i].append(w)
B=[0]*h
a=1
for i in range(h):
  if len(GG[i])==2:
    continue
  B[i]=1
  ans[i][GG[i][0]:GG[i][2]]=[a]*(GG[i][2]-GG[i][0])
  a=a+1
  for j in range(len(GG[i])-3):
    ans[i][GG[i][j+2]:GG[i][j+3]]=[a]*(GG[i][j+3]-GG[i][j+2])
    a=a+1
for i in range(B[2]-B[0]):
  ans[i]=ans[B[1]]
for i in range(B[2],h):
  if B[i]==0:
    ans[i]=ans[i-1]
print(ans)

Buggy submission (verdict: Runtime Error):
h,w,k=map(int,input().split())
g = [['#']*(w) for i in range(h)]
for i in range(h):
  g[i] = list(input())
a = [0] * (h)
for i in range(h):
  a[i] = [j for j in range(w) if g[i][j] == '#']
G=[[]*w for i in range(h)]
b=1
if g[0]==['.']*w:
  for i in range(h):
    if g[i]!=['.']*w:
      if len(a[i])==1:
        G[i]=[b]*w
        b=b+1
      else:
        for j in range(len(a[i])):
          if j==0:
            G[i]=[b]*(a[i][j+1])
            b=b+1
          elif j!=len(a[i])-1:
            for k in range(a[i][j+1]-a[i][j]):
              G[i].append(b)
            b=b+1
            
          else:
            for k in range(w-a[i][j]):
              G[i].append(b)
            b=b+1
            break
            
            
  for l in range(i-1):
    G[l]=G[i-1]
  for i in range(i+1,h):
    if (a[i])==0:
      G[i]==G[i-1]
    elif len(a[i])==1:
      G[i]=[b]*w
      b=b+1
    else:
      for j in range(len(a[i])):
        if j==0:
          G[i]=[b]*(a[i][j+1])
          b=b+1
        elif j==len(a[i])-1:
          for k in range(w-a[i][j]):
            G[i].append(b)
          b=b+1
        else:
          for k in range(a[i][j+1]-a[i][j]):
            G[i].append(b)
          b=b+1
      
else:
  for i in range(h):
    if (a[i])==0:
      G[i]==G[i-1]
    elif len(a[i])==1:
      G[i]=[b]*w
      b=b+1
    else:
      for j in range(len(a[i])):
        if j==0:
          G[i]=[b]*(a[i][j+1])
          b=b+1
        elif j==len(a[i])-1:
          for k in range(w-a[i][j]):
            G[i].append(b)
          b=b+1
        else:
          for k in range(a[i][j+1]-a[i][j]):
            G[i].append(b)
          b=b+1
for i in range(h):
  for j in range(w):
    if j!=w-1:
      print(G[i][j], end="　")
    else:
      print(G[i][j])

Buggy submission (verdict: Runtime Error):
h,w,k=map(int,input().split())
g = [['#']*(w) for i in range(h)]
for i in range(h):
  g[i] = list(input())
a = [0] * (h)
for i in range(h):
  a[i] = [j for j in range(w) if g[i][j] == '#']
G=[[]*w for i in range(h)]
b=1
if g[0]==['.']*w:
  for i in range(h):
    if g[i]!=['.']*w:
      if len(a[i])==1:
        G[i]=[b]*w
        b=b+1
      else:
        for j in range(len(a[i])):
          if j==0:
            G[i]=[b]*(a[i][j+1])
            b=b+1
          elif j!=len(a[i])-1:
            for k in range(a[i][j+1]-a[i][j]):
              G[i].append(b)
            b=b+1
            
          else:
            for k in range(w-a[i][j]):
              G[i].append(b)
            b=b+1
            break
            
            
  for l in range(i-1):
    G[l]=G[i-1]
  for i in range(i+1,h):
    if (a[i])==0:
      G[i]==G[i-1]
    elif len(a[i])==1:
      G[i]=[b]*w
      b=b+1
    else:
      for j in range(len(a[i])):
        if j==0:
          G[i]=[b]*(a[i][j+1])
          b=b+1
        elif j==len(a[i])-1:
          for k in range(w-a[i][j]):
            G[i].append(b)
          b=b+1
        else:
          for k in range(a[i][j+1]-a[i][j]):
            G[i].append(b)
          b=b+1
      
else:
  for i in range(h):
    if (a[i])==0:
      G[i]==G[i-1]
    elif len(a[i])==1:
      G[i]=[b]*w
      b=b+1
    else:
      for j in range(len(a[i])):
        if j==0:
          G[i]=[b]*(a[i][j+1])
          b=b+1
        elif j==len(a[i])-1:
          for k in range(w-a[i][j]):
            G[i].append(b)
          b=b+1
        else:
          for k in range(a[i][j+1]-a[i][j]):
            G[i].append(b)
          b=b+1
for i in range(h):
  for j in range(w):
    print(G[i][j], end="　")
  print()