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:
import math
import itertools
import queue


[H, W] = map(int, input().rstrip().split(" "))
S = []


for h in range(H):
    Ss = input().rstrip()
    S.append(Ss)

max_cost = 0


for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        que = queue.Queue()
        history = [ [float('inf') for w in range(W)] for h in range(H)]
        que.put([h, w, 0])
        while que.empty() == False:
            [x, y, cost] = que.get()
            history[x][y] = cost
            if x + 1 < H and S[x+1][y] != "#" and history[x+1][y] > cost + 1:
                que.put([x + 1, y, cost + 1])
            if y + 1 < W and S[x][y+1] != "#" and history[x][y+1] > cost + 1:
                que.put([x, y + 1, cost + 1])
            if y -1 >= 0 and S[x][y-1] != "#" and history[x][y-1] > cost + 1:
                que.put([x, y - 1, cost + 1])
            if x - 1 >= 0 and S[x-1][y] != "#" and history[x-1][y] > cost + 1:
                que.put([x-1, y, cost + 1])
        for h in range(H):
            for w in range(W):
                if history[h][w] == float('inf'):
                    history[h][w] = 0
        current_max_cost = max(list(itertools.chain.from_iterable(history)))
        if max_cost < current_max_cost:
            max_cost = current_max_cost

print(max_cost)

Submission status: Wrong Answer

Solution code:
import math
import itertools

[H, W] = map(int, input().rstrip().split(" "))
S = []


for h in range(H):
    Ss = input().rstrip()
    S.append(Ss)

max_cost = 0


for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        que = []
        history = [ [float('inf') for w in range(W)] for h in range(H)]
        que.append([h, w, 0])
        while len(que) != 0:
            [x, y, cost] = que.pop(0)
            history[x][y] = cost
            if x + 1 < H and S[x+1][y] != "#" and history[x+1][y] > cost + 1:
                que.append([x + 1, y, cost + 1])
            if y + 1 < W and S[x][y+1] != "#" and history[x][y+1] > cost + 1:
                que.append([x, y + 1, cost + 1])
            if y -1 >= 0 and S[x][y-1] != "#" and history[x][y-1] > cost + 1:
                que.append([x, y - 1, cost + 1])
            if x - 1 >= 0 and S[x-1][y] != "#" and history[x-1][y] > cost + 1:
                que.append([x-1, y, cost + 1])
        for h in range(H):
            for w in range(W):
                if history[h][w] == float('inf'):
                    history[h][w] = 0
        current_max_cost = max(list(itertools.chain.from_iterable(history)))
        if max_cost < current_max_cost:
            max_cost = current_max_cost

print(max_cost)

Submission status: Wrong Answer

Solution code:
import math
import itertools

[H, W] = map(int, input().rstrip().split(" "))
S = []


for h in range(H):
    Ss = input().rstrip()
    S.append(Ss)

max_cost = 0


for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        que = []
        history = [ [float('inf') for w in range(W)] for h in range(H)]
        que.append([h, w, 0])
        while len(que) != 0:
            [x, y, cost] = que.pop()
            history[x][y] = cost
            if x + 1 < H and S[x+1][y] != "#" and history[x+1][y] > cost + 1:
                que.append([x + 1, y, cost + 1])
            if y + 1 < W and S[x][y+1] != "#" and history[x][y+1] > cost + 1:
                que.append([x, y + 1, cost + 1])
            if y -1 >= 0 and S[x][y-1] != "#" and history[x][y-1] > cost + 1:
                que.append([x, y - 1, cost + 1])
            if x - 1 >= 0 and S[x-1][y] != "#" and history[x-1][y] > cost + 1:
                que.append([x-1, y, cost + 1])
        for h in range(H):
            for w in range(W):
                if history[h][w] == float('inf'):
                    history[h][w] = 0
        current_max_cost = max(list(itertools.chain.from_iterable(history)))
        if max_cost < current_max_cost:
            max_cost = current_max_cost

print(max_cost)

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))

#迷路の作成
graph = []
for i in range(H):
    row = input()
    graph.append(list(row))
#迷路と同じ形の何もない空間を作成
ans = 0
for i in range(H):
    for j in range(W):
        costs = []
        for i in range(H):
            costs.append([-1] * W)
        sy = i
        sx = j
        if graph[sy][sx] == "#":
            continue
        costs[sy][sx] = 0
        q = [[sy, sx]]
        #移動方向
        di = [1, -1, 0, 0]
        dj = [0, 0, -1, 1]

        while len(q) != 0:
            y, x = q.pop(0)

            for k in range(4):
                t = y + di[k]
                u = x + dj[k]
                if not (0<=t<H and 0<=u<W):
                    continue
                if graph[t][u] == '#':
                    continue
                if costs[t][u] != -1:
                    continue
                costs[t][u] = costs[y][x] + 1
                q.append([t, u])
        ans = max(ans,max(max(costs)))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))

#迷路の作成
graph = []
for i in range(H):
    row = input()
    graph.append(list(row))
#迷路と同じ形の何もない空間を作成
ans = 0
for i in range(H):
    for j in range(W):
        costs = []
        for i in range(H):
            costs.append([-1] * W)
        sy = i
        sx = j
        if graph[sy][sx] == "#":
            continue
        costs[sy][sx] = 0
        q = [[sy, sx]]
        #移動方向
        di = [1, -1, 0, 0]
        dj = [0, 0, -1, 1]

        while len(q) != 0:
            y, x = q.pop(0)

            for k in range(4):
                t = y + di[k]
                u = x + dj[k]
                if t < 0 or u < 0 or t >= H or u >= W:
                    continue
                if graph[t][u] == '#':
                    continue
                if costs[t][u] != -1:
                    continue
                costs[t][u] = costs[y][x] + 1
                q.append([t, u])
        ans = max(ans,max(max(costs)))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))

#迷路の作成
graph = []
for i in range(H):
    row = input()
    graph.append(list(row))
#迷路と同じ形の何もない空間を作成
ans = 0
cost_max = 0
for i in range(H):
    for j in range(W):

        costs = []
        for i in range(H):
            costs.append([-1] * W)

        sy = i
        sx = j
        if graph[sy][sx] == "#":
            continue
        #sy と　sxをfor文で回す
        costs[sy][sx] = 0
        q = [[sy, sx]]
        #移動方向
        di = [1, -1, 0, 0]
        dj = [0, 0, -1, 1]

        while len(q) != 0:
            y, x = q.pop(0)

            for k in range(4):
                t = y + di[k]
                u = x + dj[k]
                if t < 0 or u < 0 or t >= H or u >= W:
                    continue
                if graph[t][u] == '#':
                    continue
                if costs[t][u] != -1:
                    continue
                costs[t][u] = costs[y][x] + 1
                q.append([t, u])
        ans = max(ans,max(max(costs)))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))

#迷路の作成
graph = []
for i in range(H):
    row = input()
    graph.append(list(row))
#迷路と同じ形の何もない空間を作成
ans = 0
cost_max = 0
for i in range(H):
    for j in range(W):

        costs = []
        for i in range(H):
            costs.append([-1] * W)

        sy = i
        sx = j
        if graph[sy][sx] == "#":
            continue
        #sy と　sxをfor文で回す
        costs[sy][sx] = 0
        q = [[sy, sx]]
        #移動方向
        di = [1, -1, 0, 0]
        dj = [0, 0, -1, 1]

        while len(q) != 0:
            y, x = q.pop(0)

            for k in range(4):
                t = y + di[k]
                u = x + dj[k]
                if t < 0 or u < 0 or t >= H or u >= W:
                    continue
                if graph[t][u] == '#':
                    continue
                if costs[t][u] != -1:
                    continue
                costs[t][u] = costs[y][x] + 1
                q.append([t, u])
        print(costs)
        ans = max(ans,max(max(costs)))
print(ans)

Submission status: Wrong Answer

Solution code:
H, W = list(map(int, input().split()))

#迷路の作成
graph = []
for i in range(H):
    row = input()
    graph.append(list(row))
#迷路と同じ形の何もない空間を作成
ans = 0
cost_max = 0
for i in range(H):
    for j in range(W):

        costs = []
        for i in range(H):
            costs.append([-1] * W)

        sy = i
        sx = j
        #sy と　sxをfor文で回す
        costs[sy][sx] = 0
        q = [[sy, sx]]
        #移動方向
        di = [1, -1, 0, 0]
        dj = [0, 0, -1, 1]

        while len(q) != 0:
            y, x = q.pop(0)

            for k in range(4):
                t = y + di[k]
                u = x + dj[k]
                if t < 0 or u < 0 or t >= H or u >= W:
                    continue
                if graph[t][u] == '#':
                    continue
                if costs[t][u] != -1:
                    continue
                costs[t][u] = costs[y][x] + 1
                q.append([t, u])
        ans = max(ans,max(max(costs)))
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import product
import queue
 
H, W = map(int, input().split())
road_grid = list()
for _ in range(H):
  road_grid.append(input())

# for each start point, find the furthest travellable point
# then return largest path length of all these
# ps y is down, x is right
furthest_so_far = -1
for y_init, x_init in product(range(H), range(W)):
    if road_grid[y_init][x_init] == "#": continue
    boundary = queue.Queue()
    boundary.put((y_init, x_init))
    steps_taken = [[-1]*W for _ in range(H)]
    steps_taken[y_init][x_init] = 0
    while not boundary.empty():
        y, x = boundary.get()
        neighbours = [(y+1, x), (y, x+1), (y-1, x), (y, x-1)]
        for (new_y, new_x) in neighbours:
            if (0 <= new_y < H and 0 <= new_x < W
                and steps_taken[new_y][new_x] == -1 and road_grid[new_y][new_x] == "."):
                steps_taken[new_y][new_x] = steps_taken[y][x] + 1
                boundary.put((new_y, new_x))
    furthest_so_far = max(furthest_so_far, max(max(steps_taken)))
print(furthest_so_far)

Submission status: Wrong Answer

Solution code:
from itertools import product
import queue
 
H, W = map(int, input().split())
road_grid = list()
for _ in range(H):
  road_grid.append(input())

print(road_grid)

# for each start point, find the furthest travellable point
# then return largest path length of all these
# ps y is down, x is right
furthest_so_far = -1
for y_init, x_init in product(range(H), range(W)):
    if road_grid[y_init][x_init] == "#": continue
    boundary = queue.Queue()
    boundary.put((y_init, x_init))
    steps_taken = [[-1]*W for _ in range(H)]
    steps_taken[y_init][x_init] = 0
    while not boundary.empty():
        y, x = boundary.get()
        neighbours = [(y+1, x), (y, x+1), (y-1, x), (y, x-1)]
        for (new_y, new_x) in neighbours:
            if (0 <= new_y < H and 0 <= new_x < W
                and steps_taken[new_y][new_x] == -1 and road_grid[new_y][new_x] == "."):
                steps_taken[new_y][new_x] = steps_taken[y][x] + 1
                boundary.put((new_y, new_x))
    furthest_so_far = max(furthest_so_far, max(max(steps_taken)))
print(furthest_so_far)

Submission status: Wrong Answer

Solution code:
import queue
height,width=map(int,input().split())

graph=[]
for _ in range(height):
    s_row = input()
    row = []
    for s in s_row:
        row.append(s)
    graph.append(row)

diff=[[0,-1],[0,1],[-1,0],[1,0]]
max_step=[]
for h in range(height):
    for w in range(width):
        if graph[h][w]=='#':
            continue

        route = []
        for _ in range(height):
            route.append([-1]*width)

        # start x,y,経路数
        q = queue.Queue()
        q.put([w,h,0])

        while not q.empty():
            now = q.get()
            now_w = now[0]
            now_h = now[1]
            now_n = now[2]

            route[now_h][now_w] = now_n

            for dx,dy in diff:
                next_w = now_w+dx
                next_h = now_h+dy
                if   width<=next_w \
                  or height<=next_h \
                  or next_w < 0 \
                  or next_h < 0 \
                  or graph[next_h][next_w]=='#' \
                  or route[next_h][next_w]>-1:
                    continue
                q.put([next_w, next_h, now_n+1])
        
        max_step.append(max(max(step for step in route)))
        
print(max(max_step))

Submission status: Wrong Answer

Solution code:
import queue
height,width=map(int,input().split())

graph=[]
for _ in range(height):
    s_row = input()
    row = []
    for s in s_row:
        row.append(s)
    graph.append(row)

diff=[[0,-1],[0,1],[-1,0],[1,0]]
max_step=[]
for h in range(height):
    for w in range(width):
        if graph[h][w]=='#':
            continue

        route = []
        for _ in range(height):
            route.append([-1]*width)

        # start x,y,経路数
        q = queue.Queue()
        q.put([w,h,0])

        while not q.empty():
            now = q.get()
            now_w = now[0]
            now_h = now[1]
            now_n = now[2]

            route[now_h][now_w] = now_n

            for dx,dy in diff:
                next_w = now_w+dx
                next_h = now_h+dy
                if   width<=next_w \
                  or height<=next_h \
                  or next_w < 0 \
                  or next_h < 0 \
                  or graph[next_h][next_w]=='#' \
                  or route[next_h][next_w]>-1:
                    continue
                q.put([next_w, next_h, now_n+1])
        
        max_step.append(max(max(step for step in route)))
        
print(max(max_step))

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import copy

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

dxs = [0, 0, 1, -1]
dys = [1, -1, 0, 0]
cand = []
field_org = [[-1] * W for _ in range(H)]
for j in range(H):
    for i in range(W):
        if S[j][i] == '#':
            continue
        q = deque()
        q.append((i, j, 0))
        field = copy(field_org)
        field[j][i] = 0
        while q:
            p = q.popleft()
            for dx, dy in zip(dxs, dys):
                x, y = p[0] + dx, p[1] + dy
                if not (0 <= x < W) or not (0 <= y < H) or S[y][x] == '#' or field[y][x] != -1:
                    continue
                field[y][x] = p[2] + 1
                q.append((x, y, p[2] + 1))
        cand.append(max(max(row) for row in field))
print(max(cand))

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

dxs = [0, 0, 1, -1]
dys = [1, -1, 0, 0]
cand = [0]
for j in range(H):
    for i in range(W):
        if S[j][i] == '#':
            continue
        q = deque()
        q.append((i, j, 0))
        field = [[0] * W for _ in range(H)]
        while q:
            p = q.popleft()
            for dx, dy in zip(dxs, dys):
                x, y = p[0] + dx, p[1] + dy
                if not (0 <= x < W) or not (0 <= y < H) or S[y][x] == '#' or field[y][x] != 0:
                    continue
                field[y][x] = p[2] + 1
                cand.append(p[2] + 1)
                q.append((x, y, p[2] + 1))
print(max(cand))

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]

dxs = [0, 0, 1, -1]
dys = [1, -1, 0, 0]
cand = []
for j in range(H):
    for i in range(W):
        if S[j][i] == '#':
            continue
        q = deque()
        q.append((i, j, 0))
        field = [[-1] * W for _ in range(H)]
        tmp_max = -1
        while q:
            p = q.popleft()
            for dx, dy in zip(dxs, dys):
                x, y = p[0] + dx, p[1] + dy
                if not (0 <= x < W) or not (0 <= y < H) or S[y][x] == '#' or field[y][x] != -1:
                    continue
                field[y][x] = p[2] + 1
                cand.append(p[2] + 1)
                q.append((x, y, p[2] + 1))
print(max(cand))

Submission status: Wrong Answer

Solution code:
import collections as cl
def check(y,x):
    if 0 <= x and x < w and 0 <= y and y < h :
        return True
    else:
        return False

def func(y,x):
    conf = set()
    q = cl.deque()
    q.append([y,x,0])
    ans = [1]
    while q:
        y,x,count= q.popleft()
        if check(y,x) == False or s[y][x] == '#' or (y,x) in conf:
            continue
        else:
            q.append([y-1,x,count+1])
            q.append([y+1,x,count+1])
            q.append([y,x-1,count+1])
            q.append([y,x+1,count+1])
            ans.append(count)
            conf.add((y,x))
    return max(ans)

h,w = map(int,input().split())
s = []
step = []
for i in range(h):
    s.append(list(map(str,input())))

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            a=func(j,i)
            step.append(a)
print(max(step))

Submission status: Wrong Answer

Solution code:
import collections as cl
def check(y,x):
    if 0 <= x and x < w and 0 <= y and y < h and s[y][x] == '.' :
        return True
    else:
        return False

def func(y,x):
    conf = [[0 for i in range(w)]for j in range(h)]
    q = cl.deque()
    count = 0
    q.append([y,x,count])
    ans = [1]
    while q:
        y,x,count= q.popleft()
        if check(y-1,x) and conf[y-1][x] == 0:
            q.append([y-1,x,count+1])
            conf[y-1][x] = 1
            ans.append(count+1)
        if check(y+1,x) and conf[y+1][x] == 0:
            q.append([y+1,x,count+1])
            conf[y+1][x] = 1
            ans.append(count+1)
        if check(y,x-1) and conf[y][x-1] == 0:
            q.append([y,x-1,count+1])
            conf[y][x-1] = 1
            ans.append(count+1)
        if check(y,x+1) and conf[y][x+1] == 0:
            q.append([y,x+1,count+1])
            conf[y][x+1] = 1
            ans.append(count+1)
    return max(ans)

