Problem:


Score : 400 points

Problem Statement

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.

Constraints

1 \leq H,W \leq 20

S_{ij} is . or #.

S contains at least two occurrences of ..

Any road square can be reached from any road square in zero or more moves.

Input

Input is given from Standard Input in the following format:

H W
S_{11}...S_{1W}
:
S_{H1}...S_{HW}

Output

Print the maximum possible number of moves Aoki has to make.

Sample Input 1

3 3
...
...
...

Sample Output 1

4

If Takahashi chooses the top-left square as the starting square and the bottom-right square as the goal square, Aoki has to make four moves.

Sample Input 2

3 5
...#.
.#.#.
.#...

Sample Output 2

10

If Takahashi chooses the bottom-left square as the starting square and the top-right square as the goal square, Aoki has to make ten moves.



Sample input:
3 3
...
...
...


Sample output:
4

Submission status: Wrong Answer

Solution code:
from collections import deque
 
H, W = map(int, input().split())
S = [input() for _ in range(H)]
 
INF = 10**15
d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
 
answer = 0
for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      continue
    else:
      dist = [[INF for _ in range(W)] for _ in range(H)]
      dist[i][j] = 0
      stack = deque([(i, j)])
      
      while stack:
        s = stack.popleft()
        si, sj = s
        for di, dj in d:
          if 0 <= si + di <= H - 1 and 0 <= sj + dj <= W - 1 and S[si + di][sj + dj] != "#":
            if dist[si + di][sj + dj] > dist[si][sj] + 1:
              dist[si + di][sj + dj] = dist[si][sj] + 1
              stack.append((si + di, sj + dj))
      #print(dist)
 
      max_dist = 0
      for ii in range(H):
        print(dist[ii])
        for jj in range(W):
          if dist[ii][jj] != INF:
            max_dist = max(max_dist, dist[ii][jj])
      print("")
      answer = max(max_dist, answer)
      
print(answer)

Submission status: Wrong Answer

Solution code:
from collections import deque
 
H, W = map(int, input().split())
S = [input() for _ in range(H)]
 
INF = 10**2
d = [(1, 0), (-1, 0), (0, 1), (0, -1)]
 
answer = 0
for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      continue
    else:
      dist = [[INF for _ in range(W)] for _ in range(H)]
      dist[i][j] = 0
      stack = deque([(i, j)])
      
      while stack:
        s = stack.popleft()
        si, sj = s
        for di, dj in d:
          if 0 <= si + di <= H - 1 and 0 <= sj + dj <= W - 1 and S[si + di][sj + dj] != "#":
            if dist[si + di][sj + dj] > dist[si][sj] + 1:
              dist[si + di][sj + dj] = dist[si][sj] + 1
              stack.append((si + di, sj + dj))
      #print(dist)
 
      max_dist = 0
      for ii in range(H):
        #print(dist[ii])
        for jj in range(W):
          if dist[ii][jj] != INF:
            max_dist = max(max_dist, dist[ii][jj])
      #print("")
      answer = max(max_dist, answer)
      
print(answer)

Submission status: Wrong Answer

Solution code:
import copy

H, W = map(int, input().split(' '))
maze = []
for i in range(H):
    maze.append(input())


def search(visit, queue):
    while len(queue) > 0:
        x, y, d = queue.pop(0)
        index = y * W + x
        visit[index] = d
        # top
        if y > 0 and visit[index - W] == 0 and maze[y - 1][x] == '.':
            queue.append((x, y - 1, visit[index] + 1))
        # left
        if x > 0 and visit[index - 1] == 0 and maze[y][x - 1] == '.':
            queue.append((x - 1, y, visit[index] + 1))
        # right
        if x < W - 1 and visit[index + 1] == 0 and maze[y][x + 1] == '.':
            queue.append((x + 1, y, visit[index] + 1))
        # bottom
        if y < H - 1 and visit[index + W] == 0 and maze[y + 1][x] == '.':
            queue.append((x, y + 1, visit[index] + 1))


