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 numpy as np

def chk_route(c,s,T):
    global H,W,R
    mx = H*W+1
    #print(c)
    #print(T)
    flg = 0
    T[c[0]][c[1]]=s
    B=T.copy()
    if R[c[0]][c[1]] > s:
        R[c[0]][c[1]] = s

    if c[0] != H-1:
        #if T[c[0]+1][c[1]]!=1 and T[c[0]+1][c[1]]!=s-1 and T[c[0]+1][c[1]]!=2:
        if T[c[0]+1][c[1]]==0:
            flg=1

            chk_route([c[0]+1,c[1]],s+1,B)
    if c[0] != 0:
        #if T[c[0]-1][c[1]]!=1 and T[c[0]-1][c[1]]!=s-1 and T[c[0]-1][c[1]]!=2:
        if T[c[0]-1][c[1]]==0:
            flg=1

            chk_route([c[0]-1,c[1]],s+1,B)
    if c[1] != W-1:
        #if T[c[0]][c[1]+1]!=1 and T[c[0]][c[1]+1]!=s-1 and T[c[0]][c[1]+1]!=2:
        if T[c[0]][c[1]+1]==0:
            flg=1

            chk_route([c[0],c[1]+1],s+1,B)
    if c[1] != 0:
        #if T[c[0]][c[1]-1]!=1 and T[c[0]][c[1]-1]!=s-1 and T[c[0]][c[1]-1]!=2:
        if T[c[0]][c[1]-1]==0:
            flg=1

            chk_route([c[0],c[1]-1],s+1,B)

    if flg == 0:
        #print(T)
        #r=np.count_nonzero(T==2)
        if R[c[0]][c[1]] > s:
            R[c[0]][c[1]] = s
        #return s
    if s==2: pass #return mx
        #T=np.copy(B)
H,W= [int(x) for x in input().split()]
n=[]
#T = [[0] * W for i in range(H)]
T=np.zeros((H,W),dtype=int)
MX = H*W+1
R=np.full((H,W),MX,dtype = int)
#R = [[15] * W for i in range(H)]
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#print(T)
d=0
for i in range(H):
    for j in range(W):

        if T[i][j] ==1:continue

        c=[i,j]
        B=T.copy()
        R=np.full((H,W),MX,dtype = int)
        chk_route(c,int(2),B)
        #print(R)
        d = max(d,np.max(R[np.where(R<MX)]))

        #b= b[np.where(b[:,22].astype(float) > 0)]
print(d-2)
#print(mx)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import itertools

H, W = map(int, input().split())

l = []
m = [input() for i in range(H)]

for h in range(H):
    for w in range(W):
        if m[h][w] == '.':
            l.append([h, w])
lc = list(itertools.combinations(l, 2))

def f(m, s, x_l, o_l):
    o_l.append([s[0], s[1]])
    #print(s)
    if s[0] != 0:
        if [s[0]-1, s[1]] not in x_l and m[s[0]-1][s[1]] == '.' and [s[0]-1, s[1]] not in o_l: #hidari
          x_l.append([s[0]-1, s[1]])
    if s[0] != H-1:
        if [s[0]+1, s[1]] not in x_l and m[s[0]+1][s[1]] == '.' and [s[0]+1, s[1]] not in o_l: #migi
         x_l.append([s[0]+1, s[1]])
    if s[1] != 0:
        if [s[0], s[1]-1] not in x_l and m[s[0]][s[1]-1] == '.' and [s[0], s[1]-1] not in o_l: #ue
            x_l.append([s[0], s[1]-1])
    if s[1] != W-1:
        if [s[0], s[1]+1] not in x_l and m[s[0]][s[1]+1] == '.' and [s[0], s[1]+1] not in o_l: #shita
            x_l.append([s[0], s[1]+1])
    return x_l

for i in range(len(lc)):
    
    s, g = list(lc[i])
   # s, g = [0, 4], [2, 0]
    #print(s,g)
    o_l = [] #もう通ったマス
    x_l = [] #いま通っているマス
    count = 1
    max_count = 0
    
    x_l = f(m, s, x_l, o_l)
    
    while True:
        
        if g in x_l and count > max_count:
            max_count = count
            #print(s, g, max_count)
            break
        for j in range(len(x_l)):
            x_l = f(m, x_l[j], x_l, o_l)
        count += 1
    #print(i)
