-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51.labyrinth_stack.py
44 lines (40 loc) · 1.14 KB
/
51.labyrinth_stack.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
38
39
40
41
42
43
44
import sys
sys.stdin = open('input.txt','r')
def safe(x,y):
if 0<=x<N and 0<=y<N:
return True
else:
return False
dx = [-1,0,1,0] #상우하좌
dy = [0,1,0,-1]
T = int(input())
for testcase in range(1,T+1):
N = int(input())
arr = [list(map(int, input())) for a in range(N)]
startx = 0
starty = 0
for x in range(N):
for y in range(N):
if arr[x][y] == 2:
startx = x
starty = y
break
stack = []
stack.append((startx,starty))
result = 0
while stack:
t = stack.pop()
if arr[t[0]][t[1]]== 3:
result = 1
break
arr[t[0]][t[1]] = 1
for direction in range(4):
pathx = t[0]+dx[direction]
pathy = t[1]+dy[direction]
if safe(pathx,pathy) :
if arr[pathx][pathy] in [0,3]:
future = (pathx,pathy)
stack.append(future)
if arr[future[0]][future[1]] == 3:
break
print('#{}'.format(testcase),result)