AcWing 1101. 献给阿尔吉侬的花束(python3)
原题链接
简单
作者:
种太阳_2
,
2021-04-12 12:55:18
,
所有人可见
,
阅读 339
N = 210
n = int(input())
def bfs(start, end):
q = []
q.append(start)
dist = [[-1] * N for i in range(N)]
dist[start[0]][start[1]] = 0
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
while len(q):
t = q[0]
q.pop(0)
for i in range(4):
x = t[0] + dx[i]
y = t[1] + dy[i]
if x < 0 or x >=r or y < 0 or y >= c:
continue
if g[x][y] == '#':
continue
if dist[x][y] != -1:
continue
dist[x][y] = dist[t[0]][t[1]] + 1
if end == [x, y]:
return dist[x][y]
q.append([x, y])
return -1
while n:
r, c = map(int, input().split())
g = [[0] for i in range(r)]
for i in range(r):
g[i] = list(map(str, input()))
for i in range(r):
for j in range(c):
if g[i][j] == 'S':
start = [i, j]
elif g[i][j] == 'E':
end = [i, j]
distance = bfs(start, end)
if distance == -1:
print('oop!')
else:
print(distance)
n -= 1