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:
def grid2graph(grid):
    # .が道で#が壁のグリッドをグラフに変換する
    H, W = len(grid), len(grid[0])
    f = {}
    cnt = 0
    for h in range(H):
        for w in range(W):
            if grid[h][w] == '.':
                f[(h, w)] = cnt
                cnt += 1
    G = [[] for i in range(cnt)]
    for h in range(H):
        for w in range(W):
            if grid[h][w] == '#':
                continue
            i = f[(h, w)]
            if 0 <= h-1 and grid[h-1][w] == '.':
                j = f[(h-1, w)]
                G[i].append(j)
            if 0 <= w-1 and grid[h][w-1] == '.':
                j = f[(h, w-1)]
                G[i].append(j)
            if h+1 < H and grid[h+1][w] == '.':
                j = f[(h+1, w)]
                G[i].append(j)
            if w+1 < W and grid[h][w+1] == '.':
                j = f[(h, w+1)]
                G[i].append(j)
    return G


def warshall_floyd(G):
    inf = 10**18
    n = len(G)
    d = [[inf for i in range(n)] for j in range(n)]
    for i in range(n):
        d[i][i] = 0
        for j in G[i]:
            d[i][j] = 1
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k]+d[k][j])
    return d


H, W = map(int, input().split())
S = []
for h in range(H):
    S.append(list(input()))
G = grid2graph(S)
d = warshall_floyd(G)
n = len(d)
ans = 0
for i in range(n-1):
    for j in range(n):
        ans = max(ans, d[i][j])

Submission status: Wrong Answer

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

def bfs(startx,starty):
    d = [[-1]*W for _ in range(H)]
    q = deque([[startx,starty]])
    d[startx][starty] = 0
    cnt = 0
    while(q):
        x,y = q.popleft()
        for i,j in ([1,0],[-1,0],[0,1],[0,-1]):
            new_x,new_y = x+i,y+j
            if(new_x>H-1 or new_x<0 or new_y>W-1 or new_y<0):
                continue
            elif(S[new_x][new_y] == "#"):
                continue
            elif(d[new_x][new_y] == -1):
                d[new_x][new_y] = d[x][y] + 1
                q.append([new_x,new_y])
                cnt = max(cnt,d[new_x][new_y])
        print(d)
    return cnt

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

Submission status: Wrong Answer

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

def bfs(startx,starty):
    d = [[-1]*W for _ in range(H)]
    q = deque([[startx,starty]])
    d[startx][starty] = 0
    cnt = 0
    while(q):
        x,y = q.popleft()
        for i,j in ([1,0],[-1,0],[0,1],[0,-1]):
            new_x,new_y = x+i,y+j
            if(new_x>H-1 or new_x<0 or new_y>W-1 or new_y<0):
                continue
            elif(S[new_x][new_y] == "#"):
                continue
            elif(d[new_x][new_y] == -1):
                d[new_x][new_y] = d[x][y] + 1
                q.append([new_x,new_y])
                cnt = max(cnt,d[new_x][new_y])
    #print(d)
    return cnt

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:
import sys

h,w = map(int,input().split())
s = [input() for i in range(h)]
do = []

mxm = 0
mmm = 0
mxcx = -1
mxcy = -1

for i in range(h):
    for j in range(w):
      if(mxcx == i and mxcy == j):
        print(mxm)
        sys.exit()
        
      if(s[i][j] == '.'):
        do.append([i,j])
        ex = [[0]*w for i in range(h)]
        ex[i][j] = 1
      else:
        continue
        
      while(len(do)!=0):
        x,y = do.pop(0)
        
        for k,l in [[1,0],[-1,0],[0,1],[0,-1]]:
          a = x+k
          b = y+l
          
          if(-1 < a < h and -1 < b < w):
            if(s[a][b] == '.' and ex[a][b] == 0):
              hantei = ex[x][y] + 1
              ex[a][b] = hantei
              o = a
              p = b
              do.append([a,b])
              
      
      for i in range(h):
        if(mmm < max(ex[i])):
          mmm = max(ex[i])
      #for i in range(h):
      #  print(ex[i])
      #print("")
      #print(hantei -1)           
        
      if(mxm < hantei - 1):
        mxm = hantei - 1
        mxcx = o
        mxcy = p
print(mxm)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

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

