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):
H, W = map(int, input().split())
S = [list('#' * (W + 2))]
for _ in range(H):
    S.append(list('#' + input() + '#'))
S.append(list('#' * (W + 2)))

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

inf = float('inf')

N = W * H
l = [[inf] * N for _ in range(N)]

for w in range(1, W + 1):
    for h in range(1, H + 1):
        if S[h][w] != '#':
            l[(h - 1) * W + w - 1][(h - 1) * W + w - 1] = 0
            for x, y in zip(dx, dy):
                if S[h + y][w + x] != '#':
                    l[(h - 1) * W + w - 1][(h + y - 1) * W + w + x - 1] = 1

ans = -1

# for k in range(N):

#         for j in range(N):
#             tmp = min(l[i][j], l[i][k] + l[k][j])
#             if tmp != inf:
#                 ans = max(ans, tmp)
#             l[i][j] = tmp

print(ans)


H, W = map(int, input().split())
S = [list('#' * (W + 2))]
for _ in range(H):
    S.append(list('#' + input() + '#'))
S.append(list('#' * (W + 2)))

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

inf = float('inf')

N = W * H
l = [[inf for _ in range(N)] for _ in range(N)]

for w in range(1, W + 1):
    for h in range(1, H + 1):
        if S[h][w] != '#':
            l[(h - 1) * W + w - 1][(h - 1) * W + w - 1] = 0
            for x, y in zip(dx, dy):
                if S[h + y][w + x] != '#':
                    l[(h - 1) * W + w - 1][(h + y - 1) * W + w + x - 1] = 1

for i in range(N):
    for j in range(N):
        for k in range(N):
            l[i][j] = min(l[i][j], l[i][k] + l[k][j])

for i in range(N):
    for j in range(N):
        for k in range(N):
            l[i][j] = min(l[i][j], l[i][k] + l[k][j])

ans = -1

for i in range(N):
    for j in range(N):
        k = l[i][j]
        if k != inf:
            ans = max(ans, k)
print(l)
print(ans)


H, W = map(int, input().split())
S = [list('#' * (W + 2))]
for _ in range(H):
    S.append(list('#' + input() + '#'))
S.append(list('#' * (W + 2)))

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

inf = float('inf')

N = W * H
l = [[inf] * N for _ in range(N)]

for w in range(1, W + 1):
    for h in range(1, H + 1):
        if S[h][w] != '#':
            l[(h - 1) * W + w - 1][(h - 1) * W + w - 1] = 0
            for x, y in zip(dx, dy):
                if S[h + y][w + x] != '#':
                    l[(h - 1) * W + w - 1][(h + y - 1) * W + w + x - 1] = 1

ans = -1

for k in range(N):
    for i in range(N):
        for j in range(N):
            tmp = min(l[i][j], l[i][k] + l[k][j])
            if tmp != inf:
                ans = max(ans, tmp)
            l[i][j] = tmp

print(ans)


H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()))

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

inf = float('inf')

import queue

ans = 0

for h in range(H):
    for w in range(W):
        if S[h][w] != '#':
            q = queue.Queue()
            d = [[inf for _ in range(W)] for _ in range(H)]
            
            q.put([h, w])
            d[h][w] = 0
            
            while not q.empty():
                p = q.get()
                # if p == g:
                #     break
                for i in range(4):
                    nx = p[1] + dx[i]
                    ny = p[0] + dy[i]
                    if nx >= 0 and nx < W and ny >= 0 and ny < H and S[ny][nx] != '#' and d[ny][nx] == inf:
                        q.put([ny, nx])
                        d[ny][nx] = d[p[0]][p[1]] + 1
            for hh in range(H):
                for ww in range(W):
                    if d[hh][ww] != inf:
                        ans = max(ans, d[hh][ww])

print(ans)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted