Problem: Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.  The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is `#`, and a "road" square if S_{ij} is `.`.  From a road square, you can move to a horizontally or vertically adjacent road square.  You cannot move out of the maze, move to a wall square, or move diagonally.  Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.  Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.  In this situation, find the maximum possible number of moves Aoki has to make.

Submission attempts (in order):
import collections
H, W, *S = open(0).read().split()
H,W=[int(_) for _ in [H,W]]
G = [set() for _ in range(H * W)]
for i in range(H * W):
    x, y = divmod(i, W)
    if S[x][y] == '.':
        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            nx, ny = x + dx, y + dy
            if not (0 <= nx < H and 0 <= ny < W):
                continue
            if S[nx][ny] == '.':
                G[i].add(nx * W + ny)
for s in range(H * W):
    if len(G[s]):
        break
Q = collections.deque([s])
INF = 10 ** 10
d1 = [INF] * (H * W)
d1[s] = 0
while True:
    if not Q:
        break
    i = Q.popleft()
    d = d1[i]
    for j in G[i]:
        if d1[j] != INF:
            continue
        k = j
        d1[j] = d + 1
        Q.appendleft(j)
d1 = [INF] * (H * W)
d1[k] = 0
Q = collections.deque([k])
while True:
    if not Q:
        break
    i = Q.popleft()
    d = d1[i]
    for j in G[i]:
        if d1[j] != INF:
            continue
        d1[j] = d + 1
        Q.appendleft(j)
print(d1[k])


import collections
H, W, *S = open(0).read().split()
H,W=[int(_) for _ in [H,W]]
G = [set() for _ in range(H * W)]
for i in range(H * W):
    x, y = divmod(i, W)
    if S[x][y] == '.':
        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            nx, ny = x + dx, y + dy
            if not (0 <= nx < H and 0 <= ny < W):
                continue
            if S[nx][ny] == '.':
                G[i].add(nx * W + ny)
INF = 10 ** 10
dist = [INF] * (H * W)
ans = 0
for s in range(H * W):
    if len(G[s]) and dist[s] == INF:
        Q = collections.deque([s])
        se = set()
        while True:
            if not Q:
                break
            i = Q.popleft()
            for j in G[i]:
                if j in se:
                    continue
                se.add(j)
                k = j
                Q += [j]
        dist[k] = 0
        se = set()
        Q = collections.deque([k])
        while True:
            if not Q:
                break
            i = Q.popleft()
            if i in se:
                break
            se.add(i)
            d = dist[i]
            for j in G[i]:
                if dist[j] != INF:
                    continue
                k = j
                dist[j] = d + 1
                Q.appendleft(j)
        ans = max(ans, dist[k])
print(ans)


import collections
H, W, *S = open(0).read().split()
H,W=[int(_) for _ in [H,W]]
G = [set() for _ in range(H * W)]
for i in range(H * W):
    x, y = divmod(i, W)
    if S[x][y] == '.':
        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            nx, ny = x + dx, y + dy
            if not (0 <= nx < H and 0 <= ny < W):
                continue
            if S[nx][ny] == '.':
                G[i].add(nx * W + ny)
dist = [INF] * (H * W)
ans = 0
for s in range(H * W):
    if len(G[s]) and dist[s] != INF:
        Q = collections.deque([s])
        INF = 10 ** 10
        dist[s] = 0
        while True:
            if not Q:
                break
            i = Q.popleft()
            d = d1[i]
            for j in G[i]:
                if d1[j] != INF:
                    continue
                k = j
                d1[j] = d + 1
                Q.appendleft(j)
        d1 = [INF] * (H * W)
        d1[k] = 0
        Q = collections.deque([k])
        while True:
            if not Q:
                break
            i = Q.popleft()
            d = d1[i]
            for j in G[i]:
                if d1[j] != INF:
                    continue
                k = j
                d1[j] = d + 1
                Q.appendleft(j)
        ans = max(ans, d1[k])
print(ans)


import collections
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall

H, W, *S = open(0).read().split()
H, W = [int(_) for _ in [H, W]]
A = []
B = []
C = []
for i in range(H * W):
    x, y = divmod(i, W)
    if S[x][y] == '.':
        for dx, dy in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            nx, ny = x + dx, y + dy
            if not (0 <= nx < H and 0 <= ny < W):
                continue
            if S[nx][ny] == '.':
                A += [i]
                B += [nx * W + ny]
                C += [1]
F = floyd_warshall(csr_matrix((C, (A, B)), shape=(H*W, H*W)))
print(int(np.max(F[F!=np.inf])))


Verdicts: Wrong Answer

Wrong Answer

Runtime Error

Accepted