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:
from collections import deque
h,w = map(int,input().split())
s = [0]*(h+2)
s[0]  = ["#"]*(w+2)
s[-1] = ["#"]*(w+2)
for i in range(h):
    s[i+1] = ["#"] + list(input()) + ["#"]
ans = 0
for i in range(h+2):
    for j in range(w+2):
        if s[i][j] == "#":
            pass
        else:
            xs,ys = j,i
            print(xs,ys)
            qx = deque([])
            qx.append(xs)
            qy = deque([])
            qy.append(ys)
            reach = [[0 for j in range(w+2)] for i in range(h+2)]
            reach[ys][xs] = 1
            while qx:
                x = qx.popleft()
                y = qy.popleft()

                if s[y-1][x] == "." and reach[y-1][x] == 0:
                    qx.append(x)
                    qy.append(y-1)
                    reach[y-1][x] = reach[y][x] + 1
                if s[y+1][x] == "." and reach[y+1][x] == 0:
                    qx.append(x)
                    qy.append(y+1)
                    reach[y+1][x] = reach[y][x] + 1
                if s[y][x-1] == "." and reach[y][x-1] == 0:
                    qx.append(x-1)
                    qy.append(y)
                    reach[y][x-1] = reach[y][x] + 1
                if s[y][x+1] == "." and reach[y][x+1] == 0:
                    qx.append(x+1)
                    qy.append(y)
                    reach[y][x+1] = reach[y][x] + 1
            m = 0
            for i in range(h+2):
                m = max(m,max(reach[i][:])-1)
            ans = max(ans,m)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int,input().split())
s = [0]*(h+2)
s[0]  = ["#"]*(w+2)
s[-1] = ["#"]*(w+2)
for i in range(h):
    s[i+1] = ["#"] + list(input()) + ["#"]
ans = 0
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] == "#":
            pass
        else:
            xs,ys = j,i
            qx = deque([])
            qx.append(xs)
            qy = deque([])
            qy.append(ys)
            reach = [[0 for j in range(w+2)] for i in range(h+2)]
            reach[ys][xs] = 1
            while qx:
                x = qx.popleft()
                y = qy.popleft()
                
                if s[y-1][x] == "." and reach[y-1][x] == 0:
                    qx.append(x)
                    qy.append(y-1)
                    reach[y-1][x] = reach[y][x] + 1
                if s[y+1][x] == "." and reach[y+1][x] == 0:
                    qx.append(x)
                    qy.append(y+1)
                    reach[y+1][x] = reach[y][x] + 1
                if s[y][x-1] == "." and reach[y][x-1] == 0:
                    qx.append(x-1)
                    qy.append(y)
                    reach[y][x-1] = reach[y][x] + 1
                if s[y][x+1] == "." and reach[y][x+1] == 0:
                    qx.append(x+1)
                    qy.append(y)
                    reach[y][x+1] = reach[y][x] + 1
            m = 0
            for i in range(h):
                m = max(m,max(reach[i][:])-1)
            ans = max(ans,m)
print(ans)

Submission status: Wrong Answer

Solution code:
from scipy.sparse import*
_,*s=open(0)
M=999
*g,=eval('[0]*M,'*M)
i=0
for t in s:
 i+=1;j=0
 for u in t:k=i*20+j;g[k][k+1]=u>'#'<t[j+1];g[k][k+20]=u>'#'<(s+['#'*M])[i][j];j+=1
a=csgraph.johnson(csr_matrix(g),20)
print(int(max(a[a<M])))

Submission status: Wrong Answer

Solution code:
#D
import queue
H,W=map(int,input().split())
P=[0 for i in range(H)]
for i in range(H):
    P[i]=str(input())
