Problem p02803 (python)

Fixed submission:
import queue

H, W = map(int, input().split())
S = [input() for i in range(H)]
q = queue.Queue()
longest_path = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            g = [[0 if S[k][l] == '.' else -1 for l in range(W)] for k in range(H)]
            q.put((i,j))
            g[i][j] = 0
            while not(q.empty()):
                x, y = q.get()
                for nx, ny in [(x+1, y),(x-1, y),(x, y+1),(x, y-1)]:
                    if nx < 0 or nx > H-1 or ny < 0 or ny > W-1: continue
                    if g[nx][ny] != 0: continue
                    if nx == i and ny == j: continue
                    g[nx][ny] = g[x][y] + 1
                    q.put((nx,ny))
            '''
            print('===========')
            for line in g:
                print(line)
            '''     
            longest_path = max(longest_path, max(map(max, g)))
print(longest_path)

Buggy submission (verdict: Runtime Error):
import queue

H, W = map(int, input().split())
S = [input() for i in range(H)]
q = queue.Queue()
longest_path = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            g = [[0 if S[k][l] == '.' else -1 for l in range(W)] for k in range(H)]
            q.put((i,j))
            g[i][j] = 0
            while not(q.empty()):
                x, y = q.get()
                for nx, ny in [(x+1, y),(x-1, y),(x, y+1),(x, y-1)]:
                    if nx < 0 or nx > H-1 or ny < 0 or ny > W-1: continue
                    if g[nx][ny] != 0: continue
                    if nx == i and ny == j: continue
                    g[nx][ny] = g[x][y] + 1
                    q.put((nx,ny))
        '''
        print('===========')
        for line in g:
            print(line)
        '''
        longest_path = max(longest_path, max(map(max, g)))
print(longest_path)

Buggy submission (verdict: Runtime Error):
import queue

H, W = map(int, input().split())
S = [input() for i in range(H)]
q = queue.Queue()
longest_path = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            g = [[0 if S[k][l] == '.' else -1 for l in range(W)] for k in range(H)]
            q.put((i,j))
            g[i][j] = 0
            while not(q.empty()):
                x, y = q.get()
                for nx, ny in [(x+1, y),(x-1, y),(x, y+1),(x, y-1)]:
                    if nx < 0 or nx > H-1 or ny < 0 or ny > W-1: continue
                    if g[nx][ny] != 0: continue
                    if nx == i and ny == j: continue
                    g[nx][ny] = g[x][y] + 1
                    q.put((nx,ny))
        '''
        print('===========')
        for line in g:
            print(line)
        '''
        longest_path = max(longest_path, max(map(max, g)))
print(longest_path)

Buggy submission (verdict: Runtime Error):
import queue

H, W = map(int, input().split())
S = [input() for i in range(H)]
q = queue.LifoQueue()
longest_path = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            g = [[999 if S[k][l] == '.' else -1 for l in range(W)] for k in range(H)]
            q.put((i,j))
            g[i][j] = 0
            while not(q.empty()):
                x, y = q.get()
                for nx, ny in [(x+1, y),(x-1, y),(x, y+1),(x, y-1)]:
                    if nx < 0 or nx > H-1 or ny < 0 or ny > W-1: continue
                    if S[nx][ny] == '#': continue
                    if g[nx][ny] > g[x][y] + 1:
                        g[nx][ny] = g[x][y] + 1
                        q.put((nx,ny))
        longest_path = max(longest_path, max(map(max, g)))
print(longest_path)

Buggy submission (verdict: Runtime Error):
import networkx as nx

H, W = map(int, input().split())
S = [input() for i in range(H)]

G = nx.Graph()
for i in range(len(S)):
    for j in range(len(S[i])):
        if S[i][j] == '.':
            G.add_node((i,j))

for i in list(G.nodes):
    if G.has_node((i[0],i[1]+1)):
        G.add_edge(i, (i[0], i[1]+1))
    if G.has_node((i[0]+1,i[1])):
        G.add_edge(i, (i[0]+1, i[1]))

maxpath = 0
nodes = list(G.nodes)
for i in range(len(nodes)-1):
    for j in range(i+1, len(nodes)):
        l = len(nx.shortest_path(G, source=nodes[i], target=nodes[j]))
        maxpath = max(l, maxpath)

print(maxpath - 1)