print(max_count)

Submission status: Wrong Answer

Solution code:
import copy
import queue
from itertools import combinations

def cal(s, g, f_org):
  f = copy.deepcopy(f_org)
  m = [(1, 0), (0, 1), (-1, 0), (0, -1)]
  q = queue.Queue()
  q.put(s)
  while not q.empty():
    x, y, t = q.get()
    if (x == g[0]) and (y == g[1]):
      break
    for i, j in m:
      if f[y+i][x+j] == '.':
        q.put((x+j, y+i, t+1))
        f[y+i][x+j] = '#'
  return t

def run():
  H, W = map(int, input().split())
  f = [['#']*(W+2)]
  f += [list('#'+input()+'#') for _ in range(H)]
  f += [['#']*(W+2)]
  t_li = []
  po_li = []
  for x in range(1, W+1):
    for y in range(1, H+1):
      if f[y][x] == '#':
        continue
      for i, j, k, l in [(0, 1, 0, -1), (1, 0, -1, 0)]:
        if f[y+i][x+j] == '.' and f[y+k][x+l] == '.':
          break
      else:
        po_li.append((x, y))
  for (sx, sy), (gx, gy) in combinations(po_li, 2):
    t = cal((sx, sy, 0), (gx, gy), f)
    #print(sx, sy, gx, gy, t)
    t_li.append(t)
  #print(t_li)
  print(max(t_li))

if __name__ == '__main__':
  run()

Submission status: Wrong Answer

Solution code:
from collections import deque

h, w = map(int, (input().split()))
maze = ['#' * (w + 2)] + ['#{}#'.format(input()) for i in range(h)] + ['#' * (w + 2)]
directions = [[0, -1], [-1, 0], [1, 0], [0, 1]]
queue = deque()
answer = 0

for start_x in range(1, h + 1):
    for start_y in range(1, w + 1):
        if maze[start_x][start_y] == '.':
            distance = [[0] * (w + 2) for i in range(h + 2)]
            queue.append([start_x, start_y])
            while queue:
                x, y = queue.popleft()
                for direction in directions:
                    next_x, next_y = x + direction[0], y + direction[1]
                    if maze[next_x][next_y] == '.' and distance[next_x][next_y] == 0:
                        distance[next_x][next_y] = distance[x][y] + 1
                        queue.append([next_x, next_y])
                        answer = max(answer, distance[next_x][next_y])

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
from queue import Queue
h,w=map(int,input().split())
S=[input() for _ in range(h)]

def bfs(q):
  x,y,cnt=q.get()
  if x<0 or x>=w or y<0 or y>=h:
    return
  elif S[y][x]=='#':
    return
  elif memo[y][x]<0:
    memo[y][x]=cnt
    q.put([x-1,y,cnt+1])
    q.put([x+1,y,cnt+1])
    q.put([x,y-1,cnt+1])
    q.put([x,y+1,cnt+1])

ans=0
for x in range(w):
  for y in range(h):
    q=Queue()
    q.put([x,y,0])
    memo=[[-1]*w for _ in range(h)]
    while not q.empty():
      bfs(q)
    ans=max(ans,max(max(memo)))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
from queue import Queue
h,w=map(int,input().split())
S=[input() for _ in range(h)]

def bfs(q):
  x,y,cnt=q.get()
  if x<0 or x>=w or y<0 or y>=h:
    return
  elif S[y][x]=='#':
    return
  elif memo[y][x]<0:
    memo[y][x]=cnt
    q.put([x-1,y,cnt+1])
    q.put([x+1,y,cnt+1])
    q.put([x,y-1,cnt+1])
    q.put([x,y+1,cnt+1])

ans=0
for x in range(w):
  for y in range(h):
    if x==h//2+1 and y==w//2+1:
      break
    q=Queue()
    q.put([x,y,0])
    memo=[[-1]*w for _ in range(h)]
    while not q.empty():
      bfs(q)
    ans=max(ans,max(max(memo)))
