Problem p02803 (python)

Fixed submission:
import math 
import bisect
import heapq
from collections import deque

h,w=map(int,input().split())
M=[]
M=[input() for _ in range(h)]
 
def bfs(x,y):
    dist=[[-1]*w  for _ in range(h)]
    seen=[[False]*w for _ in range(h)]
    stk=[(x,y,0)]
    while stk:
        px,py,dst=stk.pop(0)
        if seen[px][py]:
            continue
        seen[px][py]=True
        
        if M[px][py]=='#':
            dist[px][py]=0
            continue
        
        for x,y in [(-1,0),(1,0),(0,-1),(0,1)]:
            if px+x<0 or px+x>=h or py+y<0 or py+y>=w:
                continue
            else:
                stk.append((px+x,py+y,dst+1))
        dist[px][py]=dst
    ans=0
 
    for i in range(h):
        ans=max(max(dist[i]),ans)
    return ans
ans = 0
for i in range(h):
    for j in range(w):
        if M[i][j]=='#':
            continue
        ans=max(bfs(i,j),ans)
print(ans)

Buggy submission (verdict: Runtime Error):
import math 
import bisect
import heapq
from collections import deque

h,w = map(int,input().split())
s = [map(int,input().split()) for _ in range(h)]
v = [ [0] * w ] * h

def bfs(i,j,s,v,count):

    if s[i][j] == '#': return 0
    else:
        v[i][j] = 1
        if i+1 < h and j+1 < w :
            if v[i+1][j+1] != 1 and s[i+1][j+1] == '.':
                count += 1
                bfs(i+1,j+1,s,v,count)

        elif i+1 < h and j-1 > 0 :
            if v[i+1][j-1] != 1 and s[i+1][j-1] == '.':
                count += 1
     
                bfs(i+1,j-1,s,v,count)

        elif i-1 < h and j+1 > 0 :
            if v[i-1][j+1] != 1 and s[i-1][j+1] == '.':
                count += 1
       
                bfs(i-1,j+1,s,v,count)

        elif i-1 < h and j-1 > 0 :
            if v[i+1][j-1] != 1 and s[i-1][j-1] == '.':
                count += 1
               
                bfs(i-1,j-1,s,v,count)

        else :return count

ans = 0
for i in range(h):
    for j in range(w):
        x,y = i,j
        ans = max(ans,bfs(i,j,s,v,0))

print(ans)

Buggy submission (verdict: Runtime Error):
import math 
import bisect
import heapq
from collections import deque

h,w = map(int,input().split())
s = [map(int,input().split()) for _ in range(h)]
v = [ [0] * w ] * h

def bfs(i,j,s,v,count):

    if s[i][j] == '#': return 0
    else:
        v[i][j] = 1
        if i+1 < h and j+1 < w :
            if v[i+1][j+1] != 1 and s[i+1][j+1] == '.':
                count += 1
                bfs(i+1,j+1,s,v,count)

        elif i+1 < h and j-1 > 0 :
            if v[i+1][j-1] != 1 and s[i+1][j-1] == '.':
                count += 1
     
                bfs(i+1,j-1,s,v,count)

        elif i-1 < h and j+1 > 0 :
            if v[i-1][j+1] != 1 and s[i-1][j+1] == '.':
                count += 1
       
                bfs(i-1,j+1,s,v,count)

        elif i-1 < h and j-1 > 0 :
            if v[i+1][j-1] != 1 and s[i-1][j-1] == '.':
                count += 1
               
                bfs(i-1,j-1,s,v,count)

        else :return count

ans = 1000000
for i in range(h):
    for j in range(w):
        x,y = i,j
        ans = min(ans,bfs(i,j,s,v,0))

Buggy submission (verdict: Runtime Error):
import math 
import bisect
import heapq
from collections import deque

h,w = map(int,input().split())
s = [map(int,input().split()) for _ in range(h)]
v = [ [0] * w ] * h

