AcWing 1101. 献给阿尔吉侬的花束
原题链接
简单
作者:
二十六
,
2025-04-06 01:11:04
· 中国香港
,
所有人可见
,
阅读 1
move = [[0, 1], [1, 0], [-1, 0], [0, -1]]
T = int(input())
def bfs(n, m):
for i in range(n):
for j in range(m):
if grids[i][j] == 'S':
start = [i, j]
grids[i][j] == '#'
break
queue = [start]
hh, tt = 0, 0
ret = 0
while hh <= tt:
ret += 1
for _ in range(tt-hh+1):
x, y = queue[hh]; hh += 1
for i, j in move:
nx, ny = x + i, y + j
if 0<= nx < n and 0<= ny < m and grids[nx][ny] != '#':
if grids[nx][ny] == 'E': return ret
queue.append([nx, ny]); tt += 1
grids[nx][ny] = '#'
return 0
while T:
T -= 1
n, m = map(int, input().split())
grids = []
for _ in range(n):
grids.append(list(input()))
res = bfs(n, m)
if res: print(res)
else: print('oop!')