print(ans)

Submission status: Wrong Answer

Solution code:
from queue import Queue
h,w=map(int,input().split())
S=[input() for _ in range(h)]

ans=0
dxy=[(0,1),(1,0),(0,-1),(-1,0)]
for x in range(w):
  for y in range(h):
    if S[y][x]=='#': continue
    q=Queue()
    q.put([x,y])
    dist=[[0]*w for _ in range(h)]
    visited=[[0]*w for _ in range(h)]
    visited[y][x]=1
    while not q.empty():
      x,y=q.get()
      for dx,dy in dxy:
        nx,ny=x+dx,y+dy
        if not 0<=nx<w: continue
        if not 0<=ny<h: continue
        if visited[ny][nx]: continue
        if S[ny][nx] == '#': continue
        visited[ny][nx] = 1
        dist[ny][nx] = dist[y][x] + 1
        q.put([nx,ny])
    ans = max(ans, max(max(row) for row in dist))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque


def find_farthest(adj, start):
    distance = [float("inf")] * len(adj)
    q = deque([(start, 0)])
    while len(q):
        cur, d = q.popleft()
        if distance[cur] <= d:
            continue
        distance[cur] = d
        for nxt in adj[cur]:
            if distance[nxt] > d+1:
                q.append((nxt, d+1))
    ret = (-1, -1)
    for i, d in enumerate(distance):
        if d != float("inf") and d > ret[1]:
            ret = (i, d)
    return ret


def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    adj = [set() for _ in range(H*W)]
    start = None
    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                continue
            if i >= 1 and S[i-1][j] == ".":
                adj[i*W+j].add((i-1)*W+j)
                adj[(i-1)*W+j].add(i*W+j)
            if j >= 1 and S[i][j-1] == ".":
                adj[i*W+j].add(i*W+(j-1))
                adj[i*W+(j-1)].add(i*W+j)
            start = i*W+j
    nstart, _ = find_farthest(adj, start)
    nstart, _ = find_farthest(adj, nstart)
    _, ans = find_farthest(adj, nstart)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def find_farthest(adj, start):
    distance = [float("inf")] * len(adj)
    q = deque([(start, 0)])
    while len(q):
        cur, d = q.popleft()
        if distance[cur] <= d:
            continue
        distance[cur] = d
        for nxt in adj[cur]:
            if distance[nxt] > d+1:
                q.append((nxt, d+1))
    ret = (-1, -1)
    for i, d in enumerate(distance):
        if d != float("inf") and d > ret[1]:
            ret = (i, d)
    return ret


def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    adj = [set() for _ in range(H*W)]
    start = None
    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                continue
            if i >= 1 and S[i-1][j] == ".":
                adj[i*W+j].add((i-1)*W+j)
                adj[(i-1)*W+j].add(i*W+j)
            if j >= 1 and S[i][j-1] == ".":
                adj[i*W+j].add(i*W+(j-1))
                adj[i*W+(j-1)].add(i*W+j)
            start = i*W+j
    nstart, _ = find_farthest(adj, start)
    _, ans = find_farthest(adj, nstart)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def find_farthest(adj, start):
    distance = [-1] * len(adj)
    q = deque([(start, 0)])
    while len(q):
        cur, d = q.popleft()
        if distance[cur] <= d and distance[cur] != -1:
            continue
        distance[cur] = d
        for nxt in adj[cur]:
            if distance[nxt] == -1 or distance[nxt] > d+1:
                q.append((nxt, d+1))
    return max(enumerate(distance), key=lambda x: x[1])


def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    adj = [set() for _ in range(H*W)]
    start = None
    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                continue
            if i >= 1 and S[i-1][j] == ".":
                adj[i*W+j].add((i-1)*W+j)
                adj[(i-1)*W+j].add(i*W+j)
            if j >= 1 and S[i][j-1] == ".":
                adj[i*W+j].add(i*W+(j-1))
                adj[i*W+(j-1)].add(i*W+j)
            start = i*W+j
    nstart, _ = find_farthest(adj, start)
    _, ans = find_farthest(adj, nstart)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque


def find_farthest(adj, start):
    distance = [-1] * len(adj)
    q = deque([(start, 0)])
    while len(q):
        cur, d = q.popleft()
        if distance[cur] <= d and distance[cur] != -1:
            continue
        distance[cur] = d
        for nxt in adj[cur]:
            if distance[nxt] == -1:
                q.append((nxt, d+1))
    return max(enumerate(distance), key=lambda x: x[1])


def main():
    H, W = map(int, input().split())
    S = [input() for _ in range(H)]
    adj = [set() for _ in range(H*W)]
    start = None
    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                continue
            if i >= 1 and S[i-1][j] == ".":
                adj[i*W+j].add((i-1)*W+j)
                adj[(i-1)*W+j].add(i*W+j)
            if j >= 1 and S[i][j-1] == ".":
                adj[i*W+j].add(i*W+(j-1))
                adj[i*W+(j-1)].add(i*W+j)
            start = i*W+j
    nstart, _ = find_farthest(adj, start)
    _, ans = find_farthest(adj, nstart)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

dis_x = [1, 0, -1, 0]
dis_y = [0, 1, 0, -1]

h, w = map(int, input().split())
s = ["#{}#".format(input()) for _ in range(h)]
h += 2
w += 2

s = ["#" * w] + s + ["#" * w]

# 最終的な最大値
res_opt = -2

# 各ループ開始
for h_i in range(h):
  for w_i in range(w):
    if s[h_i][w_i] != '#':
      # BFS開始
      visited = [ [-1] * w for _ in range(h) ]
      visited[h_i][w_i] = 0
      queue = deque([(h_i, w_i)])
      
      while queue:
        x, y = queue.popleft()
        
        # 上下4マスに対して
        opt = 0
        for i in range(4):
          
          dx, dy = dis_x[i], dis_y[i]
          if s[x+dx][y+dy] == '.' and visited[x+dx][y+dy] == -1:
            visited[x+dx][y+dy] = visited[x][y] + 1
            opt += visited[x+dx][y+dy]
            queue.append((x+dx, y+dy))
        res_opt = max(res_opt, opt)

print(res_opt)

Submission status: Wrong Answer

Solution code:
from collections import deque

dis_x = [1, 0, -1, 0]
dis_y = [0, 1, 0, -1]

h, w = map(int, input().split())
s = ["#{}#".format(input()) for _ in range(h)]
h += 2
w += 2

s = ["#" * w] + s + ["#" * w]

# 最終的な最大値
opt = -2

# 各ループ開始
for h_i in range(h):
  for w_i in range(w):
    if s[h_i][w_i] != '#':
      
      # BFS開始
      visited = [ [-1] * w for _ in range(h) ]
      visited[h_i][w_i] = 0
      queue = deque([(h_i, w_i)])
      
      while queue:
        x, y = queue.popleft()
        
        # 上下4マスに対して
        for i in range(4):
          dx, dy = dis_x[i], dis_y[i]
          if s[x+dx][y+dy] == '.' and visited[x+dx][y+dy] == -1:
            visited[x+dx][y+dy] = visited[x][y] + 1
            opt = visited[x+dx][y+dy]
            queue.append((x+dx, y+dy))
            
print(opt)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int,input().split())
s = [input() for _ in range(h)]
ans = 0

def bfs(sh,sw,gh,gw):
    dh = (1,-1,0,0)
    dw = (0,0,1,-1)
    dist = [[-1 for _ in range(w)] for _ in range(h)]
    queue = deque()
    queue.append((sh,sw))
    dist[sh][sw] = 0
    while len(queue) != 0:
        ch,cw = queue.popleft()
        for i in range(4):
            nh,nw = ch+dh[i],cw+dw[i]
            if nh == gh and nw == gw:
                return dist[ch][cw]+1
            if not (0 <= nh <= h-1 and 0 <= nw <= w-1): continue
            if s[nh][nw] == "#": continue
            if dist[nh][nw] != -1: continue
            queue.append((nh,nw))
            dist[nh][nw] = dist[ch][cw]+1