h,w = map(int,input().split())
s = []
step = []
for i in range(h):
    s.append(list(map(str,input())))

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            a=func(j,i)
            step.append(a)
print(max(step))

Submission status: Wrong Answer

Solution code:
import collections as cl
def check(y,x):
    if (0 <= x and x < len(s[0])) and (0 <= y and y < len(s)) and s[y][x] == '.' :
        return True
    else:
        return False

def func(y,x):
    conf = [[0 for i in range(len(s[0]))]for j in range(len(s))]
    q = cl.deque()
    count = 0
    q.append([y,x,count])
    ans = [1]
    while q:
        y,x,count= q.popleft()
        if check(y-1,x) and conf[y-1][x] == 0:
            q.append([y-1,x,count+1])
            conf[y-1][x] = 1
            ans.append(count+1)
        if check(y+1,x) and conf[y+1][x] == 0:
            q.append([y+1,x,count+1])
            conf[y+1][x] = 1
            ans.append(count+1)
        if check(y,x-1) and conf[y][x-1] == 0:
            q.append([y,x-1,count+1])
            conf[y][x-1] = 1
            ans.append(count+1)
        if check(y,x+1) and conf[y][x+1] == 0:
            q.append([y,x+1,count+1])
            conf[y][x+1] = 1
            ans.append(count+1)
    return max(ans)

h,w = map(int,input().split())
s = []
step = []
for i in range(h):
    s.append(list(map(str,input())))

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            a=func(j,i)
            step.append(a)
print(max(step))

Submission status: Wrong Answer

Solution code:
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
            # スタートをキューに、距離を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 h in range(H):
                for w in range(W):
                    if d[h][w] != inf:
                        ans = max(ans, d[h][w])

print(ans)

Submission status: Wrong Answer

Solution code:
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 = -1

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
            # スタートをキューに、距離を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 h in range(H):
                for w in range(W):
                    if d[h][w] != inf:
                        ans = max(ans, d[h][w])

print(ans)

Submission status: Wrong Answer

Solution code:
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 = -1

for h in range(H):
    for w in range(W):
        q = queue.Queue()
        d = [[inf for _ in range(W)] for _ in range(H)]
        # 距離を保存
        if S[h][w] == '#':
            break
        q.put([h, w])
        d[h][w] = 0
        # スタートをキューに、距離を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 h in range(H):
            for w in range(W):
                if d[h][w] != inf:
                    ans = max(ans, d[h][w])

print(ans)

Submission status: Wrong Answer

Solution code:
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 = -1

for h in range(H):
    for w in range(W):
        q = queue.Queue()
        d = [[inf for _ in range(W)] for _ in range(H)]
        # 距離を保存
        q.put([h, w])
        d[h][w] = 0
        # スタートをキューに、距離を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 h in range(H):
            for w in range(W):
                if d[h][w] != inf:
                    ans = max(ans, d[h][w])

print(ans)

Submission status: Wrong Answer

Solution code:
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 = -1

for h in range(H):
    for w in range(W):
        q = queue.Queue()
        d = [[inf for _ in range(W)] for _ in range(H)]
        # 距離を保存
        q.put([h, w])
        d[h][w] = 0
        # スタートをキューに、距離を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
                    if d[ny][nx] != inf:
                        ans = max(ans, d[ny][nx])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from queue import PriorityQueue
H, W = map(int, input().split())
M = []
for h in range(H):
    M.append(input()+'#')
M.append('#'*(W+1))

INF = 10000
def default():
    return INF
neighbor = defaultdict(list)
for h in range(H):
    for w in range(W):
        if M[h][w] == '#':
            continue
        u = (h, w)
        if M[h+1][w] == '.':
            v = (h+1, w) 
            neighbor[u].append(v)
            neighbor[v].append(u)
        if M[h][w+1] == '.':
            v = (h, w+1)
            neighbor[u].append(v)
            neighbor[v].append(u)