best = 0
zero = [0 for _ in range(H * W)]
for i in range(H):
    for j in range(W):
        #visit = np.zeros((H, W), dtype=np.int32)
        visit = copy.copy(zero)
        search(visit, [(j, i, 1)])
        dist = max(visit)
        if dist > best:
            best = dist
print(best - 1)

Submission status: Wrong Answer

Solution code:
# 幅優先探索（BFS)
def calc_maze(maze, sh, sw):
    H = len(maze)
    W = len(maze[0])

    # 距離行列
    distance = []
    for i in range(H):
        distance.append([])
        for j in range(W):
            distance[i].append(-1)

    # 座標格納キュー
    # (H座標, W座標, 距離)
    queue = []
    # 初期ポジション格納
    queue.append((sh, sw))
    distance[sh][sw] = 0
    # 最大距離を格納
    max_distance = 0
    # 探索する場所がなくなるまで
    while queue:
        h, w = queue.pop(0)

        for i in range(0, 4):
            nh, nw = h + [1, 0, -1, 0][i], w + [0, 1, 0, -1][i]
            if (0 <= nh and nh < H and 0 <= nw and nw < W and maze[nh][nw] != '#' and distance[nh][nw] == -1):
                queue.append((nh, nw))
                distance[nh][nw] = distance[h][w] + 1
                # 最大距離更新
                if max_distance < (distance[h][w] + 1):
                    max_distance = distance[h][w] + 1
    return max(max(d) for d in distance)



def main():
    H, W = map(int, input().split())
    maze = []
    ans = 0

    for _ in range(H):
        l = list(input())
        maze.append(l)

    for j in range(W):
        for i in range(H):
            tmp = calc_maze(maze, i, j)
            if ans < tmp:
                #print(dis)
                ans = tmp
    print(ans)
    return 0
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split(' '))
maze = []
for i in range(H):
    maze.append(input())


def search(visit, queue):
    while len(queue) > 0:
        x, y, d = queue.pop(0)
        index = y * W + x
        visit[index] = d
        # top
        if y > 0 and visit[index - W] == 0 and maze[y - 1][x] == '.':
            queue.append((x, y - 1, visit[index] + 1))
        # left
        if x > 0 and visit[index - 1] == 0 and maze[y][x - 1] == '.':
            queue.append((x - 1, y, visit[index] + 1))
        # right
        if x < W - 1 and visit[index + 1] == 0 and maze[y][x + 1] == '.':
            queue.append((x + 1, y, visit[index] + 1))
        # bottom
        if y < H - 1 and visit[index + W] == 0 and maze[y + 1][x] == '.':
            queue.append((x, y + 1, visit[index] + 1))


best = 0
for i in range(H):
    for j in range(W):
        #visit = np.zeros((H, W), dtype=np.int32)
        visit = [0 for _ in range(H * W)]
        search(visit, [(j, i, 1)])
        dist = max(visit)
        if dist > best:
            best = dist
print(best - 1)

Submission status: Wrong Answer

Solution code:
# 幅優先探索（BFS)
def calc_maze(maze, sh, sw):
    H = len(maze)
    W = len(maze[0])

    # 距離行列
    distance = []
    for i in range(H):
        distance.append([])
        for j in range(W):
            distance[i].append(-1)

    # 座標格納キュー
    # (H座標, W座標, 距離)
    queue = []
    i = 0
    # 初期ポジション格納
    queue.append((sh, sw))
    distance[sh][sw] = 0
    # 最大距離を格納
    max_distance = 0
    # 探索する場所がなくなるまで
    while len(queue) > 0:
        h, w = queue.pop(0)

        for i in range(0, 4):
            nh, nw = h + [1, 0, -1, 0][i], w + [0, 1, 0, -1][i]
            if (0 <= nh and nh < H and 0 <= nw and nw < W and maze[nh][nw] != '#' and distance[nh][nw] == -1):
                queue.append((nh, nw))
                distance[nh][nw] = distance[h][w] + 1
                # 最大距離更新
                if max_distance < (distance[h][w] + 1):
                    max_distance = distance[h][w] + 1
    return max_distance, distance