ans=0
for i in range(H*W):
    s=(i//W,i%W)
    if P[s[0]][s[1]]=="#":
        continue
    st=queue.Queue()
    st.put(s)
    log=[[-1 for i in range(W)] for j in range(H)]
    log[s[0]][s[1]]=0
    while not st.empty():
        x,y=st.get()
        for z,w in [(y+1,x),(y-1,x),(y,x+1),(y,x-1)]:
            if 0>z or 0>w:
                continue
            elif not (z<H and w<W):
                continue
            elif P[z][w]=="." and log[z][w]==-1:
                st.put((z,w))
                log[z][w]=log[x][y]+1
                ans=max(ans,log[z][w])
print(ans)

Submission status: Wrong Answer

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

M = []
for i in range(H):
    M.append(list(input()))

from collections import deque

out = 0
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

for sx in range(0,W):
    for gx in range(0,W):
        for sy in range(0,H):
            for gy in range(0,H):
                if (sx==gx)and(sy==gy):
                    continue
                d = [[10**4] * W for i in range(H)]
                que = deque([])
                que.append((sy, sx))
                d[sy][sx] = 0
                while que:
                    p = que.popleft()
                    if p[0] == gy and p[1] == gx:
                        break
                    for i in range(4):
                        ny = p[0] + dy[i]
                        nx = p[1] + dx[i]
                        if (0<=nx<W)and(0<=ny<H)and(M[ny][nx]!="#")and(d[ny][nx]==10**4):
                            que.append((ny, nx))
                            d[ny][nx] = d[p[0]][p[1]] + 1
                if out < d[gy][gx] and M[gy][gx]!="#":
                    out=d[gy][gx]
print(out)

Submission status: Wrong Answer

Solution code:
from collections import *

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

ans = 0
INF = float('inf')

for i in range(H):
    for j in range(W):
        if S[i][j] == '#': continue

        dist = [[INF for i in range(W)] for j in range(H)]
        visited = [[False for i in range(W)] for j in range(H)]

        q = deque()
        q.append((i, j, 0))
        
        while q:
            i, j, c = q.popleft()

            if visited[i][j]: continue
            visited[i][j] = True
            dist[i][j] = c
            ans = max(ans, c)
            for a, b in [(1,0), (-1,0), (0,1), (0,-1)]:
                x = i+a
                y = j+b

                if 0 <= x < H and 0 <= y < W and S[x][y] == '.':
                    q.append((x, y, c+1))

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

H,W = map(int, input().split())
s = ["#"*(W+2)]+["#"+input()+"#" for i in range(H)]+["#"*(W+2)]

def bfs(sh,sw):
  if s[sh][sw] == "#":
    return 0
  queue = deque([(sh,sw)]) #スタート地点をdequeに入れる
  dist = [[-1] * (W+1) for i in range(H+1)] #距離をメモするリストを作成
  dist[sh][sw] = 0 #スタート地点の距離を0にする
  while queue:
    h,w = queue.popleft() #queueの左から位置情報を取り出す
    d = dist[h][w] #現在のマスの数字を保持(移動距離)
    print("dist", dist)
    for i in [h-1,h+1]: #x軸に前後する
      if s[i][w] == "." and dist[i][w] == -1: #行ける、かつ未踏破のマス
        dist[i][w] = d + 1 #移動距離をメモ
        queue.append((i,w)) #移動先のマスをqueueに格納
    for j in [w-1,w+1]: #y軸を行ったり来たり
      if s[h][j] == "." and dist[h][j] == -1:
        dist[h][j] = d + 1
        queue.append((h,j))
          
  max_dist = 0
  for i in dist:
    max_dist = max(max_dist,max(i)) #移動距離が多いマスを抽出
  return max_dist
      

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

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

H, W = MAP()
S = [input() for _ in range(H)]
dy = (1, 0, -1, 0)
dx = (0, -1, 0, 1)

def diff_max(start_y, start_x):
	check = [[-1]*W for _ in range(H)]
	check[start_y][start_x] = 0
	q = deque([(start_y, start_x)])

	while q:
		y, x = q.popleft()
		for i in range(4):
			ny = y + dy[i]
			nx = x + dx[i]
			if 0 <= ny < H and 0 <= nx < W:
				if check[ny][nx] == -1 and S[ny][nx] == ".":
					check[ny][nx] = check[y][x] + 1
					q.append((ny, nx))

	return max([max(x) for x in check])

ans = -INF
for i in range(H):
	for j in range(W):
		ans = max(ans, diff_max(i, j))

print(ans)

Submission status: Wrong Answer

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

def bfs(field, sx, sy, seen):
    queue = deque([(sx, sy)])
    seen[sx][sy] += 1
    
    while queue:
        x, y = queue.popleft()

        for dx, dy in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
            nx = x + dx
            ny = y + dy
            
            if seen[nx][ny] == -1 and field[nx][ny] != "#":
                seen[nx][ny] = seen[x][y] + 1
                queue.append((nx, ny))
        
    return seen[x][y]


def main():
    H,W = map(int, readline().split())
    c = ["#" * (W + 2)]
    for _ in range(H):
        c.append("#" + readline().strip() + "#")
    c.append("#" * (W + 2))

    ans = 0
    for sx in range(1,H):
        for sy in range(1,W+1):
            if c[sx][sy] == ".":
                seen = [[-1] * (W + 2) for i in range(H+2)]
                dist = bfs(c, sx, sy, seen)
                ans = max(ans, dist)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

h,w = map(int, input().split())
grid = []
for i in range(h):
    s = list(input().rstrip())
    grid.append(s)
dydx = [[0, 1], [1,0], [-1, 0], [0, -1]]

def find_longest_path(xy):
    visited = [[0]*w for _ in range(h)]
    dist = [[0]*w for _ in range(h)]
    que = deque()
    que.append(xy)
    while que:
        v = que.popleft()
        x,y = v[0],v[1]
        visited[y][x]=1
        for i in dydx:
            dx = i[0]
            dy = i[1]
            if ((x+dx<w and y+dy<h) and ((x+dx)>-1)and(y+dy)>-1) and not visited[y+dy][x+dx]:
                if(grid[y+dy][x+dx]=="."):
                    que.appendleft([x+dx, y+dy])
                    dist[y+dy][x+dx]=dist[y][x]+1
                    u,k = x+dx, y+dy
                    r = dist[y+dy][x+dx]



    return r, [u,k]

def main():
    for i in range(h):
        for j in range(w):
            if grid[i][j]==".":
                x,y= j,i
    r,v=find_longest_path([x,y])
    r,res = find_longest_path(v)
    print(r)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

h,w = map(int, input().split())
grid = []
for i in range(h):
    s = list(input().rstrip())
    grid.append(s)
dxdy = [[0, 1], [1,0], [-1, 0], [0, -1]]
def max1(a,b):
    if a[0]>b[0]:
        return a
    else:
        return b
maxdist = 0
for i in range(h):
    for j in range(w):
        if grid[i][j]==".":
            dist = [[0]*w for _ in range(h)]
            visited = [[0]*w for _ in range(h)]
            que=deque()
            que.append([j,i])
            while que:
                v=que.popleft()
                x = v[0]
                y = v[1]
                visited[y][x]=1

                for k in dxdy:
                    dx = k[0]
                    dy = k[1]
                    if((x+dx<w and y+dy<h) and ((x+dx)>-1)and(y+dy)>-1) and not visited[y+dy][x+dx]:
                        maxdist = max(dist[y+dy][x+dx], maxdist)
                        dist[y+dy][x+dx]=dist[y][x]+1
                        que.append([x+dx, y+dy])
print(maxdist)

Submission status: Wrong Answer

Solution code:
from collections import deque
h,w = map(int, input().split())
grid = []
for i in range(h):
    s = list(input().rstrip())
    grid.append(s)
dxdy = [[0, 1], [1,0], [-1, 0], [0, -1]]
visited = [[0]*w for _ in range(h)]
dist = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if grid[i][j]==".":
            y = i
            x = j
            break

que=deque()
que.append([x,y])
while que:
    v=que.popleft()
    x = v[0]
    y = v[1]
    visited[y][x]=1
    for i in dxdy:
        dx = i[0]
        dy = i[1]
        if((x+dx<w and y+dy<h) and ((x+dx)>-1)and(y+dy)>-1) and not visited[y+dy][x+dx]:
            dist[y+dy][x+dx]=dist[y][x]+1
            que.append([x+dx, y+dy])
max_dist = [0, []]
def max1(a,b):
    if a[0]>b[0]:
        return a
    else:
        return b
for i in range(h):
    for j in range(w):
        max_dist = max1(max_dist, [dist[i][j], i,j])
visited = [[0]*w for _ in range(h)]
dist = [[0]*w for _ in range(h)]

que2 = deque()
que2.append([max_dist[2], max_dist[1]])
while que2:
    v=que2.popleft()
    x = v[0]
    y = v[1]
    visited[y][x]=1
    for i in dxdy:
        dx = i[0]
        dy = i[1]
        if((x+dx<w and y+dy<h) and ((x+dx)>-1)and(y+dy)>-1) and not visited[y+dy][x+dx] and grid[y+dy][x+dx]==".":
            dist[y+dy][x+dx]=dist[y][x]+1
            que2.append([x+dx, y+dy])
for i in range(h):
    for j in range(w):
        max_dist = max1(max_dist, [dist[i][j], i,j])
print(max_dist[0])

Submission status: Wrong Answer

Solution code:
from collections import deque
que=deque()
h,w = map(int, input().split())

grid = []

for i in range(h):
    s = list(input().rstrip())
    grid.append(s)
dxdy = [[0, 1], [1,0], [-1, 0], [0, -1]]
visited = [[0]*w for _ in range(h)]
dist = [[0]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if grid[i][j]==".":
            y = i
            x = j
            break



dist[y][x] = 0
max_dist = [0, []]
def max1(a,b):
    if a[0]>b[0]:
        return a
    else:
        return b
def dfs(x, y):
    visited[y][x]=1
    for dydx in dxdy:
        if (x+dydx[0]<w and y+dydx[1]<h) and (x+dydx[0]>-1 and y+dydx[1]>-1) and not visited[y+dydx[1]][x+dydx[0]]:
            if grid[y+dydx[1]][x+dydx[0]]==".":
                dist[y+dydx[1]][x+dydx[0]] = dist[y][x]+1
                dfs(x+dydx[0], y+dydx[1])
dfs(x, y)

for i in range(h):
    for j in range(w):
        max_dist = max1(max_dist, [dist[i][j], i,j])

visited = [[0]*w for _ in range(h)]
dist = [[0]*w for _ in range(h)]
dfs(max_dist[2],max_dist[1])


for i in range(h):
    for j in range(w):
        max_dist = max1(max_dist, [dist[i][j], i,j])
print(max_dist[0])

Submission status: Wrong Answer

Solution code:
h,w=[int(x) for x in input().rstrip().split()]
l=[list(input()) for i in range(h)]
dist=[[0]*w for i in range(h)]
move=[[1,0],[-1,0],[0,-1],[0,1]]
def bfs():
  stack=[[0,h-1]]
  done=[[False]*w for i in range(h)]
  done[h-1][0]=True
  while(stack):
    nx,ny=stack.pop(0)
    for dx,dy in move:
      mx=nx+dx
      my=ny+dy
      if not(0<=mx<=w-1) or not(0<=my<=h-1) or done[my][mx]==True or l[my][mx]=="#":
        continue
      done[my][mx]=True
      dist[my][mx]=dist[ny][nx]+1
      stack.append([mx,my])
bfs()
print(dist[0][w-1])

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    maize = []
    for i in range(h):
        s = input()
        maize.append(s)
    ans = 0
    for i in range(h):
        for j in range(w):
            que = [[i,j]]
            if maize[i][j] == '.':
                dis = [[-1 for i in range(w)] for i in range(h)]
                dis[i][j] = 0
                while len(que)>0:
                    q = que.pop(0)
                    if q[0]>0:
                        n = [q[0]-1,q[1]]
                        if maize[n[0]][n[1]]=='.':
                            if dis[n[0]][n[1]]==-1:
                                dis[n[0]][n[1]] = dis[q[0]][q[1]]+1
                                que.append(n)
                    if q[0] < h-1:
                        n = [q[0]+1,q[1]]
                        if maize[n[0]][n[1]]=='.':
                            if dis[n[0]][n[1]]==-1:
                                dis[n[0]][n[1]] = dis[q[0]][q[1]]+1
                                que.append(n)
                    if q[1] > 0:
                        n = [q[0],q[1]-1]
                        if maize[n[0]][n[1]]=='.':
                            if dis[n[0]][n[1]]==-1:
                                dis[n[0]][n[1]] = dis[q[0]][q[1]]+1
                                que.append(n)
                    if q[1] < w-1:
                        n = [q[0],q[1]+1]
                        if maize[n[0]][n[1]]=='.':
                            if dis[n[0]][n[1]]==-1:
                                dis[n[0]][n[1]] = dis[q[0]][q[1]]+1
                                que.append(n)
                d = []
                for i in range(h):
                    d+=dis[i]
                if max(d)>ans:
                    ans = max(d)
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h,w = map(int,input().split())
    maize = []
    for i in range(h):
        s = input()
        maize.append(s)
    ans = 0
    for i in range(h):
        for j in range(w):
            que = [[i,j]]
            if maize[i][j] == '.':
                dis = [[-1 for i in range(w)] for i in range(h)]
                dis[i][j] = 0
                while len(que)>0:
                    q = que.pop(0)
                    if q[0]>0:
                        n = [q[0]-1,q[1]]
                        if maize[n[0]][n[1]]=='.':
                            if dis[n[0]][n[1]]==-1:
                                dis[n[0]][n[1]] = dis[q[0]][q[1]]+1
                                que.append(n)
                    if q[0] < h-1:
                        n = [q[0]+1,q[1]]
                        if maize[n[0]][n[1]]=='.':
                            if dis[n[0]][n[1]]==-1:
                                dis[n[0]][n[1]] = dis[q[0]][q[1]]+1
                                que.append(n)
                    if q[1] > 0:
                        n = [q[0],q[1]-1]
                        if maize[n[0]][n[1]]=='.':
                            if dis[n[0]][n[1]]==-1:
                                dis[n[0]][n[1]] = dis[q[0]][q[1]]+1
                                que.append(n)
                    if q[1] < w-1:
                        n = [q[0],q[1]+1]
                        if maize[n[0]][n[1]]=='.':
                            if dis[n[0]][n[1]]==-1:
                                dis[n[0]][n[1]] = dis[q[0]][q[1]]+1
                                que.append(n)
                if max(max(dis))>ans:
                    ans = max(max(dis))
    print(ans)

    

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
def main():
    H,W = map(int, input().split())
    S = [[0]*W for _ in range(H)]
    for i in range(H):
        # 1は道,0は壁
        tmp = list(map(lambda x:1 if x=='.' else 0, input()))
        S[i] = tmp
    # BFS
    ans = 0
    for i in range(H):
        for j in range(W):
            # (i,j)がスタート地点
            dist = [[-1]*W for _ in range(H)]
            dist[i][j] = 0
            que = deque([(i,j)])
            while que:
                si,sj = que.pop()
                for r,b in [(1,0),(0,1),(-1,0),(0,-1)]: # 右,下,左,上(移動方向)
                    gi = si+r
                    gj = sj+b
                    # 枠外
                    if gi<0 or (H-1)<gi or gj<0 or (W-1)<gj:
                        continue
                    # 壁
                    if S[gi][gj] == 0:
                        continue
                    # 探索済み
                    if dist[gi][gj]!=-1:
                        continue
                    dist[gi][gj] = dist[si][sj]+1
                    que.appendleft((gi,gj))
            ans = max(ans,max([max(d) for d in dist]))
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
from collections import deque
import sys

M = []
H = 0
W = 0
qx = deque()
qy = deque()
qc = deque()

def main():
    global M,H,W
    H,W = map(int, input().split())  
    for i in range(H):
        M.append(list(input()))

    startX = 0
    startY = 0
    for i in range(H):
        for j in range(W):
            if M[i][j]=='.':
                startX=j
                startY=i
        

    Mtemp = deepcopy(M)

    Mtemp[startY][startX] = '#'
    qx.append(startX)
    qy.append(startY)
    qc.append(0)
    cnt = 0
    x = 0 
    y = 0

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[y][x-1] == '.':
                Mtemp[y][x-1] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[y][x+1] == '.':
                Mtemp[y][x+1] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[y-1][x] == '.':
                Mtemp[y-1][x] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[y+1][x] == '.':
                Mtemp[y+1][x] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)

    Mtemp = deepcopy(M)

    Mtemp[y][x] = '#'
    qx.append(x)
    qy.append(y)
    qc.append(0)
    cnt = 0

    while len(qx) !=0:
        x =  qx.popleft()
        y =  qy.popleft()
        cnt = qc.popleft()

        if x-1>=0:
            if Mtemp[y][x-1] == '.':
                Mtemp[y][x-1] = '#'
                qx.append(x-1)
                qy.append(y)
                qc.append(cnt+1)
        if x+1<W:
            if Mtemp[y][x+1] == '.':
                Mtemp[y][x+1] = '#'
                qx.append(x+1)
                qy.append(y)
                qc.append(cnt+1)
        if y-1>=0:
            if Mtemp[y-1][x] == '.':
                Mtemp[y-1][x] = '#'
                qx.append(x)
                qy.append(y-1)
                qc.append(cnt+1)
        if y+1<H:
            if Mtemp[y+1][x] == '.':
                Mtemp[y+1][x] = '#'
                qx.append(x)
                qy.append(y+1)
                qc.append(cnt+1)


    print(cnt)




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import copy
sys.setrecursionlimit(9**9)
H,W=map(int,input().split())
M=[list(input()) for _ in range(H)]
N=copy.deepcopy(M)
m=x=y=0
def d(c,P):
    global m,x,y
    Q=[]
    for p in P:
        h=p[0]
        w=p[1]
        if h!=H-1 and M[h+1][w]=='.':
            M[h+1][w]='#'
            Q.append([h+1,w])
        if h!=0 and M[h-1][w]=='.':
            M[h-1][w]='#'
            Q.append([h-1,w])
        if w!=W-1 and M[h][w+1]=='.':
            M[h][w+1]='#'
            Q.append([h,w+1])
        if w!=0 and M[h][w-1]=='.':
            M[h][w-1]='#'
            Q.append([h,w-1])
    if len(Q)!=0:
      c+=1
      return d(c,Q)
    else:
        m=c
        x=P[0][0]
        y=P[0][1]
f=False
for i in range(H):
    for j in range(W):
        if M[i][j]=='.':
            M[i][j]='#'
            d(0,[[i,j]])
            f=True
            break
    if f:break
M=N
m=0
M[x][y]='#'
d(0,[[x,y]])
print(m)

Submission status: Wrong Answer

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

state = [[-1]*W for _ in range(H)]
start_list = []
for i in range(H):
    for j in range(W):
        if s[i][j] == '.':
            start_list.append((i,j))
            state[i][j] = 0

def dfs(black_cells,state):
    d = 0
    dist = [[0]*W for _ in range(H)]
    while black_cells:
        h, w =  black_cells.popleft()
        #print(h,w)
        d = dist[h][w]
        for dy, dx in ((1,0), (0,1), (-1,0), (0,-1)):
            new_h = h+dy
            new_w = w+dx
            if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
                continue
            if state[new_h][new_w] == 0 and dist[new_h][new_w] == 0:
                dist[new_h][new_w] = d+1
                black_cells.append((new_h, new_w))
    return np.array(dist).max()

ans = 0
for start in start_list:
    black_cells = deque()
    black_cells.append((start))
    ans = max(ans, dfs(black_cells,state))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = [int(_) for _ in input().split()]
e = "#" * (W + 2)
maze = [e]
for i in range(H):
    s = input()
    maze.append("#" + s + "#")
maze.append(e)
max_count = 0
for h in range(H):
    for w in range(W):
        visited = [[False for i in range(W+2)] for j in range(H+2)]
        if maze[h+1][w+1] == '#':
            continue
        q = deque([[w+1, h+1, 0]])
        def search(x, y, c):
            if maze[y][x] == '.' and not visited[y][x]:
                q.append([x, y, c+1])
                visited[y][x] = True
        count = 0
        while len(q) != 0:
            p = q.popleft()
            search(p[0]+1, p[1], p[2])
            search(p[0]-1, p[1], p[2])
            search(p[0], p[1]+1, p[2])
            search(p[0], p[1]-1, p[2])
            count = p[2]
        max_count = max(max_count, count)
print(max_count)

Submission status: Wrong Answer

Solution code:
from collections import deque
H, W = [int(_) for _ in input().split()]
e = "#" * (W + 2)
maze = [e]
for i in range(H):
    s = input()
    maze.append("#" + s + "#")
maze.append(e)
max_count = 0
for h in range(H):
    for w in range(W):
        visited = [[False for i in range(W+2)] for j in range(H+2)]
        q = deque([[w+1, h+1, 0]])
        def search(x, y, c):
            if maze[y][x] == '.' and not visited[y][x]:
                q.append([x, y, c+1])
                visited[y][x] = True
        count = 0
        while len(q) != 0:
            p = q.popleft()
            search(p[0]+1, p[1], p[2])
            search(p[0]-1, p[1], p[2])
            search(p[0], p[1]+1, p[2])
            search(p[0], p[1]-1, p[2])
            count = p[2]
        max_count = max(max_count, count)
print(max_count)

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = map(int, input().split())
s = []
for _ in range(H):
    s.append(input())
dh = [0, 0, 1, -1]
dw = [1, -1, 0, 0]
ans = 0
for i in range(H):
    for j in range(W):
        if s[i][j] == "#":
            continue
        dismax = 0
        dist = [[-1 for j in range(W)] for i in range(H)]
        queue = deque([[i, j]])
        dist[i][j] = 0
        while len(queue) != 0:
            now = queue.popleft()
            h = now[0]
            w = now[1]
            for k in range(4):
                nh = h + dh[k]
                nw = w + dw[k]
                if nh < 0 or nw < 0 or nh >= H or nw >= W:
                    continue
                if dist[nh][nw] != -1 or s[nh][nw] == "#":
                    continue
                else:
                    dist[nh][nw] = dist[h][w] + 1
                    dismax = max(dist[nh][nw], dismax)
                    queue.append([nh, nw])
        print(dist)
        ans = max(ans, dismax)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

H, W = map(int, input().split())
s = []
for _ in range(H):
    s.append(input())
dh = [0, 0, 1, -1]
dw = [1, -1, 0, 0]
ans = 0
for i in range(H):
    for j in range(W):
        dismax = 0
        dist = [[-1 for j in range(W)] for i in range(H)]
        queue = deque([[i, j]])
        dist[i][j] = 0
        while len(queue) != 0:
            now = queue.popleft()
            h = now[0]
            w = now[1]
            for k in range(4):
                nh = h + dh[k]
                nw = w + dw[k]
                if nh < 0 or nw < 0 or nh >= H or nw >= W:
                    continue
                if dist[nh][nw] != -1 or s[nh][nw] == "#":
                    continue
                else:
                    dist[nh][nw] = dist[h][w] + 1
                    dismax = max(dist[nh][nw], dismax)
                    queue.append([nh, nw])
        ans = max(ans, dismax)
print(ans)

Submission status: Wrong Answer

Solution code:
H,W=list(map(int,input().split()))
l=[]
import copy
for i in range(H):
   a=list(input())
   a=[-1 if i == "#" else 0 for i in a]
   l.append(a)
from collections import deque
def bfs(x,y,s):
   visited=copy.deepcopy(s)
   visited[x][y] = 1
   que=deque([[x,y]])
   while que:
      node=que.popleft()
      S=visited[node[0]][node[1]]
      try:
         if not visited[node[0]+1][node[1]]:
            visited[node[0]+1][node[1]]=S+1
            que.append([node[0]+1,node[1]])
      except IndexError:
         pass
      try:
         if not visited[node[0]][node[1]+1]:
            visited[node[0]][node[1]+1]=S+1
            que.append([node[0],node[1]+1])
      except IndexError:
         pass
      try:
         if not visited[node[0]][node[1]-1]:
            if node[1]-1 != -1:
               visited[node[0]][node[1]-1]=S+1
               que.append([node[0],node[1]-1])
      except IndexError:
         pass
      try:
         if not visited[node[0]-1][node[1]]:
            if node[0]-1 != -1:
               visited[node[0]-1][node[1]]=S+1
               que.append([node[0]-1,node[1]])
      except IndexError:
         pass
   visited=sum(visited,[])
   return max(visited)-1
ans=0
for i in range(H):
   for j in range(W):
      ans=max(ans,bfs(i,j,l))
print(ans)