for sh in range(h):
    for sw in range(w):
        if s[sh][sw] == "#":
            continue
        for gh in range(h):
            for gw in range(w):
                if s[gh][gw] == "#":
                    continue
                ans = max(ans,bfs(sh,sw,gh,gw))

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict
from itertools import chain, combinations
import json


def main():
    # N = int(input())
    H, W = [int(a) for a in input().split()]
    S = [
        input()
        for _ in range(H)
    ]

    def saitan(start, end):
        mem = {start}

        def get_rinsetsu(ii, jj):
            d = [
                (1,0),
                (-1, 0),
                (0, 1),
                (0, -1),
            ]
            res = []
            for di,dj in d:
                if di == 0 and dj == 0:
                    continue
                ni = ii + di
                nj = jj + dj
                if ni < 0 or nj < 0:
                    continue
                if ni >= H or nj >= W:
                    continue
                if S[ni][nj] == '.':
                    if (ni, nj) not in mem:
                        res.append((ni, nj))
            return res

        q = [(start, 0)]
        while len(q) != 0:
            (i, j), distance = q.pop()
            rinsetsu = get_rinsetsu(i, j)
            for r in rinsetsu:
                if r == end:
                    return distance + 1
                q.append((r, distance + 1))
                mem.add(r)
        return -1

    res = -1
    for a in range(H * W):
        start = a // W, a % W
        if S[start[0]][start[1]] == '#':
            continue
        for b in range(a + 1, H * W):
            end = b // W, b % W
            if S[end[0]][end[1]] == '#':
                continue
            ddd = saitan(start, end)
            # print(ddd,a,b)
            res = max(res, ddd)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np
h,w=map(int,input().split())
s=[[0 for i in range(w)] for j in range(h)]
for i in range(h):
	s[i]=list(input())
# h,w=3,3
# s=[['.', '.', '.'], ['.', '.', '.'], ['.', '.', '.']]
v=[[1000000 for i in range(w)] for j in range(h)]
s=np.array(s)
v=np.array(v)
def search(H,W,p):
	m=0
	if H<h-1 and s[H+1][W]=='.' and v[H+1][W]>p+1:
		v[H+1][W]=p+1
		m=p+1
	if H>0 and s[H-1][W]=='.' and v[H-1][W]>p+1:
		v[H-1][W]=p+1
		m=p+1
	if W>0 and s[H][W-1]=='.' and v[H][W-1]>p+1:
		v[H][W-1]=p+1
		m=p+1
	if W<w-1 and s[H][W+1]=='.' and v[H][W+1]>p+1:
		v[H][W+1]=p+1
		m=p+1
	return m

def dijk():
	f=False
	global P
	global M
	for i in range(h):
		for j in range(w):
			if v[i][j]==P:
				m=search(i,j,P)
				M=max(m,M)
				f=True

	P+=1
	if f==False:
		return 0
	dijk()
M_=0
for i in range(h):
	for j in range(w):
		v[i][j]=0
		P=0
		M=0
		if s[i][j]!='.':
			continue
		dijk()
		M_=max(M,M_)
		v=[[1000000 for i in range(w)] for j in range(h)]
print(M_)

Submission status: Wrong Answer

Solution code:
import copy
from collections import deque

h, w = map(int,input().split())

input_maze=[]
for i in range(h):
	input_maze.append(list(input()))

maze=[[-1 for j in range(w+2)] for i in range(h+2)]

#banpei
for i in range(h):
    for j in range(w):
        if input_maze[i][j]=='.':
            maze[i+1][j+1]=0
        else:
            maze[i+1][j+1]=-1

#print(maze)