def bfs(sy,sx):
    queue = deque([[sy,sx]])
    log = [[0 for i in range(W)] for j in range(H)]
    while queue:
        p = queue.popleft()

        for i,j in [1,0],[-1,0],[0,1],[0,-1]:
            next_p = [p[0]+i,p[1]+j]
            
            if 0 <= next_p[0] < H and 0 <= next_p[1] < W:
                if next_p[0] == sy and next_p[1] == sx:
                    continue

                if maze[next_p[0]][next_p[1]] == '.'\
                    and log[next_p[0]][next_p[1]] == 0:
                    log[next_p[0]][next_p[1]] = log[p[0]][p[1]] + 1
                    queue.append([next_p[0],next_p[1]])
    
    return log

for i in range(H):
    for j in range(W):
        if maze[i][j] == '.':
            tmp_i = i
            tmp_j = j
            break


tmplog = bfs(tmp_i,tmp_j)
max_v = 0

for i in range(H):
    for j in range(W):
        if tmplog[i][j] > max_v:
            max_v = tmplog[i][j]
            s_y = i
            s_x = j

finlog = bfs(s_y,s_x)

for i in range(H):
    for j in range(W):
        if finlog[i][j] > max_v:
            max_v = finlog[i][j]

print(max_v)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

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

def bfs(sy,sx):
    queue = deque([[sy,sx]])
    log = [[0 for i in range(W)] for j in range(H)]
    while queue:
        p = queue.popleft()

        for i,j in [1,0],[-1,0],[0,1],[0,-1]:
            next_p = [p[0]+i,p[1]+j]
            
            if 0 <= next_p[0] < H and 0 <= next_p[1] < W:
                if next_p[0] == sy and next_p[1] == sx:
                    continue

                if maze[next_p[0]][next_p[1]] == '.'\
                    and log[next_p[0]][next_p[1]] == 0:
                    log[next_p[0]][next_p[1]] = log[p[0]][p[1]] + 1
                    queue.append([next_p[0],next_p[1]])
                    print(log)
    
    return log

for i in range(H):
    for j in range(W):
        if maze[i][j] == '.':
            tmp_i = i
            tmp_j = j
            break


tmplog = bfs(tmp_i,tmp_j)
max_v = 0

for i in range(H):
    for j in range(W):
        if tmplog[i][j] > max_v:
            max_v = tmplog[i][j]
            s_y = i
            s_x = j

finlog = bfs(s_y,s_x)

for i in range(H):
    for j in range(W):
        if finlog[i][j] > max_v:
            max_v = finlog[i][j]

print(max_v)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

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

def bfs(sy,sx):
    queue = deque([[sy,sx]])
    log = [[0 for i in range(W)] for j in range(H)]
    while queue:
        p = queue.popleft()

        for i,j in [1,0],[-1,0],[0,1],[0,-1]:
            next_p = [p[0]+i,p[1]+j]
            
            if 0 <= next_p[0] < H and 0 <= next_p[1] < W:
                if maze[next_p[0]][next_p[1]] == '.'\
                    and log[next_p[0]][next_p[1]] == 0:
                    log[next_p[0]][next_p[1]] = log[p[0]][p[1]] + 1
                    queue.append([next_p[0],next_p[1]])
    
    return log

for i in range(H):
    for j in range(W):
        if maze[i][j] == '.':
            tmp_i = i
            tmp_j = j
            break

tmplog = bfs(tmp_i,tmp_j)
max_v = 0

for i in range(H):
    for j in range(W):
        if tmplog[i][j] > max_v:
            max_v = tmplog[i][j]
            tmp_i = i
            tmp_j = j

tmplog = bfs(tmp_i,tmp_j)
max_v = 0

for i in range(H):
    for j in range(W):
        if tmplog[i][j] > max_v:
            max_v = tmplog[i][j]
            s_y = i
            s_x = j

finlog = bfs(s_y,s_x)

for i in range(H):
    for j in range(W):
        if finlog[i][j] > max_v:
            max_v = finlog[i][j]

print(max_v)

Submission status: Wrong Answer

Solution code:
import Queue
import copy

