Skip to content

Commit 2ec53d9

Browse files
committed
[BOJ] 파이프 옮기기 1 / 골드 5 / 60분
https://www.acmicpc.net/problem/17070
1 parent 8fec85c commit 2ec53d9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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))

0 commit comments

Comments
 (0)