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:
H, W = map(int, input().split())
Atlas = []
for i in range(H):
  Atlas.append(input().split())

def BFS(a,b,atlas):
  times = 0
  queue = [[a, b]]
  old_queue = []
  while True:
    new_queue = []
    changed = False
    for x, y in queue:
      if x+1 < H:
        if atlas[x+1][0][y] != "#" and not [x+1, y] in old_queue:
          new_queue.append([x+1, y])
          old_queue.append([x+1, y])
          changed = True
      if x-1 >= 0:
        if atlas[x-1][0][y] != "#" and not [x-1, y] in old_queue:
          new_queue.append([x-1, y])
          old_queue.append([x-1, y])
          changed = True
      if y+1 < W:
        if atlas[x][0][y+1] != "#" and not [x, y+1] in old_queue:
          new_queue.append([x, y+1])
          old_queue.append([x, y+1])
          changed = True
      if y-1 >= 0:
        if atlas[x][0][y-1] != "#" and not [x, y-1] in old_queue:
          new_queue.append([x, y-1])
          old_queue.append([x, y-1])
          changed = True
    queue = new_queue
    if not changed:
      break
    times += 1
  return times

TIMES = 0
for j in range(H):
  for k in range(W):
    if Atlas[j][0][k] == "#":
      break
    new_TIMES = BFS(j, k, Atlas)
    if new_TIMES > TIMES:
      TIMES = new_TIMES
      
print(TIMES)

Submission status: Wrong Answer

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

def BFS(a,b,atlas):
  times = 0
  queue = [[a, b]]
  old_queue = []
  while True:
    new_queue = []
    changed = False
    for x, y in queue:
      if x+1 < H:
        if atlas[x+1][0][y] != "#" and not [x+1, y] in old_queue:
          new_queue.append([x+1, y])
          old_queue.append([x+1, y])
          changed = True
      if x-1 >= 0:
        if atlas[x-1][0][y] != "#" and not [x-1, y] in old_queue:
          new_queue.append([x-1, y])
          old_queue.append([x-1, y])
          changed = True
      if y+1 < W:
        print(x,y)
        if atlas[x][0][y+1] != "#" and not [x, y+1] in old_queue:
          new_queue.append([x, y+1])
          old_queue.append([x, y+1])
          changed = True
      if y-1 >= 0:
        if atlas[x][0][y-1] != "#" and not [x, y-1] in old_queue:
          new_queue.append([x, y-1])
          old_queue.append([x, y-1])
          changed = True
    queue = new_queue
    if not changed:
      break
    times += 1
  return times

TIMES = 0
for j in range(H):
  for k in range(W):
    if Atlas[j][0][k] == "#":
      break
    new_TIMES = BFS(j, k, Atlas)
    if new_TIMES > TIMES:
      TIMES = new_TIMES
      
print(TIMES)

Submission status: Wrong Answer

Solution code:
import copy
import queue

input_int_list = list(map(int, input().split()))

H = input_int_list[0]
W = input_int_list[1]

MAX_DIST = 100000

# master_maze_list =[ [0] * H ] * W
master_maze_list = [[-1 for i in range(H+2)] for j in range(W+2)]
for vertical_i in range(H):
	input_str_H = input()
	for holizontal_i in range(W):
		if input_str_H[holizontal_i] == '#':
			master_maze_list[holizontal_i+1][vertical_i+1] = -1
		elif input_str_H[holizontal_i] == '.':
			master_maze_list[holizontal_i+1][vertical_i+1] = MAX_DIST


# print(master_maze_list)


distance_max = 0

for vertical_i in range(H):
	for holizontal_i in range(W):
		search_target_maze = copy.deepcopy(master_maze_list)
		root_h = holizontal_i+1
		root_v = vertical_i+1
		next_place_queue = queue.Queue()
		next_place_queue.put(root_h)
		next_place_queue.put(root_v)
		search_target_maze[root_h][root_v] = 0
		if search_target_maze[root_h][root_v] == -1:
			continue

		while next_place_queue.empty() != True:
			now_h = next_place_queue.get()
			now_v = next_place_queue.get()
			now_place_val = search_target_maze[now_h][now_v]

			if now_place_val == -1:
				pass
			else:
				if search_target_maze[now_h+1][now_v] > now_place_val+1:
					search_target_maze[now_h+1][now_v] = now_place_val+1
					next_place_queue.put(now_h+1)
					next_place_queue.put(now_v)

				if search_target_maze[now_h][now_v+1] > now_place_val+1:
					search_target_maze[now_h][now_v+1] = now_place_val+1
					next_place_queue.put(now_h)
					next_place_queue.put(now_v+1)

				if search_target_maze[now_h-1][now_v] > now_place_val+1:
					search_target_maze[now_h-1][now_v] = now_place_val+1
					next_place_queue.put(now_h-1)
					next_place_queue.put(now_v)

				if search_target_maze[now_h][now_v-1] > now_place_val+1:
					search_target_maze[now_h][now_v-1] = now_place_val+1
					next_place_queue.put(now_h)
					next_place_queue.put(now_v-1)
		distance_max = max(distance_max, now_place_val)
#		print(distance_max)

print(distance_max)

Submission status: Wrong Answer

Solution code:
import copy
import queue

input_int_list = list(map(int, input().split()))

H = input_int_list[0]
W = input_int_list[1]

MAX_DIST = 100000

# master_maze_list =[ [0] * H ] * W
master_maze_list = [[-1 for i in range(H+2)] for j in range(W+2)]
for vertical_i in range(H):
	input_str_H = input()
	for holizontal_i in range(W):
		if input_str_H[holizontal_i] == '#':
			master_maze_list[holizontal_i+1][vertical_i+1] = -1
		elif input_str_H[holizontal_i] == '.':
			master_maze_list[holizontal_i+1][vertical_i+1] = MAX_DIST


# print(master_maze_list)


distance_max = 0

for vertical_i in range(H):
	for holizontal_i in range(W):
		search_target_maze = copy.deepcopy(master_maze_list)
		root_h = holizontal_i+1
		root_v = vertical_i+1
		distance = -1
		next_place_queue = queue.Queue()
		next_place_queue.put(root_h)
		next_place_queue.put(root_v)
		search_target_maze[root_h][root_v] = 0
		while next_place_queue.empty() != True:
			now_h = next_place_queue.get()
			now_v = next_place_queue.get()
			now_place_val = search_target_maze[now_h][now_v]

			if now_place_val == -1:
				pass
			else:
				if search_target_maze[now_h+1][now_v] > now_place_val+1:
					search_target_maze[now_h+1][now_v] = now_place_val+1
					next_place_queue.put(now_h+1)
					next_place_queue.put(now_v)

				if search_target_maze[now_h][now_v+1] > now_place_val+1:
					search_target_maze[now_h][now_v+1] = now_place_val+1
					next_place_queue.put(now_h)
					next_place_queue.put(now_v+1)

				if search_target_maze[now_h-1][now_v] > now_place_val+1:
					search_target_maze[now_h-1][now_v] = now_place_val+1
					next_place_queue.put(now_h-1)
					next_place_queue.put(now_v)

				if search_target_maze[now_h][now_v-1] > now_place_val+1:
					search_target_maze[now_h][now_v-1] = now_place_val+1
					next_place_queue.put(now_h)
					next_place_queue.put(now_v-1)
		distance_max = max(distance_max, now_place_val)
#		print(distance_max)

print(distance_max)

Submission status: Wrong Answer

Solution code:
import copy
import queue

input_int_list = list(map(int, input().split()))

H = input_int_list[0]
W = input_int_list[1]

MAX_DIST = 100000

# master_maze_list =[ [0] * H ] * W
master_maze_list = [[-1 for i in range(H+2)] for j in range(W+2)]
for vertical_i in range(H):
	input_str_H = input()
	for holizontal_i in range(W):
		if input_str_H[holizontal_i] == '#':
			master_maze_list[holizontal_i+1][vertical_i+1] = -1
		elif input_str_H[holizontal_i] == '.':
			master_maze_list[holizontal_i+1][vertical_i+1] = MAX_DIST


print(master_maze_list)


distance_max = 0

for vertical_i in range(H):
	for holizontal_i in range(W):
		search_target_maze = copy.deepcopy(master_maze_list)
		root_h = holizontal_i+1
		root_v = vertical_i+1
		distance = -1
		next_place_queue = queue.Queue()
		next_place_queue.put(root_h)
		next_place_queue.put(root_v)
		search_target_maze[root_h][root_v] = 0
		while next_place_queue.empty() != True:
			now_h = next_place_queue.get()
			now_v = next_place_queue.get()
			now_place_val = search_target_maze[now_h][now_v]

			if now_place_val == -1:
				pass
			else:
				if search_target_maze[now_h+1][now_v] > now_place_val+1:
					search_target_maze[now_h+1][now_v] = now_place_val+1
					next_place_queue.put(now_h+1)
					next_place_queue.put(now_v)

				if search_target_maze[now_h][now_v+1] > now_place_val+1:
					search_target_maze[now_h][now_v+1] = now_place_val+1
					next_place_queue.put(now_h)
					next_place_queue.put(now_v+1)

				if search_target_maze[now_h-1][now_v] > now_place_val+1:
					search_target_maze[now_h-1][now_v] = now_place_val+1
					next_place_queue.put(now_h-1)
					next_place_queue.put(now_v)

				if search_target_maze[now_h][now_v-1] > now_place_val+1:
					search_target_maze[now_h][now_v-1] = now_place_val+1
					next_place_queue.put(now_h)
					next_place_queue.put(now_v-1)
		distance_max = max(distance_max, now_place_val)
#		print(distance_max)

print(distance_max)

Submission status: Wrong Answer

Solution code:
from queue import Queue

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

ans = 0
for i in range(h):
    for j in range(w):
        start = s[i][j]
        if start == "#":
            continue
        else:
            visited = [[False for k in range(w)] for l in range(h)]
            queue = Queue()
            queue.put((i, j, 0))

            while 0 < queue.qsize():
                now = queue.get()
                ans = max(ans, now[2])

                if (now[0] <= (h - 2)) and (s[now[0] + 1][now[1]] == ".") and not visited[now[0] + 1][now[1]]:
                    queue.put(((now[0] + 1), now[1], (now[2] + 1)))
                    visited[now[0] + 1][now[1]] = True
                if (now[1] <= (w - 2)) and (s[now[0]][now[1] + 1] == ".") and not visited[now[0]][now[1] + 1]:
                    queue.put((now[0], (now[1] + 1), (now[2] + 1)))
                    visited[now[0]][now[1] + 1] = True
                if (1 <= now[0]) and (s[now[0] - 1][now[1]] == ".") and not visited[now[0] - 1][now[1]]:
                    queue.put(((now[0] - 1), now[1], (now[2] + 1)))
                    visited[now[0] - 1][now[1]] = True
                if (1 <= now[1]) and (s[now[0]][now[1] - 1] == ".") and not visited[now[0]][now[1] - 1]:
                    queue.put((now[0], (now[1] - 1), (now[2] + 1)))
                    visited[now[0]][now[1] - 1] = True

print(ans)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
maze=[]
maze.append(['#']*(W+2))
for i in range(H):
    tmp=['#']+list(input())+['#']
    if '.' in tmp:
        a,b=i,tmp.index('.')
    maze.append(tmp)
maze.append(['#']*(W+2))
infty=10**5

def far(a,b):
    cost[a][b]=0
    point[0].append((a,b))
    stocks=1
    while stocks>0:
        stocks-=1
        for i in range(infty):
            if point[i]==[]:
                continue
            else:
                a,b=point[i].pop(0)
                break
        if maze[a][b+1]=='.' and cost[a][b+1]==infty:
            cost[a][b+1]=i+1
            point[i+1].append((a,b+1))
            stocks+=1
        if maze[a][b-1]=='.' and cost[a][b-1]==infty:
            cost[a][b-1]=i+1
            point[i+1].append((a,b-1))
            stocks+=1
        if maze[a-1][b]=='.' and cost[a-1][b]==infty:
            cost[a-1][b]=i+1
            point[i+1].append((a-1,b))
            stocks+=1
        if maze[a+1][b]=='.' and cost[a+1][b]==infty:
            cost[a+1][b]=i+1
            point[i+1].append((a+1,b))
            stocks+=1
    return (a,b),i


cost=[[infty]*(W+2) for i in range(H+2)]
point=[[] for i in range(infty)]

a,b=far(a,b)[0]

cost=[[infty]*(W+2) for i in range(H+2)]
point=[[] for i in range(infty)]

print(far(a,b)[1])

Submission status: Wrong Answer

Solution code:
# adj = {
#     1: [2, 3, 4, 8, 9],
#     2: [1, 3, 5],
#     3: [1, 2, 6],
#     4: [1, 6],
#     5: [2, 6, 7],
#     6: [5, 3, 4],
#     7: [5],
#     8: [1],
#     9: [1]
# }

from collections import deque

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

adj = {(i, j): [] for i in range(H) for j in range(W)}
for i in range(H):
    for j in range(W):
        for m in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:
            if 0 <= m[0] <= H - 1 and 0 <= m[1] <= W - 1 and d[i][j] != '#':
                adj[i, j].append(m)


def bfs(g, s):
    if d[s[0]][s[1]] == "#":
        return 0
    visited = {}
    distance = {}
    q = deque()
    q.append(s)
    visited[s] = True
    distance[s] = 0
    while q:
        u = q.popleft()
        for i in g[u]:
            if i not in visited:
                q.append(i)
                visited[i] = True
                distance[i] = distance[u] + 1

    return max(distance.values())


ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, bfs(adj, (i, j)))

print(ans)

Submission status: Wrong Answer

Solution code:
# adj = {
#     1: [2, 3, 4, 8, 9],
#     2: [1, 3, 5],
#     3: [1, 2, 6],
#     4: [1, 6],
#     5: [2, 6, 7],
#     6: [5, 3, 4],
#     7: [5],
#     8: [1],
#     9: [1]
# }

from collections import deque

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

adj = {(i, j): [] for i in range(H) for j in range(W)}
for i in range(H):
    for j in range(W):
        for m in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:
            if 0 <= m[0] <= H - 1 and 0 <= m[1] <= W - 1 and d[i][j] != '#':
                adj[i, j].append(m)


def bfs(g, s):
    visited = {}
    distance = {}
    q = deque()
    q.append(s)
    visited[s] = True
    distance[s] = 0
    while q:
        u = q.popleft()
        for i in g[u]:
            if i not in visited:
                q.append(i)
                visited[i] = True
                distance[i] = distance[u] + 1

    return max(distance.values())


ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, bfs(adj, (i, j)))

print(ans)

Submission status: Wrong Answer

Solution code:
import copy
import numpy as np
from collections import deque
m,n = list(map(int, input().split()))
o2 = []
dist_mat = []
for i in range(m):
    o1 = list(str(input()))
    o2.append(o1)
    dist_mat.append(np.zeros(len(o1)))

in_mat = np.array(o2)
#dist_mat = np.zeros_like(in_mat)
dist_mat = []
for i in in_mat:
    dist_mat.append(np.zeros(len(i)))
dist_mat = np.array(dist_mat)
rows,cols = dist_mat.shape

qu = deque()

for r in range(rows):
    for c in range(cols):
        if in_mat[r][c] == ".":
            qu.appendleft((r,c))

dist_mat_base = copy.deepcopy(dist_mat)

ans = []
lenqu = len(qu)
for i in range(lenqu):
    print(i)
    strt = qu.pop()
    qu2 = deque()
    qu2.appendleft(strt)
    dist_mat = copy.deepcopy(dist_mat_base)

    while qu2:
        curr_r, curr_c = qu2.pop()
        dist = dist_mat[curr_r][curr_c]
        adj_cells = [(0,1), (1,0), (0,-1), (-1,0)]
        for r_adj, c_adj in adj_cells:
            adj_row = curr_r + r_adj
            adj_col = curr_c + c_adj
            if 0 <= adj_row < rows and 0 <= adj_col < cols:
                if dist_mat[adj_row][adj_col] == 0 and in_mat[adj_row][adj_col] == "." and not (adj_row, adj_col) == strt:
                    dist_mat[adj_row][adj_col] = dist + 1
                    qu2.appendleft((adj_row, adj_col))

    dist_vec = np.concatenate(dist_mat)
    ans.append(round(max(dist_vec)))

