結果

問題 No.582 キャンディー・ボックス3
ユーザー maimai
提出日時 2017-04-14 01:34:24
言語 Ruby
(3.3.0)
結果
TLE  
実行時間 -
コード長 1,028 bytes
コンパイル時間 106 ms
コンパイル使用メモリ 11,472 KB
実行使用メモリ 51,652 KB
最終ジャッジ日時 2023-09-27 04:01:10
合計ジャッジ時間 6,043 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
22,244 KB
testcase_01 AC 77 ms
15,132 KB
testcase_02 AC 77 ms
15,292 KB
testcase_03 AC 74 ms
15,188 KB
testcase_04 AC 74 ms
15,276 KB
testcase_05 AC 73 ms
15,044 KB
testcase_06 AC 76 ms
15,280 KB
testcase_07 AC 77 ms
15,088 KB
testcase_08 AC 74 ms
15,048 KB
testcase_09 AC 75 ms
15,116 KB
testcase_10 AC 76 ms
15,152 KB
testcase_11 AC 76 ms
15,156 KB
testcase_12 AC 190 ms
17,796 KB
testcase_13 AC 117 ms
20,264 KB
testcase_14 AC 98 ms
18,516 KB
testcase_15 AC 482 ms
21,220 KB
testcase_16 AC 114 ms
18,992 KB
testcase_17 AC 76 ms
15,044 KB
testcase_18 TLE -
testcase_19 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def scan; gets.chomp!.split.map(&:to_i); end

def flip(turn)
    turn == :a_san ? :b_san : :a_san
end

@memo={}
def solve(c, turn = :a_san)
    c.delete(0)
    
    key = c+[turn]
    
    # すべての箱が空になりキャンディーを取れなくなったほうが負け
    return flip(turn) if c.empty?
    
    # メモ化再帰
    return @memo[key] if @memo[key]
    
    c.each_index{|i|
        # i番目の箱から1つ取り出して相手の番に移った時,自分が必勝であればその手を打つ
        c[i] -= 1
        return @memo[key]=turn if solve(c.clone, flip(turn)) == turn
        if turn == :b_san && 0 < c[i]
            # Bさんはもう1個とれる
            c[i] -= 1
            return @memo[key]=turn if solve(c.clone, flip(turn)) == turn
            c[i] += 1
        end
        c[i] += 1
    }
    return @memo[key]=flip(turn)
end

# input
gets
c = scan

# キャンディーの個数をすべて3で抑える.
c.map!{|e| [3,e].min}

puts solve(c) == :a_san ? 'A' : 'B'


0