max_route=0
direction=[[1,0],[-1,0],[0,1],[0,-1]]
for i in range(h):
    for j in range(w):
        copy_maze=copy.deepcopy(maze)
        start=[i+1,j+1]
        que=deque([])
        if maze[start[0]][start[1]]==0:
            copy_maze[start[0]][start[1]]=-1
            for d in direction:
                if copy_maze[start[0]+d[0]][start[1]+d[1]]==0:
                    que.append([start[0]+d[0],start[1]+d[1]])
                    copy_maze[start[0]+d[0]][start[1]+d[1]]=1
                    max_route=1
            while len(que)>0:
                tmp=que.popleft()
                for d in direction:
                    if copy_maze[tmp[0]+d[0]][tmp[1]+d[1]]==0:
                        que.append([tmp[0]+d[0],tmp[1]+d[1]])
                        copy_maze[tmp[0]+d[0]][tmp[1]+d[1]]=copy_maze[tmp[0]][tmp[1]]+1
                        if max_route<copy_maze[tmp[0]+d[0]][tmp[1]+d[1]]:
                            max_route=copy_maze[tmp[0]+d[0]][tmp[1]+d[1]]

                
print(max_route)

Submission status: Wrong Answer

Solution code:
import copy
from collections import deque

h, w = map(int,input().split())

input_maze=[]
for i in range(h):
	input_maze.append(list(input()))

maze=[[-1 for j in range(w+2)] for i in range(h+2)]

#banpei
for i in range(h):
    for j in range(w):
        if input_maze[i][j]=='.':
            maze[i+1][j+1]=0
        else:
            maze[i+1][j+1]=-1

#print(maze)

max_route=0
direction=[[1,0],[-1,0],[0,1],[0,-1]]
for i in range(h):
    for j in range(w):
        copy_maze=copy.deepcopy(maze)
        start=[i+1,j+1]
        que=deque([])
        if maze[start[0]][start[1]]==0:
            copy_maze[start[0]][start[1]]=-1
            for d in direction:
                if copy_maze[start[0]+d[0]][start[1]+d[1]]==0:
                    que.append([start[0]+d[0],start[1]+d[1]])
                    copy_maze[start[0]+d[0]][start[1]+d[1]]=1
            while len(que)>0:
                tmp=que.popleft()
                for d in direction:
                    if copy_maze[tmp[0]+d[0]][tmp[1]+d[1]]==0:
                        que.append([tmp[0]+d[0],tmp[1]+d[1]])
                        copy_maze[tmp[0]+d[0]][tmp[1]+d[1]]=copy_maze[tmp[0]][tmp[1]]+1
                        if max_route<copy_maze[tmp[0]+d[0]][tmp[1]+d[1]]:
                            max_route=copy_maze[tmp[0]+d[0]][tmp[1]+d[1]]

                
print(max_route)

Submission status: Wrong Answer

Solution code:
from collections import deque

H,W = map(int, input().split())
fields = []
for i in range(H):
    fields.append(list(input()))
# print(fields)

mvs = [(1,0),(-1,0),(0,1),(-1,0)]

ans = 0
for i1 in range(H):
    for j1 in range(W):
        if fields[i1][j1] == "#":continue
        s = (i1,j1)
        # 探索
        q = deque([])
        q.append(s)
        used = [[-1]*W for i in range(H)]
        used[i1][j1] = 0
        while len(q) > 0:
            h,w = q.popleft()
            for dh,dw in mvs:
                if 0 <= h+dh < H and 0 <= w+dw < W:
                    if used[h+dh][w+dw]==-1 and fields[h+dh][w+dw]==".":
                        q.append((h+dh,w+dw))
                        used[h+dh][w+dw] = used[h][w] + 1
        for i in range(H):
            for j in range(W):
                ans = max(ans, used[i][j])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

H,W = map(int, input().split())
fields = []
for i in range(H):
    fields.append(list(input()))
# print(fields)

mvs = [(1,0),(-1,0),(0,1),(-1,0)]

ans = 0
for i1 in range(H):
    for j1 in range(W):
        if fields[i1][j1] == "#":continue
        s = (i1,j1)
        # 探索
        q = deque([])
        q.append(s)
        used = [[0]*W for i in range(H)]
        while len(q) > 0:
            h,w = q.popleft()
            for dh,dw in mvs:
                if 0 <= h+dh < H and 0 <= w+dw < W:
                    if used[h+dh][w+dw]==0 and fields[h+dh][w+dw]==".":
                        q.append((h+dh,w+dw))
                        used[h+dh][w+dw] = used[h][w] + 1
        for i in range(H):
            for j in range(W):
                ans = max(ans, used[i][j])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