def main():
    H, W = map(int, input().split())
    maze = []
    ans = 0

    for _ in range(H):
        l = list(input())
        maze.append(l)

    for j in range(W):
        for i in range(H):
            tmp, dis = calc_maze(maze, i, j)
            if ans < tmp:
                #print(dis)
                ans = tmp
    print(ans)
    return 0
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 幅優先探索（BFS)
def calc_maze(maze, sh, sw):
    H = len(maze)
    W = len(maze[0])

    # 距離行列
    distance = []
    for i in range(H):
        distance.append([])
        for j in range(W):
            distance[i].append(-10000)

    # 座標格納キュー
    # (H座標, W座標, 距離)
    queue = []
    i = 0
    # 初期ポジション格納
    queue.append((sh, sw))
    distance[sh][sw] = 0
    # 最大距離を格納
    max_distance = 0
    # 探索する場所がなくなるまで
    while len(queue) > 0:
        h, w = queue.pop(0)

        for i in range(0, 4):
            nh, nw = h + [1, 0, -1, 0][i], w + [0, 1, 0, -1][i]
            if (0 <= nh and nh < H and 0 <= nw and nw < W and maze[nh][nw] != '#' and distance[nh][nw] < 0):
                queue.append((nh, nw))
                distance[nh][nw] = distance[h][w] + 1
                # 最大距離更新
                if max_distance < (distance[h][w] + 1):
                    max_distance = distance[h][w] + 1
    return max_distance, distance



def main():
    H, W = map(int, input().split())
    maze = []
    ans = 0

    for _ in range(H):
        l = list(input())
        maze.append(l)

    for j in range(W):
        for i in range(H):
            tmp, dis = calc_maze(maze, i, j)
            if ans < tmp:
                #print(dis)
                ans = tmp
    print(ans)
    return 0
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
input = sys.stdin.readline

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

ans = 0

for j in range(1, H+1):
    for i in range(1, W+1):
        if S[j][i] != ".":
            continue
        m = [[-1] * (W+2) for _ in range(H+2)]

        q = deque()
        q.append((j, i, 0))

        while q:
            y, x, v = q.popleft()
            for dj, di in ((y, x + 1), (y, x-1), (y+1, x), (y-1, x)):
                if S[dj][di] == "#" or m[dj][di] != -1:
                    continue
                q.append((dj, di, v+1))
                m[dj][di] = v+1
                if ans < v+1:
                    ans = v+1
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split(' '))
maze = []
for i in range(H):
    maze.append(input())


def search(visit, queue):
    while len(queue) > 0:
        x, y, d = queue.pop(0)
        visit[y][x] = d
        # top
        if y > 0 and visit[y - 1][x] == 0 and maze[y - 1][x] == '.':
            queue.append((x, y - 1, visit[y][x] + 1))
        # right
        if x < W - 1 and visit[y][x + 1] == 0 and maze[y][x + 1] == '.':
            queue.append((x + 1, y, visit[y][x] + 1))
        # left
        if x > 0 and visit[y][x - 1] == 0 and maze[y][x - 1] == '.':
            queue.append((x - 1, y, visit[y][x] + 1))
        # bottom
        if y < H - 1 and visit[y + 1][x] == 0 and maze[y + 1][x] == '.':
            queue.append((x, y + 1, visit[y][x] + 1))


