File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ N = int (input ())
2+
3+ # 가로, 세로, 대각선 순
4+ dx = [0 , 1 , 1 ]
5+ dy = [1 , 0 , 1 ]
6+
7+
8+ def dfs (x , y , dir ):
9+ # 이미 방문한 상태라면 결과를 반환
10+ if memo [x ][y ][dir ] != - 1 :
11+ return memo [x ][y ][dir ]
12+
13+ # 종료 조건: 목표에 도달하면 경로 1개 추가
14+ if x == N - 1 and y == N - 1 :
15+ return 1
16+
17+ # 경로 수
18+ paths = 0
19+
20+ for dir_idx in range (3 ):
21+ # 현재 방향에서 이동 불가능한 방향 건너뛰기
22+ if (dir == 0 and dir_idx == 1 ) or (dir == 1 and dir_idx == 0 ):
23+ continue
24+
25+ newX = x + dx [dir_idx ]
26+ newY = y + dy [dir_idx ]
27+
28+ # 이동 가능한지 검사
29+ if 0 <= newX < N and 0 <= newY < N and arr [newX ][newY ] == 0 :
30+ if dir_idx == 2 : # 대각선
31+ if arr [newX - 1 ][newY ] == 1 or arr [newX ][newY - 1 ] == 1 :
32+ continue
33+
34+ # DFS 탐색
35+ paths += dfs (newX , newY , dir_idx )
36+
37+ # 결과를 메모이제이션에 저장
38+ memo [x ][y ][dir ] = paths
39+ return paths
40+
41+
42+ # 입력
43+ arr = [list (map (int , input ().split ())) for _ in range (N )]
44+
45+ # 메모이제이션 초기화: -1로 채움
46+ memo = [[[- 1 ] * 3 for _ in range (N )] for _ in range (N )]
47+
48+ # DFS 시작
49+ print (dfs (0 , 1 , 0 ))
You can’t perform that action at this time.
0 commit comments