Problem p02803 (python)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
from collections import deque


h,w=map(int,input().split())
s = [list(input()) for i in range(h)]

def bfs(sx,sy):
    dx = [0,1,0,-1]
    dy = [1,0,-1,0]
    d = [[-1]*w for i in range(h)]
    q = deque()
    q.append((sx,sy))
    d[sx][sy] = 0
    while q:
        x,y = q.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if  0<= nx < h and 0 <= ny < w and d[nx][ny]==-1 and s[nx][ny]=='.':
                q.append((nx,ny))
                d[nx][ny] = d[x][y]+1
    return max([max(i) for i in d])

count = 0
for i in range(h):
    for j in range(w):
        if s[i][j] != '#':
            count  = max(count, bfs(i,j))
print(count)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
from collections import deque


h,w=map(int,input().split())
s = [list(input()) for i in range(h)]

def bfs(sx,sy,gx,gy):
    dx = [0,1,0,-1]
    dy = [1,0,-1,0]
    d = [[float('inf')]*w for i in range(h)]
    q = deque()
    q.append((sx,sy))
    d[sx][sy] = 0
    while q:
        x,y = q.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if  0<= nx < h and 0 <= ny < w and d[nx][ny]==float('inf') and s[nx][ny]=='.':
                q.append((nx,ny))
                d[nx][ny] = d[x][y]+1
    return max([max(i) for i in d])

count = 0
for i in range(h):
    for j in range(w):
        if s[i][j] != '#':
            count  = max(count, bfs(i,j,k,l))
print(count)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
from collections import deque

def bfs(sx,sy,gx,gy):
    d = [[float("inf")] * w for i in range(h)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0

    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < h and 0 <= ny < w and s[nx][ny] != "#" and d[nx][ny] == float("inf"):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1

    return d[gx][gy]

h,w=map(int,input().split())
s = [list(input()) for i in range(h)]
ans=0
def check(i,j):
    if 1<=j<=w-2:
        if s[0][j-1]=='#' or s[0][j+1]=='#' or s[1][j]=='#':
            return True
    elif 1<=j<=w-2:
        if s[h][j-1]=='#' or s[h][j+1]=='#' or s[h-1][j]=='#':
            return True
    elif 1<=i<=h-2:
        if s[i-1][0]=='#' or s[i+1][0]=='#' or s[i][1]=='#':
            return True
    elif 1<=i<=h-2:
        if s[i-1][w]=='#' or s[i+1][w]=='#' or s[i][w-1]=='#':
            return True
    elif 1<=i<=h-2 and 1<=j<=w-2:
        if s[i][j+1]=='#' or s[i][j-1]=='#' or s[i+1][j]=='#' or s[i-1][j]=='#':
            return True
    elif i==0 and j==0:
        return True
    elif i==0 and j==w-1:
        return True
    elif i==h-1 and j==0:
        return True
    elif i==h-1 and j==w-1:
        return True
    else:
        return False
for i in range(h):
    for j in range(w):
        if s[i][j]=='.' and check(i,j):
            if j != w:
                for m in range(j+1,w):
                    if s[i][m]=='.' and check(i,m):
                        ans = max(ans,bfs(i,j,i,m))
            for k in range(i+1,h):
                for l in range(w):
                    if s[k][l]=='.' and check(k,l):
                        ans = max(ans,bfs(i,j,k,l))
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
from collections import deque

def bfs(sx,sy,gx,gy):
    d = [[float("inf")] * w for i in range(h)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0

    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < h and 0 <= ny < w and s[nx][ny] != "#" and d[nx][ny] == float("inf"):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1

    return d[gx][gy]

h,w=map(int,input().split())
s = [list(input()) for i in range(h)]
ans=0
def check(i,j):
    if 1<=j<=w-2:
        if s[0][j-1]=='#' or s[0][j+1]=='#' or s[1][j]=='#':
            return True
    elif 1<=j<=w-2:
        if s[h][j-1]=='#' or s[h][j+1]=='#' or s[h-1][j]=='#':
            return True
    elif 1<=i<=h-2:
        if s[i-1][0]=='#' or s[i+1][0]=='#' or s[i][1]=='#':
            return True
    elif 1<=i<=h-2:
        if s[i-1][w]=='#' or s[i+1][w]=='#' or s[i][w-1]=='#':
            return True
    elif 1<=i<=h-2 and 1<=j<=w-2:
        if s[i][j+1]=='#' or s[i][j-1]=='#' or s[i+1][j]=='#' or s[i-1][j]=='#':
            return 0
    elif i==0 and j==0:
        return True
    elif i==0 and j==w-1:
        return True
    elif i==h-1 and j==0:
        return True
    elif i==h-1 and j==w-1:
        return True
    else:
        return False
for i in range(h):
    for j in range(w):
        if s[i][j]=='.' and check(i,j):
            if j != w:
                for m in range(j+1,w):
                    if s[i][m]=='.' and check(i,m):
                        ans = max(ans,bfs(i,j,i,m))
            for k in range(i+1,h):
                for l in range(w):
                    if s[k][l]=='.' and check(k,l):
                        ans = max(ans,bfs(i,j,k,l))
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
from collections import deque

def bfs(sx,sy,gx,gy):
    d = [[float("inf")] * w for i in range(h)]
    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0

    while que:
        p = que.popleft()
        if p[0] == gx and p[1] == gy:
            break
        for i in range(4):
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if 0 <= nx < h and 0 <= ny < w and s[nx][ny] != "#" and d[nx][ny] == float("inf"):
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1

    return d[gx][gy]

h,w=map(int,input().split())
s = [list(input()) for i in range(h)]
ans=0
def check(i,j):
    if 1<=j<=w-2:
        if s[0][j-1]=='#' or s[0][j+1]=='#' or s[1][j]=='#':
            return True
    elif 1<=j<=w-2:
        if s[h][j-1]=='#' or s[h][j+1]=='#' or s[h-1][j]=='#':
            return True
    elif 1<=i<=h-2:
        if s[i-1][0]=='#' or s[i+1][0]=='#' or s[i][1]=='#':
            return True
    elif 1<=i<=h-2:
        if s[i-1][w]=='#' or s[i+1][w]=='#' or s[i][w-1]=='#':
            return True
    elif i==0 and j==0:
        return True
    elif i==0 and j==w-1:
        return True
    elif i==h-1 and j==0:
        return True
    elif i==h-1 and j==w-1:
        return True
    else:
        return False
for i in range(h):
    for j in range(w):
        if s[i][j]=='.' and check(i,j):
            if j != w:
                for m in range(j+1,w):
                    if s[i][m]=='.' and check(i,m):
                        ans = max(ans,bfs(i,j,i,m))
            for k in range(i+1,h):
                for l in range(w):
                    if s[k][l]=='.' and check(k,l):
                        ans = max(ans,bfs(i,j,k,l))
print(ans)