def main():
    H, W = map(int, raw_input().split())
    S = []

    for i in range(H):
        S.append(list(raw_input()))


    def bfs(start, S2):
        q = Queue.Queue()
        q.put(start)
        candidates = [[0, 1], [1, 0], [0, -1], [-1, 0]]
        dist = [[0]*W for _ in range(H)]
        while not q.empty():
            i = q.get()
            x = i[0]
            y = i[1]

            for candi in candidates:
                x_new = x + candi[0]
                y_new = y + candi[1]

                if x_new < H and x_new >= 0:
                    if y_new < W and y_new >=0:
                        if S2[x_new][y_new] == ".":
                            dist[x_new][y_new] = dist[x][y] + 1
                            S2[x_new][y_new] = "#"
		            q.put([x_new, y_new])

        ans = 0
        for i in range(H):
            ans = max(ans, max(dist[i]))

        return ans

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

    print ans


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

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

def bfs(sy,sx):
    queue = deque([[sy,sx]])
    log = [[0 for i in range(W)] for j in range(H)]
    while queue:
        p = queue.popleft()

        for i,j in [1,0],[-1,0],[0,1],[0,-1]:
            next_p = [p[0]+i,p[1]+j]
            
            if 0 <= next_p[0] < H and 0 <= next_p[1] < W:
                if maze[next_p[0]][next_p[1]] == '.'\
                    and log[next_p[0]][next_p[1]] == 0:
                    log[next_p[0]][next_p[1]] = log[p[0]][p[1]] + 1
                    queue.append([next_p[0],next_p[1]])
    
    return log

for i in range(H):
    for j in range(W):
        if maze[i][j] == '.':
            tmp_i = i
            tmp_j = j
            break

tmplog = bfs(tmp_i,tmp_j)
max_v = 0

for i in range(H):
    for j in range(W):
        if tmplog[i][j] > max_v:
            max_v = tmplog[i][j]
            s_y = i
            s_x = j

finlog = bfs(s_y,s_x)

for i in range(H):
    for j in range(W):
        if finlog[i][j] > max_v:
            max_v = finlog[i][j]

print(max_v)

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = map(int, input().split())
meiro = []
INF = 10**9
meiro.append(["#"]*(W+2))
for _ in range(H):
    temp = list(input())
    temp.append("#")
    temp.insert(0,"#")
    meiro.append(temp)
meiro.append(["#"]*(W+2))

def bfs(i,j):
    INF = 10 ** 9
    dist = [[INF for i in range(W+2)] for j in range(H+2)]
    dist[i][j] = 0
    q = deque([[i,j]])
    while q:
        v = q.popleft()
        dv = dist[v[0]][v[1]]
        
        if meiro[v[0]-1][v[1]] != "#":
            if dist[v[0]][v[1]]+1 < dist[v[0]-1][v[1]]:
                dist[v[0]-1][v[1]] = dist[v[0]][v[1]]+1
                q.append([v[0]-1,v[1]])
                
        if meiro[v[0]+1][v[1]] != "#":
            if dist[v[0]][v[1]]+1 < dist[v[0]+1][v[1]]:
                dist[v[0]+1][v[1]] = dist[v[0]][v[1]]+1
                q.append([v[0]+1,v[1]])
        
        if meiro[v[0]][v[1]-1] != "#":
            if dist[v[0]][v[1]]+1 < dist[v[0]][v[1]-1]:
                dist[v[0]][v[1]-1] = dist[v[0]][v[1]]+1
                q.append([v[0],v[1]-1])
        
        if meiro[v[0]][v[1]+1] != "#":
            if dist[v[0]][v[1]]+1 < dist[v[0]][v[1]+1]:
                dist[v[0]][v[1]+1] = dist[v[0]][v[1]]+1
                q.append([v[0],v[1]+1])
    
    dist = [[0 if x == INF else x for x in inner] for inner in dist]
    return max(max(dist))

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

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = map(int, input().split())
meiro = []
INF = 10**9
meiro.append(["#"]*(W+2))
for _ in range(H):
    temp = list(input())
    temp.append("#")
    temp.insert(0,"#")
    meiro.append(temp)
meiro.append(["#"]*(W+2))

