-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest3.py
37 lines (35 loc) · 1.01 KB
/
test3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def core(matrix,m,n):
start = []
end = []
for i in range(m):
for j in range(n):
if matrix[i][j] == 'S':
start = [i, j]
if matrix[i][j] == 'E':
end = [i, j]
if not start or not end:
return False
queue = [start]
visited = set()
visited.add(tuple(start))
while queue:
if end in queue:
return True
x, y = queue.pop(0)
for ix, iy in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]:
if 0 <= ix < m and 0 <= iy < n and (ix,iy) not in visited and matrix[ix][iy] in '.E':
queue.append([ix, iy])
visited.add((ix,iy))
return False
if __name__ == '__main__':
T = int(input())
for _ in range(T):
m, n = list(map(int, input().split()))
matrix = []
for i in range(m):
matrix.append(list(input().strip()))
res = core(matrix,m,n)
if res:
print('YES')
else:
print('NO')