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
|
dx = [0,1, 0, -1]
dy = [1,0, -1, 0 ]
def IsSafe(x, y):
if 0<= x < 16 and 0 <= y < 16:
return True
def DFS(x, y):
global result
visited[x][y] = 1
if arr[x][y] == 3:
result = 1
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if IsSafe(nx, ny) and visited[nx][ny] == 0 and arr[nx][ny] != 1:
DFS(nx, ny)
def Find_Start():
for i in range(16):
for j in range(16):
if arr[i][j] == 2:
return i, j
for T in range(10):
t = input()
arr = [list(map(int, input())) for _ in range(16)]
visited = [[0 for _ in range(16)] for _ in range(16)]
x, y = Find_Start()
result = 0
DFS(x, y)
print(f'#{T+1} {result}')
# 재귀를 사용할 시 return은 불편한 점이 많다.
# 하여 특정 조건에서의 값이 필요한 경우.
# 변수를 설정해서 그 값을 바꾸는 편이 간편하다.
# 다만 런타임에 조심해야 한다.
|