best = 0
for i in range(H // + 1):
    for j in range(W // 2 + 1):
        visit = np.zeros((H, W), dtype=np.int32)
        search(visit, [(j, i, 1)])
        dist = np.max(visit)
        if dist > best:
            best = dist
print(best - 1)

Submission status: Wrong Answer

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


def f(v1, v2):
    # print('--f', v1, v2)
    m = []
    for i in range(H):
        m.append([None]*W)
    m[v1][v2] = 0

    for i in range(H):
        for j in range(W):
            if S[i][j] == '#':
                continue
            elif m[i][j] == None:
                continue
            else:
                cur = m[i][j]
                u = i-1
                if 0 <= u and S[u][j] == '.':
                    if m[u][j] is None:
                        m[u][j] = cur + 1
                    m[u][j] = min(m[u][j], cur+1)
                d = i+1
                if d <= H-1 and S[d][j] == '.':
                    if m[d][j] is None:
                        m[d][j] = cur + 1
                    m[d][j] = min(m[d][j], cur+1)
                l = j-1
                if 0 <= l and S[i][l] == '.':
                    if m[i][l] is None:
                        m[i][l] = cur + 1
                    m[i][l] = min(m[i][l], cur+1)
                r = j+1
                if r <= W-1 and S[i][r] == '.':
                    if m[i][r] is None:
                        m[i][r] = cur + 1
                    m[i][r] = min(m[i][r], cur+1)
    # print(m)

    # 逆から探索
    for i_ in range(H):
        for j_ in range(W):
            i = H - i_ - 1
            j = W - j_ - 1
            if S[i][j] == '#':
                continue
            elif m[i][j] == None:
                continue
            else:
                cur = m[i][j]
                u = i-1
                if 0 <= u and S[u][j] == '.':
                    if m[u][j] is None:
                        m[u][j] = cur + 1
                    m[u][j] = min(m[u][j], cur+1)
                d = i+1
                if d <= H-1 and S[d][j] == '.':
                    if m[d][j] is None:
                        m[d][j] = cur + 1
                    m[d][j] = min(m[d][j], cur+1)
                l = j-1
                if 0 <= l and S[i][l] == '.':
                    if m[i][l] is None:
                        m[i][l] = cur + 1
                    m[i][l] = min(m[i][l], cur+1)
                r = j+1
                if r <= W-1 and S[i][r] == '.':
                    if m[i][r] is None:
                        m[i][r] = cur + 1
                    m[i][r] = min(m[i][r], cur+1)
    # print(m)

    for i in range(H):
        for j in range(W):
            if S[i][j] == '#':
                continue
            elif m[i][j] == None:
                continue
            else:
                cur = m[i][j]
                u = i-1
                if 0 <= u and S[u][j] == '.':
                    if m[u][j] is None:
                        m[u][j] = cur + 1
                    m[u][j] = min(m[u][j], cur+1)
                d = i+1
                if d <= H-1 and S[d][j] == '.':
                    if m[d][j] is None:
                        m[d][j] = cur + 1
                    m[d][j] = min(m[d][j], cur+1)
                l = j-1
                if 0 <= l and S[i][l] == '.':
                    if m[i][l] is None:
                        m[i][l] = cur + 1
                    m[i][l] = min(m[i][l], cur+1)
                r = j+1
                if r <= W-1 and S[i][r] == '.':
                    if m[i][r] is None:
                        m[i][r] = cur + 1
                    m[i][r] = min(m[i][r], cur+1)
    # print(m)
    ans = 0
    for i in range(H):
        for j in range(W):
            if m[i][j] == None:
                continue
            else:
                ans = max(ans, m[i][j])
    return ans

r = -1
#　スタート位置
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            continue
        else:
            x = f(i, j)
            r = max(r, x)
print(r)

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np
import sys
def input():
  return sys.stdin.readline()[:-1]

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

def f(data):
  m[data[0]][data[1]]=data[2]
  if m[data[0]-1][data[1]]<0 and S[data[0]-1][data[1]]=='.':
    q.append([data[0]-1, data[1], data[2]+1])
  if m[data[0]+1][data[1]]<0 and S[data[0]+1][data[1]]=='.':
    q.append([data[0]+1, data[1], data[2]+1])
  if m[data[0]][data[1]-1]<0 and S[data[0]][data[1]-1]=='.':
    q.append([data[0], data[1]-1, data[2]+1])
  if m[data[0]][data[1]+1]<0 and S[data[0]][data[1]+1]=='.':
    q.append([data[0], data[1]+1, data[2]+1])

out=0
for x in range(1, H+1):
  for y in range(1, W+1):
    if S[x][y]=='#':
      continue
    q=deque()
    q.append([x, y, 0])
    m=[[-1]*(W+2) for i in range(H+2)]
    while len(q)>0:
      data=q.popleft()
      f(data)
    tmp=np.min(m)
    if tmp>out:
      out=tmp
      
print(out)

Submission status: Wrong Answer

Solution code:
# 幅優先探索（BFS)
def calc_maze(maze, sh, sw):
    H = len(maze)
    W = len(maze[0])

    # 距離行列
    distance = []
    for i in range(H):
        distance.append([])
        for j in range(W):
            distance[i].append(-10000)

    # 座標格納キュー
    # (H座標, W座標, 距離)
    queue = []
    i = 0
    # 初期ポジション格納
    queue.append((sh, sw))
    distance[sh][sw] = 0
    # 最大距離を格納
    max_distance = 0
    # 探索する場所がなくなるまで
    while len(queue) > 0:
        h, w = queue.pop(0)

        for i in range(0, 4):
            nh, nw = h + [1, 0, -1, 0][i], w + [0, 1, 0, -1][i]
            if (0 <= nh and nh < H and 0 <= nw and nw < W and maze[nh][nw] != '#' and distance[nh][nw] < 0):
                queue.append((nh, nw))
                distance[nh][nw] = distance[h][w] + 1
                # 最大距離更新
                if max_distance < (distance[h][w] + 1):
                    max_distance = distance[h][w] + 1
    return max_distance



def main():
    H, W = map(int, input().split())
    maze = []
    ans = 0

    for _ in range(H):
        l = list(input())
        maze.append(l)

    for i in range(H):
        for j in range(W):
            tmp = calc_maze(maze, i, j)
            if ans < tmp:
                ans = tmp
    print(ans)
    return 0
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split(' '))
maze = []
for i in range(H):
    maze.append(input())


def search(visit, queue):
    while len(queue) > 0:
        x, y, d = queue.pop(0)
        visit[y][x] = d
        # top
        if y > 0 and visit[y - 1][x] == 0 and maze[y - 1][x] == '.':
            queue.append((x, y - 1, visit[y][x] + 1))
        # right
        if x < W - 1 and visit[y][x + 1] == 0 and maze[y][x + 1] == '.':
            queue.append((x + 1, y, visit[y][x] + 1))
        # left
        if x > 0 and visit[y][x - 1] == 0 and maze[y][x - 1] == '.':
            queue.append((x - 1, y, visit[y][x] + 1))
        # bottom
        if y < H - 1 and visit[y + 1][x] == 0 and maze[y + 1][x] == '.':
            queue.append((x, y + 1, visit[y][x] + 1))


best = 0
for i in range(H):
    for j in range(W):
        visit = np.zeros((H, W), dtype=np.int32)
        search(visit, [(j, i, 1)])
        dist = np.max(visit)
        if dist > best:
            best = dist
print(best - 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W=map(int, input().split())
S=['#'*(W+2)]+['#'+input()+'#' for i in range(H)]+['#'*(W+2)]

def f(data):
  m[data[0]][data[1]]=data[2]
  if m[data[0]-1][data[1]]<0 and S[data[0]-1][data[1]]=='.':
    q.append([data[0]-1, data[1], data[2]+1])
  if m[data[0]+1][data[1]]<0 and S[data[0]+1][data[1]]=='.':
    q.append([data[0]+1, data[1], data[2]+1])
  if m[data[0]][data[1]-1]<0 and S[data[0]][data[1]-1]=='.':
    q.append([data[0], data[1]-1, data[2]+1])
  if m[data[0]][data[1]+1]<0 and S[data[0]][data[1]+1]=='.':
    q.append([data[0], data[1]+1, data[2]+1])

out=0
for x in range(1, H+1):
  for y in range(1, W+1):
    q=deque()
    q.append([x, y, 0])
    m=[[-1]*(W+2) for i in range(H+2)]
    while len(q)>0:
      data=q.popleft()
      f(data)
    tmp=max([max(m[i]) for i in range(1, H+1)])
    if tmp>out:
      out=tmp
      
print(out)

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split(' '))
maze = []
for i in range(H):
    maze.append(input())


def search(visit, queue):
    while len(queue) > 0:
        x, y, d = queue.pop(0)
        visit[y][x] = d
        for i in range(3):
            dy = y + i - 1
            if dy == H or dy == -1:
                continue
            for j in range(3):
                if i == 0 and j == 0:
                    continue
                if i == 2 and j == 2:
                    continue
                if i == 0 and j == 2:
                    continue
                if i == 2 and j == 0:
                    continue
                dx = x + j - 1
                if dx == W or dx == -1:
                    continue
                if visit[dy][dx] > 0:
                    continue
                if maze[dy][dx] == '.':
                    queue.append((dx, dy, visit[y][x] + 1))


best = 0
for i in range(H):
    for j in range(W):
        visit = np.zeros((H, W), dtype=np.int32)
        search(visit, [(j, i, 0)])
        print(visit)
        dist = np.max(visit)
        if dist > best:
            best = dist
print(best - 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())

maze = [list(input()) for _ in range(h)]
#print(maze)
ans = 0
def solve(sx, sy):
    INF = 1000000
    reached = [[INF for _ in range(w)] for _ in range(h)]
    d = deque()
    d.append([sx, sy])
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]    
    while d:
        x, y = d.popleft()
        for i in range(4):
            next_y = y + dy[i]
            next_x = x + dx[i]
            if next_x < 0 or next_x > h - 1 or next_y < 0 or next_y > w - 1:
                continue
            if maze[next_x][next_y] == "#":
                continue
            if reached[next_x][next_y] != INF:
                continue
            d.append([next_x, next_y])
            reached[next_x][next_y] = reached[x][y] + 1
    gl = 0
    for i in range(h):
        for j in range(w):
            if maze[i][j] == ".":
                gl = max(gl, reached[i][j])
    return gl

