백준 17825 주사위 윷놀이

gmlwlswldbs·2021년 10월 19일
0

코딩테스트

목록 보기
56/130
dice = list(map(int, input().split()))

g = [[] for _ in range(31)]
g[1] = [1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 41]
g[10] = [10, 13, 16, 19, 25, 30, 35, 40, 41]
g[20] = [20, 22, 24, 25, 30, 25, 40, 41]
g[30] = [30, 28, 27, 26, 25, 30, 35, 40, 41]

mal = [1, 1, 1, 1]
scorelist = []

def move(turn, mal, score):
    if turn == 10 or sum(mal) == 164:
        print(turn, mal)
        scorelist.append(score)
        return
    for i in range(len(mal)):
        x = mal[i]
        #파란색 시작
        if x == 10 or x == 20 or x == 30:
            # 리스트 어딘가에서 끝
            if dice[turn] < len(g[x]) - 1:
                if g[x][dice[turn]] in mal:
                    continue
                mal[i] = g[x][dice[turn]]
                score += mal[i]
                move(turn+1, mal, score)
            # 도착 지점에 도착
            elif dice[turn] >= len(g[x]) - 1:
                mal[i] = 41
                move(turn+1, mal, score)
        #빨간색 시작점
        else:
            for j in range(len(g[1])):
                if g[1][j] == x:  
                    if j + dice[turn] >= len(g[1]) - 1:
                        mal[i] = 41
                        move(turn+1, mal, score)
                    else:
                        if g[1][j + dice[turn]] in mal:
                            continue  
                        mal[i] = g[1][j + dice[turn]]
                        score += mal[i]
                        move(turn+1, mal, score)

move(0, mal, 0)
print(max(scorelist))

왜안돼.......? 어떻게 바로잡는지도 모르겟다

dice = list(map(int, input().split()))
score = [
    0,2,4,6,8,
    10,13,16,19,25,
    12,14,16,18,20,
    22,24,22,24,26,
    28,26,27,28,30,
    32,34,36,38,30,
    35,40,0
]
a = [0]*33
a[0] = [1,1]
a[1] = [2,2]
a[2] = [3,3]
a[3] = [4,4]
a[4] = [5,5]
a[5] = [6,10]
a[6] = [7,7]
a[7] = [8,8]
a[8] = [9,9]
a[9] = [29,29]
a[10] = [11,11]
a[11] = [12,12]
a[12] = [13,13]
a[13] = [14,14]
a[14] = [15,17]
a[15] = [16,16]
a[16] = [9,9]
a[17] = [18,18]
a[18] = [19,19]
a[19] = [20,20]
a[20] = [24,24]
a[21] = [9,9]
a[22] = [21,21]
a[23] = [22,22]
a[24] = [23,25]
a[25] = [26,26]
a[26] = [27,27]
a[27] = [28,28]
a[28] = [31,31]
a[29] = [30,30]
a[30] = [31,31]
a[31] = [32,32]
a[32] = [32,32]
mal = [0, 0, 0, 0]
def move(turn, mal, ans):
    if turn == 10:
        return ans
    s = 0
    for i in range(4):
        # 말을 이동..
        moveto = mal[i]
        for j in range(dice[turn]):
            if j == 0: #시작할 때
                moveto = a[moveto][0]
            else: #그냥 이동시
                moveto = a[moveto][1]
        # 만약 이동한 자리(moveto)가 도착지점이 아니고 말 리스트에 없으면(도착지점에 다른 말이 없으면) 다시 move 호출해야함
        ok = True
        if moveto != 32:
            for j in range(4):
                if i == j:
                    continue
                if moveto == mal[j]:
                    ok = False
        if ok:
            tmp_mal = mal[:]
            tmp_mal[i] = moveto
            t_ans = move(turn+1, tmp_mal, ans + score[moveto])
            if s < t_ans:
                s = t_ans
    return s

print(move(0, mal, 0))

0개의 댓글