def bfs(i,j,s,v,count):

    if s[i][j] == '#': return 0
    else:
        v[i][j] = 1
        if i+1 < h and j+1 < w :
            if v[i+1][j+1] != 1 and s[i+1][j+1] == '.':
                count += 1
                bfs(i+1,j+1,s,v,count)

        elif i+1 < h and j-1 > 0 :
            if v[i+1][j-1] != 1 and s[i+1][j-1] == '.':
                count += 1
     
                bfs(i+1,j-1,s,v,count)

        elif i-1 < h and j+1 > 0 :
            if v[i-1][j+1] != 1 and s[i-1][j+1] == '.':
                count += 1
       
                bfs(i-1,j+1,s,v,count)

        elif i-1 < h and j-1 > 0 :
            if v[i+1][j-1] != 1 and s[i-1][j-1] == '.':
                count += 1
               
                bfs(i-1,j-1,s,v,count)

        else :return count

ans = 1000000
for i in range(h):
    for j in range(w):
        x,y = i,j
        ans = min(ans,bfs(i,j,s,v))

Buggy submission (verdict: Runtime Error):
import math 
import bisect
import heapq
from collections import deque

h,w = map(int,input().split())
s = [map(int,input().split()) for _ in range(h)]
v = [ [0] * w ] * h

def bfs(i,j,s,v):
    count = 0
    mx = 0
    if s[i][j] == '#': return 0
    else:
        v[i][j] = 1
        if i+1 < h and j+1 < w :
            if v[i+1][j+1] != 1 and s[i+1][j+1] == '.':
                count += 1
                mx = max(count,mx)
                bfs(i+1,j+1,s,v)

        elif i+1 < h and j-1 > 0 :
            if v[i+1][j-1] != 1 and s[i+1][j-1] == '.':
                count += 1
                mx = max(count,mx)
                bfs(i+1,j-1,s,v)

        elif i-1 < h and j+1 > 0 :
            if v[i-1][j+1] != 1 and s[i-1][j+1] == '.':
                count += 1
                mx = max(count,mx)
                bfs(i-1,j+1,s,v)

        elif i-1 < h and j-1 > 0 :
            if v[i+1][j-1] != 1 and s[i-1][j-1] == '.':
                count += 1
                mx = max(count,mx)
                bfs(i-1,j-1,s,v)


    return mx

ans = 1000000
for i in range(h):
    for j in range(w):
        x,y = i,j
        ans = min(ans,bfs(i,j,s,v))

Buggy submission (verdict: Runtime Error):
import math 
import bisect
import heapq
from collections import deque

h,w = map(int,input().split())
s = [map(int,input().split()) for _ in range(h)]
v = [ [0] * w ] * h

bfs(i,j,s,v):
    count = 0
    mx = 0
    if s[i][j] == '#': return 0
    elif:
        v[i][j] = 1
        if i+1 < h and j+1 < w 
            if v[i+1][j+1] != 1 and s[i+1][j+1] == '.'
                count += 1
                mx = max(count,mx)
                bfs(i+1,j+1,s,v)

        elif i+1 < h and j-1 > 0 
            if v[i+1][j-1] != 1 and s[i+1][j-1] == '.'
                count += 1
                mx = max(count,mx)
                bfs(i+1,j-1,s,v)

        elif i-1 < h and j+1 > 0 
            if v[i-1][j+1] != 1 and s[i-1][j+1] == '.'
                count += 1
                mx = max(count,mx)
                bfs(i-1,j+1,s,v)

        elif i-1 < h and j-1 > 0 
            if v[i+1][j-1] != 1 and s[i-1][j-1] == '.'
                count += 1
                mx = max(count,mx)
                bfs(i-1,j-1,s,v)


    return mx

ans = 1000000
for i in range(h):
    for j in range(w):
        x,y = i,j
        ans = min(ans,bfs(i,j,s,v))