for i in range(h):
    for j in range(w):
        if maze[i][j] == ".":
            res = solve(i, j)
            ans = max(ans, res)
print(ans)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
from collections import deque
ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j]=='#':
      continue
    inf = 10**5
    A = [[inf]*W for _ in range(H)]
    A[i][j]=0
    judge = [[False]*W for _ in range(H)]
    judge[i][j]=True
    q = deque([[i,j]])
    while q:
      a = q.pop()
      x=a[0]
      y=a[1]
      b = A[x][y]
      for m in [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]:
        h = m[0]
        w = m[1]
        if (h<0)or(h>H-1)or(w<0)or(w>W-1):
          continue
        if S[h][w]=='#':
          continue
        c = b+1
        if judge[h][w]:
          continue
        A[h][w]=c
        judge[h][w]=True
        q.append([h,w])
        
    A = [a for b in A for a in b if a!=inf]
    ans = max(ans,max(A))
    
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np


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


def bfs(initial):
    # goal判定はなく、一番遠いところで返す
    dist = [[-1] * W for _ in range(H)]
    dist[initial[0]][initial[1]] = 0
    frontier = deque()
    frontier.append((initial, None))
    # tuple
    explored = {initial}
    while len(frontier):
        locations = []
        current_node = frontier.popleft()
        current_state = current_node[0]
        x, y = current_state
        if (x + 1 < H) and (maze[x + 1][y] != '#'):
            locations.append((x + 1, y))
            dist[x + 1][y] = dist[x][y] + 1
        # 上に行けるかの判定
        if (x - 1 >= 0) and (maze[x - 1][y] != '#'):
            locations.append((x - 1, y))
            dist[x - 1][y] = dist[x][y] + 1
            # 右に行けるかの判定
        if (y + 1 < W) and (maze[x][y + 1] != '#'):
            locations.append((x, y + 1))
            dist[x][y + 1] = dist[x][y] + 1
        # 左に行けるかの判定
        if (y - 1 >= 0) and (maze[x][y - 1] != '#'):
            locations.append((x, y - 1))
            dist[x][y-1] = dist[x][y] + 1
        for child in locations:
            if str(child) in [str(i) for i in explored]:
                continue
            explored.add(child)
            frontier.append((child, current_node))
    return np.max(dist)


