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):
from array import array

h, w = map(int, input().split())
maze = []
for i in range(h):
    maze.append(input())

g = []

for i in range(w * h):
    g.append(array('l', [10000000] * (w * h)))

for i in range(w * h):
    g[i][i] = 0

for i in range(h):
    for j in range(w):
        if maze[i][j] == '#':
            continue
        if maze[i][j] == '.':
            if i > 0 and maze[i-1][j] != '#':
                g[i * w + j][(i-1) * w + j] = 1
                g[(i-1) * w + j][i * w + j] = 1
            if i < h-1 and maze[i+1][j] != '#':
                g[i * w + j][(i+1) * w + j] = 1
                g[(i+1) * w + j][i * w + j] = 1
            if j > 0 and maze[i][j-1] != '#':
                g[i * w + j][i * w + j-1] = 1
                g[i * w + j-1][i * w + j] = 1
            if j < w-1 and maze[i][j+1] != '#':
                g[i * w + j][i * w + j+1] = 1
                g[i * w + j+1][i * w + j] = 1

def floyed(g, h, w):
    result = 0
    for k in range(h*w):
        for i in range(h*w):
            if i == k:
                continue
            if g[i][k] == 10000000:
                continue
            for j in range(h*w):
                if g[k][j] == 10000000:
                    continue
                if i == j or i == k or k == j:
                    continue
                if g[i][j] > g[i][k] + g[k][j]:
                    g[i][j] = g[i][k] + g[k][j]
                    if g[i][j] > result:
                        result = g[i][j]
    return result

print(floyed(g,h,w))


from array import array

h, w = map(int, input().split())
maze = []
for i in range(h):
    maze.append(input())

nodes = h*w

g = []

for i in range(nodes):
    g.append(array('l', [10000000] * (w * h)))

for i in range(nodes):
    g[i][i] = 0

for i in range(h):
    for j in range(w):
        if maze[i][j] == '#':
            continue
        if maze[i][j] == '.':
            if i > 0 and maze[i-1][j] != '#':
                g[i * w + j][(i-1) * w + j] = 1
                g[(i-1) * w + j][i * w + j] = 1
            if i < h-1 and maze[i+1][j] != '#':
                g[i * w + j][(i+1) * w + j] = 1
                g[(i+1) * w + j][i * w + j] = 1
            if j > 0 and maze[i][j-1] != '#':
                g[i * w + j][i * w + j-1] = 1
                g[i * w + j-1][i * w + j] = 1
            if j < w-1 and maze[i][j+1] != '#':
                g[i * w + j][i * w + j+1] = 1
                g[i * w + j+1][i * w + j] = 1

def floyed(g, nodes):
    result = 0
    for k in range(nodes):
        for i in range(nodes):
            if i == k:
                continue
            if g[i][k] == 10000000:
                continue
            for j in range(nodes):
                if g[k][j] == 10000000:
                    continue
                if i == j or i == k or k == j:
                    continue
                if g[i][j] > g[i][k] + g[k][j]:
                    g[i][j] = g[i][k] + g[k][j]
                    if g[i][j] < 10000000 and g[i][j] > result:
                        result = g[i][j]
    return result

print(floyed(g,nodes))


from array import array

h, w = map(int, input().split())
maze = []
for i in range(h):
    maze.append(input())

cnt = 0
for i in range(h):
    for j in range(w):
        if maze[i][j] == '.':
            cnt += 1

if cnt == w*h:
    print(w+h-2)
else:
    nodes = h*w

    g = []

    for i in range(nodes):
        g.append(array('l', [10000000] * (w * h)))

    for i in range(nodes):
        g[i][i] = 0

    for i in range(h):
        for j in range(w):
            if maze[i][j] == '#':
                continue
            if maze[i][j] == '.':
                if i > 0 and maze[i-1][j] != '#':
                    g[i * w + j][(i-1) * w + j] = 1
                    g[(i-1) * w + j][i * w + j] = 1
                if i < h-1 and maze[i+1][j] != '#':
                    g[i * w + j][(i+1) * w + j] = 1
                    g[(i+1) * w + j][i * w + j] = 1
                if j > 0 and maze[i][j-1] != '#':
                    g[i * w + j][i * w + j-1] = 1
                    g[i * w + j-1][i * w + j] = 1
                if j < w-1 and maze[i][j+1] != '#':
                    g[i * w + j][i * w + j+1] = 1
                    g[i * w + j+1][i * w + j] = 1

    def floyed(g, nodes):
        result = 0
        for k in range(nodes):
            for i in range(nodes):
                if i == k:
                    continue
                if g[i][k] == 10000000:
                    continue
                for j in range(nodes):
                    if g[k][j] == 10000000:
                        continue
                    if i == j or i == k or k == j:
                        continue
                    if g[i][j] > g[i][k] + g[k][j]:
                        g[i][j] = g[i][k] + g[k][j]

    floyed(g,nodes)

    result = 0
    for i in range(nodes):
        for j in range(nodes):
            if g[i][j] < 10000000 and g[i][j] > result:
                result = g[i][j]
    print(result)


Verdicts: Wrong Answer

Wrong Answer

Accepted