DPコンテスト    次のDPコンテストの問題へ    前のDPコンテストの問題へ

Educational DP Contest K Stones


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("2 4");
            WillReturn.Add("2 3");
            //First
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 5");
            WillReturn.Add("2 3");
            //Second
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2 7");
            WillReturn.Add("2 3");
            //First
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("3 20");
            WillReturn.Add("1 2 3");
            //Second
        }
        else if (InputPattern == "Input5") {
            WillReturn.Add("3 21");
            WillReturn.Add("1 2 3");
            //First
        }
        else if (InputPattern == "Input6") {
            WillReturn.Add("1 100000");
            WillReturn.Add("1");
            //Second
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int K = wkArr[1];

        int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();

        // その手番のプレーヤが勝ち[石の数]なDP表
        bool[] DPArr = new bool[K + 1];
        DPArr[0] = false;
        for (int I = 1; I <= K; I++) {
            // 遷移可能な盤面の少なくとも1つで、その手番のプレーヤーの負けなら、
            // この手番のプレーヤーは勝ち
            bool CanWin = false;
            foreach (int EachA in AArr) {
                int ToNodeInd = I - EachA;
                if (ToNodeInd < 0) continue;
                if (DPArr[ToNodeInd] == false) {
                    CanWin = true;
                    break;
                }
            }
            DPArr[I] = CanWin;
        }
        Console.WriteLine(DPArr[K] ? "First" : "Second");
    }
}


解説

DPで後退解析してます。