print(round(max(ans)))

Submission status: Wrong Answer

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

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

def bfs(S, H, W, start):
    INF = -1
    stack=deque([start])
    distance = [[INF]*W for _ in range(H)]
    distance[start[1]][start[0]] = 0
    while stack:
        x, y = stack.popleft()
        for i, j in [(1,0),(0,1),(-1,0),(0,-1)]:
            next_x = x + i
            next_y = y + j
            if not(0 <= next_x < W and 0 <= next_y < H):
                continue
            if S[next_y][next_x] != '#' and distance[next_y][next_x] == -1:
                stack.append([next_x, next_y])
                distance[next_y][next_x] = distance[y][x] + 1
    return max(max(distance))

ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != "#":
            ans = max(ans, bfs(S, H, W, (j, i)))
if ans == -1:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

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

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

def bfs(S, H, W, start):
    INF = -1
    stack=deque([start])
    distance = [[INF]*W for _ in range(H)]
    distance[start[1]][start[0]] = 0
    while stack:
        x, y = stack.popleft()
        for i, j in [(1,0),(0,1),(-1,0),(0,-1)]:
            next_x = x + i
            next_y = y + j
            if not(0 <= next_x < W and 0 <= next_y < H):
                continue
            if S[next_y][next_x] != '#' and distance[next_y][next_x] == -1:
                stack.append([next_x, next_y])
                distance[next_y][next_x] = distance[y][x] + 1
    return max(max(distance))

ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] != "#":
            ans = max(ans, bfs(S, H, W, (j, i)))
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

def search():
    v = 0
    now = [[i1,j1,0]]
    go = True
    while go:
        for i in now[:]:
            #上
            if i[0] == 0:
                pass
            else:
                if meiro[i[0]-1][i[1]] == ".":
                    now.append([i[0]-1,i[1],i[2]+1])
                    meiro[i[0]-1][i[1]] = i[2]+1
            #下
            if i[0] == h-1:
                pass
            else:
                if meiro[i[0]+1][i[1]] == ".":
                    now.append([i[0]+1,i[1],i[2]+1])
                    meiro[i[0]+1][i[1]] = i[2]+1
            #左
            if i[1] == 0:
                pass
            else:
                if meiro[i[0]][i[1]-1] == ".":
                    now.append([i[0],i[1]-1,i[2]+1])
                    meiro[i[0]][i[1]-1] = i[2]+1
            #右
            if i[1] == w-1:
                pass
            else:
                if meiro[i[0]][i[1]+1] == ".":
                    now.append([i[0],i[1]+1,i[2]+1])
                    meiro[i[0]][i[1]+1] = i[2]+1
            v = max(v,i[2])
            now.remove(i)
            if now == []:
                return v


saitankeiro = 0
maze,h,w = maze_maker()
for i1 in range(h):
    for j1 in range(w):
        if maze[i1][j1] != "#":
            meiro = copy.deepcopy(maze)
            saitankeiro = max(saitankeiro,search())
print(saitankeiro)

Submission status: Wrong Answer

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

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

def bfs(S, H, W, start):
    INF = -1
    stack=deque([start])
    distance = [[INF]*W for _ in range(H)]
    distance[start[1]][start[0]] = 0
    while stack:
        x, y = stack.popleft()
        for i, j in [(1,0),(0,1),(-1,0),(0,-1)]:
            next_x = x + i
            next_y = y + j
            if not(0 <= next_x < W and 0 <= next_y < H):
                continue
            if S[next_y][next_x] != '#' and distance[next_y][next_x] == -1:
                stack.append([next_x, next_y])
                distance[next_y][next_x] = distance[y][x] + 1
    return max(max(distance))

