06bcdb27eae639aa658084e54487746f5b2de6b884c5b258ed488a16b392fcd9e649765e5eb3021df92b4015import numpy as np


def solve_with_optimal_strategy():

    target = 5

    # win_prob[i][j]: A가 i점, B가 j점일 때 A의 최종 승률

    win_prob = np.zeros((target +1, target + 1))

    

    # 기저 조건: 누군가 5점에 도달했을 때

    for i in range(target):

        win_prob[target][i] = 1.0  # A 승리

        win_prob[i][target] = 0.0  # B 승리


    # 1. 승률 최적화 (Value Iteration)

    for _ in range(100):

        new_win_prob = win_prob.copy()

        for i in range(target):

            for j in range(target):

                # A가 이겼을 때 최적 선택

                p_plus_A = win_prob[i+1][j]

                p_minus_A = win_prob[i][max(0, j-1)] if j > 0 else -1

                best_p_A = max(p_plus_A, p_minus_A)

                

                # B가 이겼을 때 최적 선택 (B는 A의 승률을 최소화하려 함)

                p_plus_B = win_prob[i][j+1]

                p_minus_B = win_prob[max(0, i-1)][j] if i > 0 else 2

                best_p_B = min(p_plus_B, p_minus_B)

                

                new_win_prob[i][j] = 0.5 * best_p_A + 0.5 * best_p_B

        if np.allclose(win_prob, new_win_prob):

            break

        win_prob = new_win_prob


    # 2. 결정된 최적 전략하에서 평균 라운드 수(E) 계산

    # E = 1 + 0.5 * E(next_state_if_A_wins) + 0.5 * E(next_state_if_B_wins)

    states = [(i, j) for i in range(target) for j in range(target)]

    n = len(states)

    A_mat = np.eye(n)

    b_vec = np.ones(n)


    for idx, (i, j) in enumerate(states):

        # A 승리 시 행동 결정

        if win_prob[i+1][j] >= (win_prob[i][max(0, j-1)] if j > 0 else -1):

            next_A = (i+1, j)

        else:

            next_A = (i, j-1)

            

        # B 승리 시 행동 결정

        if win_prob[i][j+1] <= (win_prob[max(0, i-1)][j] if i > 0 else 2):

            next_B = (i, j+1)

        else:

            next_B = (max(0, i-1), j)


        for next_state in [next_A, next_B]:

            if next_state[0] < target and next_state[1] < target:

                A_mat[idx, states.index(next_state)] -= 0.5


    exp_rounds = np.linalg.solve(A_mat, b_vec)

    return win_prob, exp_rounds[states.index((0, 0))]


win_p, avg_r = solve_with_optimal_strategy()

print(f"0:0 상태에서의 평균 라운드 수: {avg_r:.2f}")



위 문제를 이 코드를 이용해서 답을 구해보려 했음. 코드 실행하면 나오는 값은 14.9

근데 찾아봤을때 옳은 답은 11이라고 함

왜 코드에서는 11이 아닌 14.9가 나오는 건지, 대체 어느 부분에서 오류가 있는지 도저히 모르겠는데 좀 알려줄 사람 없음?