ans = 0
for x in range(H):
    for y in range(W):
        if maze[x][y] == '.':
            solution = bfs((x, y))
            if ans < solution:
                ans = solution

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
 
H,W=map(int, sys.stdin.readline().split())
S=[ sys.stdin.readline().strip() for _ in range(H) ]
 
D=[(1,0),(-1,0),(0,1),(0,-1)]
 
def bfs(start):
    Visit={}
    q=deque()
    q.append(start)
    L=defaultdict(lambda: float("inf")) 
    L[start]=0
 
    while q:
        fro_x,fro_y=q.popleft()
        for dx,dy in D:
            to_x=fro_x+dx
            to_y=fro_y+dy
            alt=L[(fro_x,fro_y)]+1
            if 0<=to_x<H and 0<=to_y<W:
                if S[to_x][to_y]=="." and alt<L[(to_x,to_y)]:
                    L[(to_x,to_y)]=alt
                    q.append((to_x,to_y))
    return max(L.values())
 
ans=0
for h in range(H):
    for w in range(W):
        ans=max(ans,bfs((h,w)))
print ans

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]
from collections import deque
ans = 0
for i in range(H):
  for j in range(W):
    if S[i][j]=='#':
      continue
    inf = 10**5
    A = [[inf]*W for _ in range(H)]
    A[i][j]=0
    judge = [[False]*W for _ in range(H)]
    q = deque([[i,j]])
    while q:
      a = q.popleft()
      x=a[0]
      y=a[1]
      b = A[x][y]
      for m in [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]:
        h = m[0]
        w = m[1]
        if (h<0)or(h>H-1)or(w<0)or(w>W-1):
          continue
        if S[h][w]=='#':
          continue
        c = b+1
        if judge[h][w]:
          continue
        A[h][w]=c
        judge[h][w]=True
        q.append([h,w])
        
    A = [a for b in A for a in b if a!=inf]
    ans = max(ans,max(A))
    
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
h, w = map(int, input().split())