ans = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            ans = max(ans, bfs(S, H, W, (j, i)))
print(ans)

Submission status: Wrong Answer

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

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

def bfs(S, H, W, start):
    INF = -1
    stack=deque([start])
    distance = [[INF]*W for _ in range(H)]
    distance[start[1]][start[0]] = 0
    while stack:
        x, y = stack.popleft()
        for i, j in [(1,0),(0,1),(-1,0),(0,-1)]:
            next_x = x + i
            next_y = y + j
            if not(0 <= next_x < W and 0 <= next_y < H):
                continue
            if S[next_y][next_x] != '#' and distance[next_y][next_x] == -1:
                stack.append([next_x, next_y])
                distance[next_y][next_x] = distance[y][x] + 1
    return max(max(distance))

ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, bfs(S, H, W, (j, i)))
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

def search():
    now = [[i1,j1,0]]
    while True:
        for i in now[:]:
            #上
            if i[0] == 0:
                pass
            else:
                if meiro[i[0]-1][i[1]] == "G":
                    return i[2]+1
                if meiro[i[0]-1][i[1]] == ".":
                    now.append([i[0]-1,i[1],i[2]+1])
                    meiro[i[0]-1][i[1]] = i[2]+1
            #下
            if i[0] == h-1:
                pass
            else:
                if meiro[i[0]+1][i[1]] == "G":
                    return i[2]+1
                if meiro[i[0]+1][i[1]] == ".":
                    now.append([i[0]+1,i[1],i[2]+1])
                    meiro[i[0]+1][i[1]] = i[2]+1
            #左
            if i[1] == 0:
                pass
            else:
                if meiro[i[0]][i[1]-1] == "G":
                    return i[2]+1
                if meiro[i[0]][i[1]-1] == ".":
                    now.append([i[0],i[1]-1,i[2]+1])
                    meiro[i[0]][i[1]-1] = i[2]+1
            #右
            if i[1] == w-1:
                pass
            else:
                if meiro[i[0]][i[1]+1] == "G":
                    return i[2]+1
                if meiro[i[0]][i[1]+1] == ".":
                    now.append([i[0],i[1]+1,i[2]+1])
                    meiro[i[0]][i[1]+1] = i[2]+1
            now.remove(i)

saitankeiro = 0
maze,h,w = maze_maker()
for i1 in range(h):
    for j1 in range(w):
        if maze[i1][j1] != "#":
            for i2 in range(h):
                for j2 in range(w):
                    if maze[i2][j2] != "#" and i1 != i2 and j1 != j2:
                        meiro = copy.deepcopy(maze)
                        meiro[i1][j1] = "S"
                        meiro[i2][j2] = "G"
                        saitankeiro = max(saitankeiro,search())
print(saitankeiro)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import itertools


def main():
  H, W = map(int, input().split())
  M = list(itertools.chain.from_iterable([[c == '.' for c in input().strip()] for _ in range(H)]))
  size = W * H
  dist = [[size] * (size) for _ in range(size)]

  for i in range(size):
    if not M[i]:
      continue

    x, y = i % W, i // W

    if x > 0 and M[i - 1]:
      dist[i][i - 1] = 1
      dist[i - 1][i] = 1

    if y > 0 and M[i - W]:
      dist[i][i - W] = 1
      dist[i - W][i] = 1

  for i in range(size):
    for j in range(size):
      for k in range(size):
        dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k])

  r = 1

  for v in itertools.chain.from_iterable(dist):
    if v < 15:
      r = max(r, v)

  print(r)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
import queue

H, W = [int(x) for x in stdin.readline().rstrip().split()]
maze = [stdin.readline().rstrip().split()[0] for _ in range(H)]

ans = 0

