从给定起点(一定为’.’),按照给定的若干跳跃(可以跨过障碍,但不可以落在’x’上),到达任意一个’.'的最小步骤次数集合中,选择一个最大的!
while True:
try:
n , m = map(int,input().split())
matrix = []
for i in range(n):
te = list(map(str,input()))
matrix.append(te)
begin_x,begin_y = map(int,input().split())
direction_num = int(input())
direction = []
for i in range(direction_num):
direction.append(list(map(int,input().split())))
visited = set()
visited.add((begin_x,begin_y))
stack = [(begin_x,begin_y,0)]
re = -1
while stack:
(x,y,step) = stack.pop(0)
re = max(re,step)
for [dx,dy] in direction:
temp_x = x+dx
temp_y = y+dy
if 0<= temp_x <= n-1 and 0<= temp_y <= m-1 and (temp_x,temp_y) not in visited and matrix[temp_x][temp_y] == '.':
visited.add((temp_x,temp_y))
stack.append((temp_x,temp_y,step+1))
count = 0
for i in range(n):
for j in range(m):
if matrix[i][j] == '.':
count+=1
if len(visited) == count:
print(re)
else:
print(-1)
except:
break