Problem: Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.  The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is `#`, and a "road" square if S_{ij} is `.`.  From a road square, you can move to a horizontally or vertically adjacent road square.  You cannot move out of the maze, move to a wall square, or move diagonally.  Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.  Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.  In this situation, find the maximum possible number of moves Aoki has to make.

Submission attempts (in order):
H,W = map(int,input().split())                                   
a = [['#']+[i for i in input()]+['#'] for j in range(H)]
a = [['
b=[x for x in a if "." in x] 
c=zip(*[y for y in zip(*b) if "." in y]) 
graph = []
for d in c:
  graph.append(list(d))
H = len(graph)
W = len(graph[0])
for i in range(H):
  graph[i].append('#')
  graph[i].insert(0,'#')
graph = [['
import copy

def dfs(r,c,goal):
    visited = [[False for i in range(W+2)] for j in range(H+2)]
    step = 0
    queue = [[r,c]]
    if graph[r][c]=='#':
      return -1
    queue_2 = []
    while len(queue) != 0:
        step += 1
        for now in queue:
          visited[now[0]][now[1]] = True
          for next in [[1,0],[0,1],[-1,0],[0,-1]]:
            if visited[now[0]+next[0]][now[1]+next[1]] == True:continue
            if graph[now[0]+next[0]][now[1]+next[1]] == '#':continue
            if [now[0]+next[0],now[1]+next[1]] == goal:return step
            if [now[0]+next[0],now[1]+next[1]] in queue_2:continue
            queue_2.append([now[0]+next[0],now[1]+next[1]])
        queue.clear()
        queue = copy.deepcopy(queue_2)
        queue_2.clear()
    return -1
  
print(max(dfs(1,1,[H,W]),dfs(H,1,[1,W])))

H,W = map(int,input().split())                                   
a = [['#']+[i for i in input()]+['#'] for j in range(H)]
a = [['
b=[x for x in a if "." in x] 
c=zip(*[y for y in zip(*b) if "." in y]) 
graph = []
for d in c:
  graph.append(list(d))
H = len(graph)
W = len(graph[0])
for i in range(H):
  graph[i].append('#')
  graph[i].insert(0,'#')
graph = [['
import copy

def dfs(start):
    visited = [[False for i in range(W+2)] for j in range(H+2)]
    step = 0
    queue = [start]
    queue_2 = []
    steps = []
    while len(queue) != 0:
        step += 1
        for now in queue:
          visited[now[0]][now[1]] = True
          for next in [[1,0],[0,1],[-1,0],[0,-1]]:
            if visited[now[0]+next[0]][now[1]+next[1]] == True:continue
            if graph[now[0]+next[0]][now[1]+next[1]] == '#':continue
            if [now[0]+next[0],now[1]+next[1]] in queue_2:continue
            queue_2.append([now[0]+next[0],now[1]+next[1]])
        queue.clear()
        queue = copy.deepcopy(queue_2)
        queue_2.clear()
    return step-1
  
hidariue = [1,1]
migiue = [1,W]
hidarisita = [H,1]
migisita = [H,W]
while True:
  if graph[hidariue[0]][hidariue[1]]=='#':
    hidariue[0]+=1
  elif graph[migiue[0]][migiue[1]]=='#':
    migiue[0]+=1
  elif graph[hidarisita[0]][hidarisita[1]]=='#':
    hidarisita[0]-=1
  elif graph[migisita[0]][migisita[1]]=='#':
    migisita[0]-=1
  else:
    break

a = dfs(hidariue)
b = dfs(hidarisita)
c = dfs(migiue)
d = dfs(migisita)
print(max(a,b,c,d))

H,W = map(int,input().split())                                   
a = [['#']+[i for i in input()]+['#'] for j in range(H)]
a = [['
b=[x for x in a if "." in x] 
c=zip(*[y for y in zip(*b) if "." in y]) 
graph = []
for d in c:
  graph.append(list(d))
H = len(graph)
W = len(graph[0])
for i in range(H):
  graph[i].append('#')
  graph[i].insert(0,'#')
graph = [['
import copy

def dfs(start,goal):
    visited = [[False for i in range(W+2)] for j in range(H+2)]
    step = 0
    queue = [start]
    queue_2 = []
    while len(queue) != 0:
        step += 1
        for now in queue:
          visited[now[0]][now[1]] = True
          for next in [[1,0],[0,1],[-1,0],[0,-1]]:
            if visited[now[0]+next[0]][now[1]+next[1]] == True:continue
            if graph[now[0]+next[0]][now[1]+next[1]] == '#':continue
            if [now[0]+next[0],now[1]+next[1]] == goal:return step
            if [now[0]+next[0],now[1]+next[1]] in queue_2:continue
            queue_2.append([now[0]+next[0],now[1]+next[1]])
        queue.clear()
        queue = copy.deepcopy(queue_2)
        queue_2.clear()
    return -1
hidariue = [1,1]
migiue = [1,W]
hidarisita = [H,1]
migisita = [H,W]
while True:
  if graph[hidariue[0]][hidariue[1]]=='#':
    hidariue[0]+=1
  elif graph[migiue[0]][migiue[1]]=='#':
    migiue[0]+=1
  elif graph[hidarisita[0]][hidarisita[1]]=='#':
    hidarisita[0]-=1
  elif graph[migisita[0]][migisita[1]]=='#':
    migisita[0]-=1
  else:
    break

a = dfs(hidariue,hidarisita)
b = dfs(hidariue,migiue)
c = dfs(hidariue,migisita)
d = dfs(hidarisita,migiue)
e = dfs(hidarisita,migisita)
f = dfs(migiue,migisita)
print(max(a,b,c,d,e,f))

H,W = map(int,input().split())
visited = [[False for i in range(W+2)] for j in range(H+2)]                                     
graph = [['#']+[i for i in input()]+['#'] for j in range(H)]
graph = [['
 
import copy

def dfs(r,c,goal):
    visited = [[False for i in range(W+2)] for j in range(H+2)]
    step = 0
    queue = [[r,c]]
    if graph[r][c]=='#':
      return -1
    queue_2 = []
    while len(queue) != 0:
        step += 1
        for now in queue:
          visited[now[0]][now[1]] = True
          for next in [[1,0],[0,1],[-1,0],[0,-1]]:
            if visited[now[0]+next[0]][now[1]+next[1]] == True:continue
            if graph[now[0]+next[0]][now[1]+next[1]] == '#':continue
            if [now[0]+next[0],now[1]+next[1]] == goal:return step
            if [now[0]+next[0],now[1]+next[1]] in queue_2:continue
            queue_2.append([now[0]+next[0],now[1]+next[1]])
        queue.clear()
        queue = copy.deepcopy(queue_2)
        queue_2.clear()
    return -1

print(max(dfs(1,1,[H,W]),dfs(H,1,[1,W])))

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

def bfs(sx,sy):
  queue = [[sx,sy,0]]
  visited = [[False for i in range(W)] for j in range(H)]
  while queue:
    now = queue.pop(0)
    visited[now[0]][now[1]] = True
    for next in [[1,0],[-1,0],[0,1],[0,-1]]:
      if now[0]+next[0] < 0 or now[1]+next[1] < 0: continue
      if now[0]+next[0] > H-1 or now[1]+next[1] > W-1: continue
      if visited[now[0]+next[0]][now[1]+next[1]]: continue
      if graph[now[0]+next[0]][now[1]+next[1]]=='#': continue
      queue.append([now[0]+next[0],now[1]+next[1],now[2]+1])
      visited[now[0]+next[0]][now[1]+next[1]] = True
  return now[2]

ans = -1
for i in range(H):
  for j in range(W):
    if graph[i][j]=='#': continue
    ans = max(ans,bfs(i,j))
    
print(ans)

Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Accepted