Problem p02803 (python)

Fixed submission:
from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)
#T=[]
S=np.zeros_like(T,dtype=int) 
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#B=T.copy()    
d=0
c=deque()  
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue
 
          
        c.append((i,j))
        S[i][j] = 1
        #c.append(j)
        
        #t=len(c)
        while True:

                try:
                  m,n = c.popleft()
                except IndexError: break
                
                
                for kk in [(m+1,n),(m-1,n),(m,n+1),(m,n-1)]:
                  if not (0<=kk[0]<H and 0<= kk[1] <W): continue
                  if T[kk]==1:continue
                  if S[kk] == 0:
                    c.append(kk)
                    S[kk] = S[m][n]+1        
                        
 
        #print(T)
        d = max(d,np.max(S))
        S=np.zeros_like(T,dtype=int)
        
print(d-1)

Buggy submission (verdict: Runtime Error):
from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)
T=[]
S=np.zeros_like(T,dtype=int) 
for i in range(H):
  T[i]=[x for x in input().split()]
#B=T.copy()    
d=0
c=deque()  
for i in range(H):
    for j in range(W):
         
        if T[i][j] =="#":continue
 
          
        c.append((i,j))
        S[i][j] = 1
        #c.append(j)
        
        #t=len(c)
        while True:

                try:
                  m,n = c.popleft()
                except IndexError: break
                
                
                for kk in [(m+1,n),(m-1,n),(m,n+1),(m,n-1)]:
                  if not (0<=kk[0]<H and 0<= kk[1] <W): continue
                  if T[kk]=="#":continue
                  if S[kk] == 0:
                    c.append(kk)
                    S[kk] = S[m][n]+1        
                        
 
        #print(T)
        d = max(d,np.max(S))
        S=np.zeros_like(T,dtype=int)
        
print(d-1)

Buggy submission (verdict: Runtime Error):
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
B=T.copy()    
d=0
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue

        c=deque()    
        s=0
        c+=[i,j]
        
        t=len(c)//2
        while t>0:
                s+=1
                t=len(c)//2
                for k in range(t):
                        m = c.popleft()
                        n = c.popleft()
                        T[m][n]=s
                        if m != H-1:
                                if T[m+1][n]==0:
                                        c.append(m+1)
                                        c.append(n)
                        if m != 0:
                                if T[m-1][n]==0:
                                        c.append(m-1)
                                        c.append(n)
 
                        if n != W-1:
                                if T[m][n+1]==0:
                                        c.append(m)
                                        c.append(n+1)
                                        
                        if n != 0:
                                if T[m][n-1]==0:
                                        c.append(m)
                                        c.append(n-1)

        #print(T)
        d = max(d,s)
        T=B.copy()
        
print(d-2)

Buggy submission (verdict: Runtime Error):
import numpy as np

def chk_route(c,s,T):
    global H,W,R
    mx = H*W
    #print(c)
    #print(T)
    flg = 0
    T[c[0]][c[1]]=s
    if R[c[0]][c[1]] > s:
        R[c[0]][c[1]] = s

    if c[0] != H-1:
        #if T[c[0]+1][c[1]]!=1 and T[c[0]+1][c[1]]!=s-1 and T[c[0]+1][c[1]]!=2:
        if T[c[0]+1][c[1]]==0:
            flg=1
            B=T.copy()
            chk_route([c[0]+1,c[1]],s+1,B)
    if c[0] != 0:
        #if T[c[0]-1][c[1]]!=1 and T[c[0]-1][c[1]]!=s-1 and T[c[0]-1][c[1]]!=2:
        if T[c[0]-1][c[1]]==0:
            flg=1
            B=T.copy()
            chk_route([c[0]-1,c[1]],s+1,B)
    if c[1] != W-1:
        #if T[c[0]][c[1]+1]!=1 and T[c[0]][c[1]+1]!=s-1 and T[c[0]][c[1]+1]!=2:
        if T[c[0]][c[1]+1]==0:
            flg=1
            B=T.copy()
            chk_route([c[0],c[1]+1],s+1,B)
    if c[1] != 0:
        #if T[c[0]][c[1]-1]!=1 and T[c[0]][c[1]-1]!=s-1 and T[c[0]][c[1]-1]!=2:
        if T[c[0]][c[1]-1]==0:
            flg=1
            B=T.copy()
            chk_route([c[0],c[1]-1],s+1,B)

    if flg == 0:
        #print(T)
        #r=np.count_nonzero(T==2)
        if R[c[0]][c[1]] > s:
            R[c[0]][c[1]] = s
        #return s
    if s==2: pass #return mx
        #T=np.copy(B)
