結果

問題 No.102 トランプを奪え
ユーザー gew1fw
提出日時 2025-06-12 13:51:28
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,883 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 82,252 KB
実行使用メモリ 329,416 KB
最終ジャッジ日時 2025-06-12 13:52:08
合計ジャッジ時間 11,745 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 TLE * 1 -- * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import lru_cache
import sys

sys.setrecursionlimit(1000000)

def main():
    n = list(map(int, input().split()))
    piles = tuple(n)
    result = evaluate(piles, 0, 0, 'Taro')
    print(result)

@lru_cache(maxsize=None)
def evaluate(piles, t, j, turn):
    total = sum(piles)
    if total == 0:
        if t > j:
            return 'Taro'
        elif j > t:
            return 'Jiro'
        else:
            return 'Draw'

    possible_outcomes = []
    for i in range(4):
        if piles[i] == 0:
            continue
        for take in range(1, 4):
            if take > piles[i]:
                continue
            new_piles = list(piles)
            new_piles[i] -= take
            new_piles = tuple(new_piles)
            last_card = (new_piles[i] == 0)
            if turn == 'Taro':
                new_t = t + take
                new_j = j
                if last_card:
                    steal = (new_j + 1) // 2
                    new_t += steal
                    new_j -= steal
                next_turn = 'Jiro'
            else:
                new_j = j + take
                new_t = t
                if last_card:
                    steal = (new_t + 1) // 2
                    new_j += steal
                    new_t -= steal
                next_turn = 'Taro'
            outcome = evaluate(new_piles, new_t, new_j, next_turn)
            possible_outcomes.append(outcome)

    if turn == 'Taro':
        if any(out == 'Taro' for out in possible_outcomes):
            return 'Taro'
        if any(out == 'Draw' for out in possible_outcomes):
            return 'Draw'
        return 'Jiro'
    else:
        if any(out == 'Jiro' for out in possible_outcomes):
            return 'Jiro'
        if any(out == 'Draw' for out in possible_outcomes):
            return 'Draw'
        return 'Taro'

if __name__ == '__main__':
    main()
0