H,W = map(int, input().split())
fields = []
for i in range(H):
    fields.append(list(input()))
# print(fields)

mvs = [(1,0),(-1,0),(0,1),(-1,0)]

ans = 0
for i1 in range(H):
    for j1 in range(W):
        if fields[i1][j1] == "#":continue
        s = (i1,j1)
        # 探索
        q = deque([])
        q.append(s)
        used = [[0]*W for i in range(H)]
        while len(q) > 0:
            h,w = q.popleft()
            for dh,dw in mvs:
                if 0 <= h+dh < H and 0 <= w+dw < W:
                    if used[h+dh][w+dw]==0 and fields[h+dh][w+dw]==".":
                        q.append((h+dh,w+dw))
                        used[h+dh][w+dw] = used[h][w] + 1
        m = 0
        for i in range(H):
            for j in range(W):
                m = max(m, used[i][j])
        ans = max(m,ans)
print(ans)

Submission status: Wrong Answer

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

maze = [["."for i in range(W)] for j in range(H)]

for h in range(H):
    ret = input()
    for i, x in enumerate(ret):
        if x == "#":
            maze[h][i] = "#"
        # else: # == "."
        #     maze[h][i] = True

# print(maze)

def bfs(maze, y, x):
    q = [[y, x]]
    res = 0
    dist = [[-1] * W for j in range(H)]
    dist[y][x] = 0
    while len(q) > 0:
        yy, xx = q[0]
        res = max(res, dist[yy][xx])
        q.pop(0)
        for dy, dx in [[1,0], [0,1], [-1,0], [0,-1]]:
            ny = yy + dy
            nx = xx + dx
            if nx < 0 or ny < 0 or nx >= W or ny >= H:
                continue
            if maze[ny][nx] == '#':
                continue
            if dist[ny][nx] == -1:
                dist[ny][nx] = dist[yy][xx] + 1
                q.append([ny, nx])
    return res

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

Submission status: Wrong Answer

Solution code:

H, W = map(int, input().split())

maze = [[False for i in range(W)] for j in range(H)]

for h in range(H):
    ret = input()
    for i, x in enumerate(ret):
        if x == "#":
            pass
        else: # == "."
            maze[h][i] = True

def can_move(y, x, past, maze):
    if y < 0 or x < 0:
        return False
    if y >= H or x >= W:
        return False
    for yy, xx in past:
        if y == yy and x == xx:
            return False
    ret = maze[y][x]
    return ret

def past_append(past, y, x):
    flag = True
    for yy, xx in past:
        if y == yy and x == xx:
            flag = False
    if flag:
        past.append([y, x])

def bfs(maze, y, x):
    q = [[y, x]]
    r = [0]
    past = []
    max_len = 0
    while len(q) > 0:
        cur_y, cur_x = q.pop(0) # dequeue
        cur_len = r.pop(0)
        past_append(past, cur_y, cur_x)
        for dx, dy in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
            ny = cur_y + dy
            nx = cur_x + dx
            if can_move(ny, nx, past, maze):
                q.append([ny, nx])
                r.append(cur_len + 1)
            past_append(past, ny, nx)
    return cur_len

max_len = 0
for j in range(H):
    for i in range(W):
        mlen = bfs(maze, j, i)
        if max_len < mlen:
            max_len = mlen
print(max_len)

Submission status: Wrong Answer

Solution code:
from collections import deque

H,W=map(int,input().split())
Map=[input() for j in range(H)]

def bps(si,sj):
    distance=[[-1]*W for j in range(H)]
    distance[si][sj]=0
    deq=deque([[si,sj]])
    fdis=0
    while deq:
        i,j=deq.popleft()
        for dir_i,dir_j in [(-1,0),(1,0),(0,-1),(0,1)]:
            if 0<=i+dir_i<H and 0<=j+dir_j<W and distance[i+dir_i][j+dir_j]==-1 and Map[i+dir_i][j+dir_j]==".":
                distance[i+dir_i][j+dir_j]=fdis=distance[i][j]+1
                deq.append([i+dir_i,j+dir_j])
    return fdis