H,W= [int(x) for x in input().split()]
n=[]
#T = [[0] * W for i in range(H)]
T=np.zeros((H,W),dtype=int)
MX = H*W
R=np.full((H,W),MX,dtype = int)
#R = [[15] * W for i in range(H)]
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#print(T)
d=0
for i in range(H):
    for j in range(W):

        if T[i][j] ==1:continue

        c=[i,j]
        B=T.copy()
        R=np.full((H,W),MX,dtype = int)
        chk_route(c,int(2),B)

        d = max(d,np.max(R[np.where(R<MX)]))

        #b= b[np.where(b[:,22].astype(float) > 0)]
print(d-2)
#print(mx)

Buggy submission (verdict: Runtime Error):
import numpy as np

def chk_route(c,s,T):
    global H,W,R
    mx = H*W
    #print(c)
    #print(T)
    flg = 0
    T[c[0]][c[1]]=s
    if R[c[0]][c[1]] > s:
        R[c[0]][c[1]] = s

    if c[0] != H-1:
        #if T[c[0]+1][c[1]]!=1 and T[c[0]+1][c[1]]!=s-1 and T[c[0]+1][c[1]]!=2:
        if T[c[0]+1][c[1]]==0:
            flg=1
            B=T.copy()
            chk_route([c[0]+1,c[1]],s+1,B)
    if c[0] != 0:
        #if T[c[0]-1][c[1]]!=1 and T[c[0]-1][c[1]]!=s-1 and T[c[0]-1][c[1]]!=2:
        if T[c[0]-1][c[1]]==0:
            flg=1
            B=T.copy()
            chk_route([c[0]-1,c[1]],s+1,B)
    if c[1] != W-1:
        #if T[c[0]][c[1]+1]!=1 and T[c[0]][c[1]+1]!=s-1 and T[c[0]][c[1]+1]!=2:
        if T[c[0]][c[1]+1]==0:
            flg=1
            B=T.copy()
            chk_route([c[0],c[1]+1],s+1,B)
    if c[1] != 0:
        #if T[c[0]][c[1]-1]!=1 and T[c[0]][c[1]-1]!=s-1 and T[c[0]][c[1]-1]!=2:
        if T[c[0]][c[1]-1]==0:
            flg=1
            B=T.copy()
            chk_route([c[0],c[1]-1],s+1,B)

    if flg == 0:
        #print(T)
        #r=np.count_nonzero(T==2)
        if R[c[0]][c[1]] > s:
            R[c[0]][c[1]] = s
        #return s
    if s==2: pass #return mx
        #T=np.copy(B)
H,W= [int(x) for x in input().split()]
n=[]
#T = [[0] * W for i in range(H)]
T=np.zeros((H,W),dtype=int)
MX = H*W
R=np.full((H,W),MX,dtype = int)
#R = [[15] * W for i in range(H)]
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#print(T)
d=0
for i in range(H):
    for j in range(W):

        if T[i][j] =='1':continue

        c=[i,j]
        B=T.copy()
        R=np.full((H,W),MX,dtype = int)
        chk_route(c,int(2),B)

        d = max(d,np.max(R[np.where(R<MX)]))

        #b= b[np.where(b[:,22].astype(float) > 0)]
print(d-2)
#print(mx)

Buggy submission (verdict: Runtime Error):
import numpy as np

