# acwing 1011 献给阿尔吉侬的花束 https://www.acwing.com/problem/content/description/1103/
# https://www.acwing.com/solution/content/9093/
# 这题虽然还好,但是debug了有一会儿,刚开始是没用函数写的,一直没找出来
# 还是用函数写简单些,以后尽量用函数写吧
from collections import deque
N = 200
t = int(input())
a = [[0] * N for _ in range(N)]
dxy = [(1, 0), (0, 1), (-1, 0), (0, -1)]
def dfs(start):
global r, c
q = deque()
bo = [[0] * N for _ in range(N)]
dis = [[0] * N for _ in range(N)]
q.append(start)
bo[start[0]][start[1]] = 1
while q:
tmp = q.popleft()
for i in dxy:
x, y = tmp[0] + i[0], tmp[1] + i[1]
if x >= 0 and y >= 0 and x < r and y < c and a[x][y] != '#' and bo[x][y] == 0:
q.append((x, y))
dis[x][y] = dis[tmp[0]][tmp[1]] + 1
bo[x][y] = 1
if (x, y) == end:
ans = dis[x][y]
return ans
return 0
for _ in range(t):
r, c = map(int, input().split())
for i in range(r):
j = 0
for tmp in input():
a[i][j] = tmp
if tmp == 'S':
start = (i, j)
if tmp == 'E':
end = (i, j)
j += 1
ans = dfs(start)
if ans != 0:
print(ans)
else:
print('oop!')
acwing 1011 献给阿尔吉侬的花束
最新推荐文章于 2024-09-15 22:31:42 发布