bestMapDistance=0
for i in range(H):
    for j in range(W):
        bestMapDistance=max(bps(i,j),bestMapDistance)

print(bestMapDistance)

Submission status: Wrong Answer

Solution code:
import collections
H,W = map(int,input().split())
LL=[]
for i in range(H):
    LL.append(input())
    
L = ((1,0),(0,1),(-1,0),(0,-1))
q = collections.deque()
max_L = []
for i in range(H):
    for j in range(W):
        d = [[0]*W for i in range(H)]
        if LL[i][j] ==".":
            q.append((i,j))
            d[i][j] = 0
            max1 = 0
            while q:
                x,y = q.popleft()
                for dx,dy in L:
                    if 0>x+dx or x+dx>H-1 or 0>y+dy or y+dy>W-1 or d[x+dx][y+dy] !=0:
                        continue
                    if  LL[x+dx][y+dy] == ".":
                        d[x+dx][y+dy] = d[x][y] +1
                        q.append((x+dx,y+dy))
                        max1 = max(max1,d[x+dx][y+dy])
                        max_L.append(max1)
                        #print(q,d ,max1)
print(max(max_L))

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
L = []
for i in range(H):
    L.append(input())
import collections
q = collections.deque()
li = ((1,0),(0,1),(-1,0),(0,-1))
LL=[]
cnt = 0
for i in range(H):
    for j in range(W):
        visited = [[-1]*W for i in range(H)]
        visited[i][j] = 0
        q.append((i,j))
        #print(q)
        while q:
            x,y = q.popleft()
            for dx,dy in li:
                nx,ny = x+dx,y+dy
                #print(nx,ny)
                if nx<0 or nx>=H or ny<0 or ny>=W or visited[nx][ny] >0:
                    continue
                if visited[nx][ny] ==-1 and  L[nx][ny] == ".": 
                    visited[nx][ny] = visited[x][y] +1
                    cnt = max(cnt,visited[nx][ny])
                    q.append((nx,ny))
        LL.append(cnt)
print(max(LL))

Submission status: Wrong Answer

Solution code:
from collections import deque

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

move = ((1, 0), (-1, 0), (0, 1), (0, -1))


def bfs(i, j):
    visited = [[False] * W for _ in range(H)]
    visited[i][j] = True
    que = deque([(i, j, 0)])

    while que:
        ci, cj, d = que.popleft()

        for di, dj in move:
            ni, nj = ci + di, cj + dj
            if not (0 <= ni < H and 0 <= nj < W) or visited[ni][nj]:
                continue
            if S[ni][nj] == '.':
                visited[ni][nj] = True
                que.append((ni, nj, d+1))

    return d


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

Submission status: Wrong Answer

Solution code:
from itertools import product


def solve(string):
    h, w, *os = string.split()
    h, w = int(h), int(w)
    s, os = [_s + "#" for _s in os] + ["#" * (w + 1)], "".join(os)
    n = [[] for _ in range(h * w)]
    for i, j in product(range(h), range(w)):
        if s[i][j] == "#":
            continue
        for x, y in [(1, 0), (0, 1)]:
            p, q = i + x, j + y
            if s[p][q] == ".":
                n[i * w + j].append(p * w + q)
                n[p * w + q].append(i * w + j)
    print(n)
    ans = 0
    for i, j in product(range(h), range(w)):
        c = i * w + j
        if not n[c]:
            continue
        t = [h * w + 1] * (h * w)
        t[c] = 0
        q = list()
        for d in n[c]:
            q.append((d, c))
        while q:
            d, c = q.pop()
            if t[c] + 1 >= t[d]:
                continue
            t[d] = t[c] + 1
            for _d in n[d]:
                q.append((_d, d))
        for i in range(h * w):
            ans = max(ans, t[i] * (os[i] == "."))
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))