def chk_route(c,s,T):
    global H,W,R
    mx = H*W
    #print(c)
    #print(T)
    flg = 0
    T[c[0]][c[1]]=s
    if R[c[0]][c[1]] > s:
        R[c[0]][c[1]] = s

    if c[0] != H-1:
        #if T[c[0]+1][c[1]]!=1 and T[c[0]+1][c[1]]!=s-1 and T[c[0]+1][c[1]]!=2:
        if T[c[0]+1][c[1]]==0:
            flg=1
            B=T.copy()
            chk_route([c[0]+1,c[1]],s+1,B)
    if c[0] != 0:
        #if T[c[0]-1][c[1]]!=1 and T[c[0]-1][c[1]]!=s-1 and T[c[0]-1][c[1]]!=2:
        if T[c[0]-1][c[1]]==0:
            flg=1
            B=T.copy()
            chk_route([c[0]-1,c[1]],s+1,B)
    if c[1] != W-1:
        #if T[c[0]][c[1]+1]!=1 and T[c[0]][c[1]+1]!=s-1 and T[c[0]][c[1]+1]!=2:
        if T[c[0]][c[1]+1]==0:
            flg=1
            B=T.copy()
            chk_route([c[0],c[1]+1],s+1,B)
    if c[1] != 0:
        #if T[c[0]][c[1]-1]!=1 and T[c[0]][c[1]-1]!=s-1 and T[c[0]][c[1]-1]!=2:
        if T[c[0]][c[1]-1]==0:
            flg=1
            B=T.copy()
            chk_route([c[0],c[1]-1],s+1,B)

    if flg == 0:
        #print(T)
        #r=np.count_nonzero(T==2)
        if R[c[0]][c[1]] > s:
            R[c[0]][c[1]] = s
        #return s
    if s==2: pass #return mx
        #T=np.copy(B)
H,W= [int(x) for x in input().split()]
n=[]
#T = [[0] * W for i in range(H)]
T=np.zeros((H,W),dtype=int)
MX = H*W
R=np.full((H,W),MX,dtype = int)
#R = [[15] * W for i in range(H)]
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#print(T)
d=0
for i in range(H):
    for j in range(W):

        if T[i][j] =='#':continue
        
        c=[i,j]
        B=T.copy()
        R=np.full((H,W),MX,dtype = int)
        chk_route(c,int(2),B)

        d = max(d,np.max(R[np.where(R<MX)]))

        #b= b[np.where(b[:,22].astype(float) > 0)]
print(d-2)
#print(mx)

Buggy submission (verdict: Runtime Error):
import numpy as np

def chk_route(c,s,T):
    global H,W,R
    mx = H*W
    #print(c)
    #print(T)
    flg = 0
    T[c[0]][c[1]]=s
    if R[c[0]][c[1]] > s:
        R[c[0]][c[1]] = s
    #if s==2: B=T.copy()
    if c[0] != H-1:
        #if T[c[0]+1][c[1]]!=1 and T[c[0]+1][c[1]]!=s-1 and T[c[0]+1][c[1]]!=2:
        if T[c[0]+1][c[1]]==0:
            flg=1
            B=T.copy()
            chk_route([c[0]+1,c[1]],s+1,B)
    if c[0] != 0:
        #if T[c[0]-1][c[1]]!=1 and T[c[0]-1][c[1]]!=s-1 and T[c[0]-1][c[1]]!=2:
        if T[c[0]-1][c[1]]==0:
            flg=1
            B=T.copy()
            chk_route([c[0]-1,c[1]],s+1,B)
    if c[1] != W-1:
        #if T[c[0]][c[1]+1]!=1 and T[c[0]][c[1]+1]!=s-1 and T[c[0]][c[1]+1]!=2:
        if T[c[0]][c[1]+1]==0:
            flg=1
            B=T.copy()
            chk_route([c[0],c[1]+1],s+1,B)
    if c[1] != 0:
        #if T[c[0]][c[1]-1]!=1 and T[c[0]][c[1]-1]!=s-1 and T[c[0]][c[1]-1]!=2:
        if T[c[0]][c[1]-1]==0:
            flg=1
            B=T.copy()
            chk_route([c[0],c[1]-1],s+1,B)

    if flg == 0:
        #print(T)
        #r=np.count_nonzero(T==2)
        if R[c[0]][c[1]] > s:
            R[c[0]][c[1]] = s
        #return s
    if s==2: pass #return mx
        #T=np.copy(B)
H,W= [int(x) for x in input().split()]
n=[]
#T = [[0] * W for i in range(H)]
T=np.zeros((H,W),dtype=int)
MX = H*W
R=np.full((H,W),MX,dtype = int)
#R = [[15] * W for i in range(H)]
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#print(T)
d=0
for i in range(H):
    for j in range(W):
        c=[i,j]
        B=T.copy()
        R=np.full((H,W),MX,dtype = int)
        chk_route(c,int(2),B)

        d = max(d,np.max(R[np.where(R<MX)]))

        #b= b[np.where(b[:,22].astype(float) > 0)]
print(d-2)
#print(mx)