def bfs(i,j):
    INF = 10 ** 9
    dist = [[INF for i in range(W+2)] for j in range(H+2)]
    dist[i][j] = 0
    q = deque([[i,j]])
    while q:
        v = q.popleft()
        dv = dist[v[0]][v[1]]
        
        if meiro[v[0]-1][v[1]] != "#":
            if dist[v[0]][v[1]]+1 < dist[v[0]-1][v[1]]:
                dist[v[0]-1][v[1]] = dist[v[0]][v[1]]+1
                q.append([v[0]-1,v[1]])
                
        if meiro[v[0]+1][v[1]] != "#":
            if dist[v[0]][v[1]]+1 < dist[v[0]+1][v[1]]:
                dist[v[0]+1][v[1]] = dist[v[0]][v[1]]+1
                q.append([v[0]+1,v[1]])
        
        if meiro[v[0]][v[1]-1] != "#":
            if dist[v[0]][v[1]]+1 < dist[v[0]][v[1]-1]:
                dist[v[0]][v[1]-1] = dist[v[0]][v[1]]+1
                q.append([v[0],v[1]-1])
        
        if meiro[v[0]][v[1]+1] != "#":
            if dist[v[0]][v[1]]+1 < dist[v[0]][v[1]+1]:
                dist[v[0]][v[1]+1] = dist[v[0]][v[1]]+1
                q.append([v[0],v[1]+1])
    
    dist = [[0 if x == INF else x for x in inner] for inner in dist]
    return max(max(dist))

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

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    ss=[a for a in s]
    S.append(ss)
answers=[]
start=[]
for h,c_h in enumerate(S):
    for w,c_w in enumerate(c_h):
        if c_w=='.':
            start.append([h,w])
for a in start:
    stack=[]
    stack.append(a)
    visited_area=[[0 for _ in range(W)] for _ in range(H)]
    def bfs(S,visited_area,stack):
        while len(stack)>0:
            h,w=stack.pop()
            for x,y in ([1,0],[-1,0],[0,1],[0,-1]):
                new_h=h+x
                new_w=w+y
                if new_h<0 or new_h>=H or new_w<0 or new_w>=W:
                    continue
                if S[new_h][new_w]=='.' and visited_area[new_h][new_w]==0:
                    visited_area[new_h][new_w]=visited_area[h][w]+1
                    stack.insert(0,[new_h,new_w])
        kotae=[]
        for z in range(H):
            saidai=max(visited_area[z])
            kotae.append(saidai)
        large=max(kotae)
        answers.append(large)
    bfs(S, visited_area, stack)
if len(answers)==0:
    print(0)
else:
    print(int(max(answers)))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    ss=[a for a in s]
    S.append(ss)
answers=[]
start=[]
for h,c_h in enumerate(S):
    for w,c_w in enumerate(c_h):
        if c_w=='.':
            start.append([h,w])
for a in start:
    stack=[]
    stack.append(a)
    visited_area=[[0 for _ in range(W)] for _ in range(H)]
    def bfs(S,visited_area,stack):
        while len(stack)>0:
            h,w=stack.pop()
            for x,y in ([1,0],[-1,0],[0,1],[0,-1]):
                new_h=h+x
                new_w=w+y
                if new_h<0 or new_h>=H or new_w<0 or new_w>=W:
                    continue
                if S[new_h][new_w]=='.' and visited_area[new_h][new_w]==0:
                    visited_area[new_h][new_w]=visited_area[h][w]+1
                    stack.insert(0,[new_h,new_w])
        kotae=[]
        for z in range(H):
            saidai=max(visited_area[z])
            kotae.append(saidai)
        large=max(kotae)
        answers.append(large)
    bfs(S, visited_area, stack)
if len(answers)==0:
    print(0)
else:
    print(max(answers))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    ss=[a for a in s]
    S.append(ss)
answers=[]
start=[]
for h,c_h in enumerate(S):
    for w,c_w in enumerate(c_h):
        if c_w=='.':
            start.append([h,w])
for a in start:
    stack=[]
    stack.append(a)
    visited_area=[[0 for _ in range(W)] for _ in range(H)]
    def bfs(S,visited_area,stack):
        while len(stack)>0:
            h,w=stack.pop()
            for x,y in ([1,0],[-1,0],[0,1],[0,-1]):
                new_h=h+x
                new_w=w+y
                if new_h<0 or new_h>=H or new_w<0 or new_w>=W:
                    continue
                if S[new_h][new_w]=='.' and visited_area[new_h][new_w]==0:
                    visited_area[new_h][new_w]=visited_area[h][w]+1
                    stack.insert(0,[new_h,new_w])
        kotae=[]
        for z in range(H):
            saidai=max(visited_area[z])
            kotae.append(saidai)
        large=max(kotae)
        answers.append(large)
    bfs(S, visited_area, stack)
