AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC201-D Game in Momotetsu World


問題へのリンク


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("3 3");
            WillReturn.Add("---");
            WillReturn.Add("+-+");
            WillReturn.Add("+--");
            //Takahashi
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 4");
            WillReturn.Add("+++-");
            WillReturn.Add("-+-+");
            //Aoki
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("1 1");
            WillReturn.Add("-");
            //Draw
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

        char[,] BanArr = CreateBanArr(InputList.Skip(1));
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        // 簡単のため(X座標 + Y座標) % 2 == 0 なら -と+を入れ替え
        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if ((X + Y) % 2 == 0) {
                    char CurrSign = BanArr[X, Y];
                    BanArr[X, Y] = (CurrSign == '-' ? '+' : '-');
                }
            }
        }

        // この座標以降での高橋君 - 青木君 の最大値 [X座標,Y座標] なDP表
        int[,] DPArr = new int[UB_X + 1, UB_Y + 1];

        DPArr[UB_X, UB_Y] = 0;
        for (int Y = UB_Y; 0 <= Y; Y--) {
            for (int X = UB_X; 0 <= X; X--) {
                if (X == UB_X && Y == UB_Y) continue;

                // 貰うDP
                var KouhoList = new List<int>();

                Action<int, int> AddAct = (pFromX, pFromY) =>
                {
                    if (pFromX < 0 || UB_X < pFromX) return;
                    if (pFromY < 0 || UB_Y < pFromY) return;

                    int Kouho = DPArr[pFromX, pFromY];
                    if (BanArr[pFromX, pFromY] == '-') Kouho--;
                    if (BanArr[pFromX, pFromY] == '+') Kouho++;
                    KouhoList.Add(Kouho);
                };
                AddAct(X, Y + 1);
                AddAct(X + 1, Y);

                int Mod2 = (X + Y) % 2;

                // 高橋君なら候補の最大値に遷移
                if (Mod2 == 0) {
                    DPArr[X, Y] = KouhoList.Max();
                }
                // 青木君なら候補の最小値に遷移
                else {
                    DPArr[X, Y] = KouhoList.Min();
                }
            }
        }
        if (DPArr[0, 0] > 0) {
            Console.WriteLine("Takahashi");
        }
        if (DPArr[0, 0] == 0) {
            Console.WriteLine("Draw");
        }
        if (DPArr[0, 0] < 0) {
            Console.WriteLine("Aoki");
        }
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をcharの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static char[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = StrList[0].Length - 1;
        int UB_Y = StrList.Count - 1;

        char[,] WillReturn = new char[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = StrList[Y][X];
            }
        }
        return WillReturn;
    }
}


解説

後退解析な貰うDPで解いてます。