maze = [list(input()) for _ in range(h)]
#print(maze)
ans = 0
def solve(sx, sy):
    reached = [[0 for _ in range(w)] for _ in range(h)]
    d = deque()
    d.append([sx, sy])
    dx = [-1, 0, 1, 0]
    dy = [0, 1, 0, -1]    
    while d:
        x, y = d.popleft()
        for i in range(4):
            next_y = y + dy[i]
            next_x = x + dx[i]
            if next_x < 0 or next_x > h - 1 or next_y < 0 or next_y > w - 1:
                continue
            if maze[next_x][next_y] == "#":
                continue
            if reached[next_x][next_y] != 0:
                continue
            d.append([next_x, next_y])
            reached[next_x][next_y] = reached[x][y] + 1
    gl = 0
    for i in range(h):
        for j in range(w):
            if i != sx and j != sy:
                gl = max(gl, reached[i][j])
    return gl

for i in range(h):
    for j in range(w):
        if maze[i][j] == ".":
            res = solve(i, j)
            ans = max(ans, res)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
 
H,W=map(int, sys.stdin.readline().split())
S=[ sys.stdin.readline().strip() for _ in range(H) ]
 
D=[(1,0),(-1,0),(0,1),(0,-1)]
 
def bfs(start):
    Visit={}
    q=deque()
    q.append(start)
    L=defaultdict(lambda: float("inf")) 
    L[start]=0
 
    while q:
        fro_x,fro_y=q.popleft()
        for dx,dy in D:
            to_x=fro_x+dx
            to_y=fro_y+dy
            alt=L[(fro_x,fro_y)]+1
            if 0<=to_x<H and 0<=to_y<W:
                if S[to_x][to_y]=="." and alt<L[to_x,to_y]:
                    L[(to_x,to_y)]=alt
                    q.append((to_x,to_y))
    return max(L.values())
 