print(max(answers))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    ss=[a for a in s]
    S.append(ss)
answers=[]
start=[]
for h,c_h in enumerate(S):
    for w,c_w in enumerate(c_h):
        if c_w=='.':
            start.append([h,w])
    for a in start:
        stack=[]
        stack.append(a)
        visited_area=[[0 for _ in range(W)] for _ in range(H)]
        def bfs(S,visited_area,stack):
            while len(stack)>0:
                h,w=stack.pop()
                for x,y in ([1,0],[-1,0],[0,1],[0,-1]):
                    new_h=h+x
                    new_w=w+y
                    if new_h<0 or new_h>=H or new_w<0 or new_w>=W:
                        continue
                    if S[new_h][new_w]=='.' and visited_area[new_h][new_w]==0:
                        visited_area[new_h][new_w]=visited_area[h][w]+1
                        stack.insert(0,[new_h,new_w])
            kotae=[]
            for z in range(H):
                saidai=max(visited_area[z])
                kotae.append(saidai)
            large=max(kotae)
            answers.append(large)
        bfs(S, visited_area, stack)
print(max(answers))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
S=[]
for _ in range(H):
    s=input()
    ss=[a for a in s]
    S.append(ss)
answers=[]
start=[]
for h,c_h in enumerate(S):
    for w,c_w in enumerate(c_h):
        if c_w=='.':
            start.append([h,w])
if len(start)==0:
    print(0)
else:
    for a in start:
        stack=[]
        stack.append(a)
        visited_area=[[0 for _ in range(W)] for _ in range(H)]
        def bfs(S,visited_area,stack):
            while len(stack)>0:
                h,w=stack.pop()
                for x,y in ([1,0],[-1,0],[0,1],[0,-1]):
                    new_h=h+x
                    new_w=w+y
                    if new_h<0 or new_h>=H or new_w<0 or new_w>=W:
                        continue
                    if S[new_h][new_w]=='.' and visited_area[new_h][new_w]==0:
                        visited_area[new_h][new_w]=visited_area[h][w]+1
                        stack.insert(0,[new_h,new_w])
            kotae=[]
            for z in range(H):
                saidai=max(visited_area[z])
                kotae.append(saidai)
            large=max(kotae)
            answers.append(large)
        bfs(S, visited_area, stack)
    print(max(answers))

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
ans=-1
dx=[1,0,-1,0]
dy=[0,1,0,-1]
for sy in range(h):
  for sx in range(w):
    for gy in range(h):
      for gx in range(w):
        if sy==gy and sx==gx:
          continue
        if s[sy][sx]=='#' or s[gy][gx]=='#':
          continue
        q=deque()
        q.append((sy,sx,0))
        visited=[[False]*w for i in range(h)]
        visited[sy][sx]=True
        while q:
          y,x,cost=q.pop()
          visited[y][x]=True
          for i in range(4):
            ny,nx=y+dy[i],x+dx[i]
            if ny==gy and nx==gx:
              ans=max(ans,cost)
            if 0<=ny<h and 0<=nx<w and not visited[ny][nx] and s[ny][nx]=='.':
              q.appendleft((ny,nx,cost+1))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
ans=-1
dx=[1,0,-1,0]
dy=[0,1,0,-1]
for sy in range(h):
  for sx in range(w):
    for gy in range(h):
      for gx in range(w):
        if sy==gy and sx==gx:
          continue
        if s[sy][sx]=='#' or s[gy][gx]=='#':
          continue
        q=deque()
        q.append((sy,sx,0))
        visited=[[False]*w for i in range(h)]
        visited[sy][sx]=True
        while q:
          y,x,cost=q.pop()
          visited[y][x]=True
          for i in range(4):
            ny,nx=y+dy[i],x+dx[i]
            if ny==gy and nx==gx:
              ans=max(ans,cost)
            if 0<=ny<h and 0<=nx<w and not visited[ny][nx] and s[ny][nx]=='.':
              q.appendleft((ny,nx,cost+1))