for h in range(H):
    for w in range(W):
        if maze[h][w] == '#':
            continue
        else:
            q = queue.Queue()
            q.put((h, w, 0))
            temp_h = h
            temp_w = w
            visited = [[0] * W for _ in range(H)]
            # visited[h][w] = 1
            while q.qsize() > 0:
                temp = q.get()
                temp_h = temp[0]
                temp_w = temp[1]
                temp_d = temp[2]
                ans = max(ans, temp_d)
                if temp_h + 1 < H and maze[temp_h + 1][temp_w] == '.' and visited[temp_h + 1][temp_w] == 0:
                    visited[temp_h + 1][temp_w] = visited[temp_h][temp_w] + 1
                    q.put((temp_h + 1, temp_w, temp_d + 1))
                if temp_h - 1 >= 0 and maze[temp_h - 1][temp_w] == '.' and visited[temp_h - 1][temp_w] == 0:
                    visited[temp_h - 1][temp_w] = visited[temp_h][temp_w] + 1
                    q.put((temp_h - 1, temp_w, temp_d + 1))
                if temp_w + 1 < W and maze[temp_h][temp_w + 1] == '.' and visited[temp_h][temp_w + 1] == 0:
                    visited[temp_h][temp_w + 1] = visited[temp_h][temp_w] + 1
                    q.put((temp_h, temp_w + 1, temp_d + 1))
                if temp_w - 1 >= 0 and maze[temp_h][temp_w - 1] == '.' and visited[temp_h][temp_w - 1] == 0:
                    visited[temp_h][temp_w - 1] = visited[temp_h][temp_w] + 1
                    q.put((temp_h, temp_w - 1, temp_d + 1))

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import deque

H, W = [int(x) for x in stdin.readline().rstrip().split()]
maze = [stdin.readline().rstrip().split()[0] for _ in range(H)]

d = [[0] * W for _ in range(H)]

for h in range(H):
    for w in range(W):
        q = deque([(h, w, 0)])
        if maze[h][w] == '#':
            continue
        else:
            temp_h = h
            temp_w = w
            visited = [[0] * W for _ in range(H)]
            while len(q) > 0:
                temp = q.popleft()
                temp_h = temp[0]
                temp_w = temp[1]
                temp_d = temp[2]
                d[h][w] = max(d[h][w], temp_d)
                visited[temp_h][temp_w] = 1
                if temp_h + 1 < H and maze[temp_h + 1][temp_w] == '.' and visited[temp_h + 1][temp_w] == 0:
                    q.append((temp_h + 1, temp_w, temp_d + 1))
                if temp_h - 1 >= 0 and maze[temp_h - 1][temp_w] == '.' and visited[temp_h - 1][temp_w] == 0:
                    q.append((temp_h - 1, temp_w, temp_d + 1))
                if temp_w + 1 < W and maze[temp_h][temp_w + 1] == '.' and visited[temp_h][temp_w + 1] == 0:
                    q.append((temp_h, temp_w + 1, temp_d))
                if temp_w - 1 >= 0 and maze[temp_h][temp_w - 1] == '.' and visited[temp_h][temp_w - 1] == 0:
                    q.append((temp_h, temp_w - 1, temp_d + 1))

ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, d[i][j])
print(ans)

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    h,w = LI()
    a = [[c=='#' for c in S()] for _ in range(h)]
    s = (-1,-1)
    for i in range(h):
        for j in range(w):
            if not a[i][j]:
                s = (i,j)

    def search(s):
        d = collections.defaultdict(lambda: inf)
        d[s] = 0
        q = []
        heapq.heappush(q, (0, s))
        v = collections.defaultdict(bool)
        while len(q):
            k, u = heapq.heappop(q)
            if v[u]:
                continue
            v[u] = True

            for di,dj in dd:
                ni = u[0] + di
                nj = u[1] + dj
                if ni < 0 or ni >= h or nj < 0 or nj >= w or a[ni][nj]:
                    continue
                uv = (ni,nj)
                if v[uv]:
                    continue
                vd = k + 1
                if d[uv] > vd:
                    d[uv] = vd
                    heapq.heappush(q, (vd, uv))

        return d

    d = search(s)
    m = -1
    ms = (-1,-1)
    for k,v in d.items():
        if v > m and v < inf:
            m = v
            ms = k
    e = search(ms)

    m = -1
    ms = (-1,-1)
    for k,v in e.items():
        if v > m and v < inf:
            m = v
            ms = k
    e = search(ms)

    return max(e.values())


print(main())