ans=0
for h in range(H):
    for w in range(W):
        ans=max(ans,bfs((h,w)))
print ans

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int,input().split())
maze = []
def mazecal(hs,ws):
    calmaze = [[-1 for _ in range(w)] for _ in range(h)]
    que = deque([[hs,ws,0]])
    smax = 0
    while len(que)!=0:
        cur = que.popleft()
        calmaze[cur[0]][cur[1]] = cur[2]
        smax = max(smax,cur[2])
        if cur[0]>0   and maze[cur[0]-1][cur[1]]==0:
            if calmaze[cur[0]-1][cur[1]] ==-1:
                calmaze[cur[0]-1][cur[1]] = 0
                que.append([cur[0]-1,cur[1],  cur[2]+1])
        if cur[0]<h-1 and maze[cur[0]+1][cur[1]]==0:
            if calmaze[cur[0]+1][cur[1]] ==-1:
                calmaze[cur[0]+1][cur[1]] = 0
                que.append([cur[0]+1,cur[1],  cur[2]+1])
        if cur[1]>0   and maze[cur[0]][cur[1]-1]==0:
            if calmaze[cur[0]][cur[1]-1] ==-1:
                calmaze[cur[0]][cur[1]-1] = 0
                que.append([cur[0]  ,cur[1]-1,cur[2]+1])
        if cur[1]<w-1 and maze[cur[0]][cur[1]+1]==0:
            calmaze[cur[0]][cur[1]+1] = 0
            if calmaze[cur[0]][cur[1]+1] ==-1:
                que.append([cur[0]  ,cur[1]+1,cur[2]+1])
    return smax

for i in range(h):maze.append([0 if c=='.' else 1 for c in input()])
gmax = 0
for i in range(h):
    for j in range(w):
        if maze[i][j]==0:gmax = max(gmax,mazecal(i,j))
print(gmax)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

def sol():
    h,w=map(int,input().split())
    d=[]
    for i in range(h):
        s=input()
        d.append(s)
    x=h*w
    p=[[x+1]*x for i in range(x)]

    for i in range(x):
        ih,iw=i//w,i%w
        if d[ih][iw]!=".": continue
        for j in range(x):
            ih,iw=i//w,i%w
            jh,jw=j//w,j%w
            if d[jh][jw]!=".": continue
            if ih==jh and abs(iw-jw)==1:
                p[i][j]=1
                p[j][i]=1
            if iw==jw and abs(ih-jh)==1:
                p[i][j]=1
                p[j][i]=1
            for k in range(x):
                ih,iw=i//w,i%w
                jh,jw=j//w,j%w
                kh,kw=k//w,k%w
                if ih==kh and abs(iw-kw)==1:
                    p[i][k]=1
                    p[k][i]=1
                if iw==kw and abs(ih-kh)==1:
                    p[i][k]=1
                    p[k][i]=1
                if jh==kh and abs(jw-kw)==1:
                    p[k][j]=1
                    p[j][k]=1
                if jw==kw and abs(jh-kh)==1:
                    p[k][j]=1
                    p[j][k]=1
                if d[ih][iw]==d[jh][jw]==d[kh][kw]==".":
                    p[j][k]=min(p[j][k],p[j][i]+p[i][k])
    ans=0
    for i in p:
        for j in i:
            if j<x+1:
                ans=max(ans,j)
    print(ans)

if __name__=="__main__":
    sol()