print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
    from collections import deque
    h, w = map(int, input().split())
    maze = [list(input()) for _ in range(h)]
    def mazesolver(x, y):
        visited = [[0]*w for _ in range(h)]
        mark = deque([[x, y]])
        times = [[0]*w for _ in range(h)]
        visited[x][y] = 1
        times[x][y] = 1
        xchange = [-1,0,1,0]
        ychange = [0,1,0,-1]
        while mark:
            tx, ty = mark.pop()
            visited[tx][ty] = 1
            a = times[tx][ty]
            for i in range (3):
                nx = tx + xchange[i]
                ny = ty + ychange[i]
                if 0 <= nx < h and 0 <= ny < w and visited[nx][ny] == 0 and maze[nx][ny] != "#":
                    mark.appendleft([nx,ny])
                    if times[nx][ny] == 0:
                        times[nx][ny] = a+1
        return max(max(times[i] for i in range(h)))
    ans = 0
    for l in range(h):
        for m in range(w):
            if maze[l][m] != "#":
                ans = max(ans, (mazesolver(l,m)))
    print(ans-1)
resolve()

Submission status: Wrong Answer

Solution code:
import queue
import copy

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

def movable(S_dist,px,py):
    if  0 <= px and px < H and 0 <= py and py < W:
        if S_dist[px][py] == INF:
            return True
    return False

def bfs(S,sx,sy):
    S_dist = [[INF for _ in range(W)] for _ in range(H)]
    #Sを直接いじれば訪問済みかどうか管理できるがそれだと毎回Sをリセットする必要がある
    #S_distのみ書き込みにすればS_distはローカルだからリセットが不要
    S_dist[sx][sy] = 0
    q = queue.Queue()
    q.put((sx,sy))
    vec = [(0,1),(0,-1),(1,0),(-1,0)]
    while not q.empty():
        p = q.get()
        ret = S_dist[p[0]][p[1]]
        for vx,vy in vec:
            if movable(S_dist,p[0]+vx,p[1]+vy):
                S_dist[p[0]+vx][p[1]+vy] = S_dist[p[0]][p[1]]+1
                q.put((p[0]+vx,p[1]+vy))
    return ret

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

Submission status: Wrong Answer

