Skip to content

Commit ebe1699

Browse files
committed
[BOJ] #17825. 주사위 윷놀이 / 골드2 / 60분 / 실패
1 parent 301fb6f commit ebe1699

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 게임판 그래프 정의
2+
graph = [[1], [2], [3], [4], [5],
3+
[6, 21], [7], [8], [9], [10],
4+
[11, 25], [12], [13], [14], [15],
5+
[16, 27], [17], [18], [19], [20],
6+
[32], [22], [23], [24], [30],
7+
[26], [24], [28], [29], [24],
8+
[31], [20], [32]]
9+
10+
# 각 칸의 점수
11+
score = [0, 2, 4, 6, 8,
12+
10, 12, 14, 16, 18,
13+
20, 22, 24, 26, 28,
14+
30, 32, 34, 36, 38,
15+
40, 13, 16, 19, 25,
16+
22, 24, 28, 27, 26,
17+
30, 35, 0]
18+
19+
dice = list(map(int, input().split())) # 주사위 값 입력
20+
answer = 0 # 최대 점수 저장 변수
21+
22+
# 백트래킹 함수
23+
def backtracking(depth, result, horses):
24+
global answer
25+
# 10개의 주사위를 모두 사용한 경우 최대값 갱신
26+
if depth == 10:
27+
answer = max(answer, result)
28+
return
29+
30+
# 4개의 말을 순서대로 선택해 이동
31+
for i in range(4):
32+
x = horses[i] # 현재 말 위치
33+
34+
# 파란색 화살표 처리
35+
if len(graph[x]) == 2:
36+
x = graph[x][1]
37+
else:
38+
x = graph[x][0]
39+
40+
# 주사위 값만큼 이동
41+
for _ in range(1, dice[depth]):
42+
x = graph[x][0]
43+
44+
# 이동한 위치가 도착 칸이거나, 다른 말이 없는 칸일 때만 이동
45+
if x == 32 or (x < 32 and x not in horses):
46+
before = horses[i] # 원래 위치 저장
47+
horses[i] = x # 말 이동
48+
backtracking(depth + 1, result + score[x], horses) # 재귀 호출
49+
horses[i] = before # 위치 복구
50+
51+
# 백트래킹 시작
52+
backtracking(0, 0, [0, 0, 0, 0])
53+
print(answer)

0 commit comments

Comments
 (0)