D = defaultdict(default)
q = PriorityQueue(H*W)
s = u
q.put((0, s))
while not q.empty():
    d, u = q.get()
    for v in neighbor[u]:
        if D[v] > d+1:
            D[v] = d+1
            q.put((d+1, v))
md = max(D.values())
mu = [u for u, d in D.items() if d == md]
answer = 0

for u in mu:
    D = defaultdict(default)
    q = PriorityQueue(H*W)
    q.put((0, u))
    while not q.empty():
        d, u = q.get()
        for v in neighbor[u]:
            if D[v] > d+1:
                D[v] = d+1
                q.put((d+1, v))
                answer = max(d+1, answer)
print(answer)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from queue import PriorityQueue
H, W = map(int, input().split())
M = []
for h in range(H):
    M.append(input()+'#')
M.append('#'*(W+1))

INF = 10000
def default():
    return INF
neighbor = defaultdict(list)
for h in range(H):
    for w in range(W):
        if M[h][w] == '#':
            continue
        u = (h, w)
        if M[h+1][w] == '.':
            v = (h+1, w) 
            neighbor[u].append(v)
            neighbor[v].append(u)
        if M[h][w+1] == '.':
            v = (h, w+1)
            neighbor[u].append(v)
            neighbor[v].append(u)
D = defaultdict(default)
q = PriorityQueue(H*W)
s = u
q.put((0, s))
while not q.empty():
    d, u = q.get()
    for v in neighbor[u]:
        if D[v] > d+1:
            D[v] = d+1
            q.put((d+1, v))
md = 0
for v, d in D.items():
    if d > md:
        md = d
        mv = v
D = defaultdict(default)
q = PriorityQueue(H*W)
q.put((0, mv))
while not q.empty():
    d, u = q.get()
    for v in neighbor[u]:
        if D[v] > d+1:
            D[v] = d+1
            q.put((d+1, v))
for v, d in D.items():
    if d > md:
        md = d
        mv = v
print(md)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
H, W = map(int, input().split())
M = []
for h in range(H):
    M.append(input()+'#')
M.append('#'*(W+1))

INF = 10000
def default():
    return INF
N = []
d = defaultdict(default)
for h in range(H):
    for w in range(W):
        hw = (h, w)
        d[(hw, hw)] = 0
        if M[h][w] == '#':
            continue
        N.append(hw)
        if M[h+1][w] == '.':
            n = (h+1, w)
            d[(hw, n)] = d[(n,hw)] = 1
        if M[h][w+1] == '.':
            n = (h, w+1)
            d[(hw, n)] = d[(n,hw)] = 1

for i in N:
    for j in N:
        for k in N:
            alt = d[(i,k)] + d[(k,j)]
            if d[(i,j)] > alt:
                d[(i,j)] = alt
                d[(j,i)] = alt
for h in range(H):
    for w in range(W):
        #print(d[(0, 0), (h, w)])
        pass
    #print()
#print(d[(H-1,W-1), (H-2,W-1)])
#print(d[(H-2,W-1), (H-1,W-1)])
print(max(x for x in d.values() if x != INF))

Submission status: Wrong Answer

Solution code:
import collections as cl

def func (h,w,i,j):
    count=0
    ans=[1]
    q=cl.deque()
    q.append([i,j,count])
    ch=[[0 for i in range(w)]for j in range(h)]
    while q:
        i,j,count=q.popleft()
        if i-1>=0 and s[i-1][j]=='.' and ch[i-1][j] == 0:
            q.append([i-1,j,count+1])
            ch[i-1][j] = 1
            ans.append(count+1)
        if i+1 < h and s[i+1][j] == '.' and ch[i+1][j] == 0:
            q.append([i+1,j,count+1])
            ch[i+1][j] = 1
            ans.append(count+1)
        if j-1 >= 0 and s[i][j-1] == '.' and ch[i][j-1] == 0:
            q.append([i,j-1,count+1])
            ch[i][j-1] = 1
            ans.append(count+1)
        if j+1 < w and s[i][j+1] == '.' and ch[i][j+1] == 0:
            q.append([i,j+1,count+1])
            ch[i][j+1] = 1
            ans.append(count+1)

    return max(ans)

h,w=map(int,input().split())
s=[]
ans=[]
for i in range(h):
    s.append(list(map(str,input())))

for i in range(h):
    for j in range(w):
        if s[i][j]=='.':
            ans.append(func(h,w,i,j))
        else:
            continue
print(max(ans))