Solution code:
import copy
import sys
input = sys.stdin.readline
H,W= map(int,input().split())
maze = [list(input()) for _ in range(H)]
maze = [l[:len(maze[0])-1] for l in maze]
ans = 0
for i in range(H):
    for j in range(W):
        if maze[i][j] !="#":
            queue = [[i,j,0]]#最初の△
            tem_ans = 0
            while True:
                if len(queue) ==0:
                    tem_ans = point[2]
                    break
                point=queue.pop(0)#queueの先頭座標+何回移動したかの取得
                maze[point[0]][point[1]] = str(point[2]) 
                #print(np.array(maze))
                if point[0] > 0 and maze[point[0]-1][point[1]]!="#" and maze[point[0]-1][point[1]]!=str(point[2]-1):#上
                    queue.append([point[0]-1,point[1],point[2]+1])
                if point[0] < len(maze)-1 and maze[point[0]+1][point[1]]!="#" and maze[point[0]+1][point[1]]!=str(point[2]-1):#下
                    queue.append([point[0]+1,point[1],point[2]+1])
                if point[1] > 0 and maze[point[0]][point[1]-1]!="#" and maze[point[0]][point[1]-1]!=str(point[2]-1):#左
                    queue.append([point[0],point[1]-1,point[2]+1])
                if point[1] < len(maze[point[0]])-1 and maze[point[0]][point[1]+1]!="#" and maze[point[0]][point[1]+1]!=str(point[2]-1):#右
                    queue.append([point[0],point[1]+1,point[2]+1])
                
            ans = max(ans,tem_ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy
import sys
input = sys.stdin.readline
H,W= map(int,input().split())
input_maze = [list(input()) for _ in range(H)]
ans = 0
num_way = 0
for i in range(H):
    for j in range(W):
        if input_maze[i][j] == ".":
            num_way += 1
if num_way >= 350:
    print(num_way)
else:
    for i in range(H):
        for j in range(W):
            if input_maze[i][j] ==".":
                queue = [[i,j,0]]#最初の△
                maze = copy.deepcopy(input_maze)
                while True:
                    if len(queue) ==0:
                        tem_ans = point[2]
                        break
                    point=queue.pop(0)#queueの先頭座標+何回移動したかの取得
                    maze[point[0]][point[1]] = point[2] 
                    if point[0] > 0 and maze[point[0]-1][point[1]]==".":#上
                        queue.append([point[0]-1,point[1],point[2]+1])
                    if point[0] < len(maze)-1 and maze[point[0]+1][point[1]]==".":#下
                        queue.append([point[0]+1,point[1],point[2]+1])
                    if point[1] > 0 and maze[point[0]][point[1]-1]==".":#左
                        queue.append([point[0],point[1]-1,point[2]+1])
                    if point[1] < len(maze[point[0]])-1 and maze[point[0]][point[1]+1]=="." :#右
                        queue.append([point[0],point[1]+1,point[2]+1])
                    
                ans = max(ans,tem_ans)
    print(ans)

Submission status: Wrong Answer

Solution code:
print(10)

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from copy import deepcopy

t = time.time()
def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

def makemaze(H, W):
    maze = []
    for i in range(H):
        s = input()
        a = []
        for c in s:
            if c == ".":
                a.append(1000)
            if c == "#":
                a.append(9999)
        maze.append(a)
        #print(maze)
    return maze

def maxroot(maze, x, y, w, h):
    sn = 0
    maxx = 0
    if maze[y][x] == 9999:
        return 0

    maze = deepcopy(maze)
    maze[y][x] = 0
    que = deque()
    que.append((y, x))


    while que:
        xy = que.pop()
        y = xy[0]
        x = xy[1]
        if maze[y][x] == 9999:
            continue

        minlist = []
        if x > 0:
            minlist.append((y, x-1))
        if y > 0:
            minlist.append((y-1, x))
        if x < w-1:
            minlist.append((y, x+1))
        if y < h-1:
            minlist.append((y+1, x))

        dd = min(maze[y][x] for y, x in minlist) + 1
        dd = min(dd, maze[y][x])
        maze[y][x] = dd

        for y, x in minlist:
            if maze[y][x] != 9999 and maze[y][x] > dd+2:
                que.append((y, x))
                sn += 1
            #print(sn)

    return max([max(i) for i in maze if i != 9999])


def main():
    H, W = iip()
    maze = makemaze(H, W)

    maxx = 1
    for y in range(H):
        for x in range(W):
            maxx = max(maxx, maxroot(maze, x, y, W, H))
            pass

    print(maxx)
    #print(maxroot(maze, 0, 2, W, H))


main()

Submission status: Wrong Answer

Solution code:
import math
import time
from collections import deque
from copy import deepcopy

t = time.time()
def iip():
    ret = [int(i) for i in input().split()]
    if len(ret) == 1:
        return ret[0]
    return ret

def makemaze(H, W):
    maze = []
    for i in range(H):
        s = input()
        a = []
        for c in s:
            if c == ".":
                a.append(1000)
            if c == "#":
                a.append(9999)
        maze.append(a)
        #print(maze)
    return maze

def maxroot(maze, x, y, w, h):
    sn = 0
    maxx = 0
    if maze[y][x] == 9999:
        return 0

    maze = deepcopy(maze)
    maze[y][x] = 0
    que = deque()
    que.append((y, x))


    while que:
        xy = que.pop()
        y = xy[0]
        x = xy[1]
        if maze[y][x] == 9999:
            continue

        minlist = []
        if x > 0:
            minlist.append((y, x-1))
        if y > 0:
            minlist.append((y-1, x))
        if x < w-1:
            minlist.append((y, x+1))
        if y < h-1:
            minlist.append((y+1, x))

        dd = min(maze[y][x] for y, x in minlist) + 1
        dd = min(dd, maze[y][x])
        maze[y][x] = dd
        maxx = max(dd, maxx)

        for y, x in minlist:
            if maze[y][x] != 9999 and maze[y][x] > dd+2:
                que.append((y, x))
                sn += 1
            print(sn)

        #if dd == 39:
        #    for i in range(20):
        #        print(maze[i])
    return maxx


def main():
    H, W = iip()
    maze = makemaze(H, W)

    maxx = 1
    for y in range(H):
        for x in range(W):
            maxx = max(maxx, maxroot(maze, x, y, W, H))
            pass

    print(maxx)
    #print(maxroot(maze, 0, 2, W, H))


main()