AtCoderの有志コンテスト    前の有志コンテストの問題へ

パ研合宿2019 D パ研軍旗


問題へのリンク


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("1");
            WillReturn.Add("B");
            WillReturn.Add("R");
            WillReturn.Add("#");
            WillReturn.Add("W");
            WillReturn.Add("B");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3");
            WillReturn.Add("WWR");
            WillReturn.Add("#RW");
            WillReturn.Add("BW#");
            WillReturn.Add("##B");
            WillReturn.Add("RBR");
            //10
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("8");
            WillReturn.Add("RRRRRRRR");
            WillReturn.Add("########");
            WillReturn.Add("BBBBBBBB");
            WillReturn.Add("RRRRRRRR");
            WillReturn.Add("WWWWWWWW");
            //28
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("7");
            WillReturn.Add("BR#WB#R");
            WillReturn.Add("RWW#WRB");
            WillReturn.Add("##WBR#W");
            WillReturn.Add("WB#B#RW");
            WillReturn.Add("BRW##BB");
            //21
        }
        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));
        long UB_X = BanArr.GetUpperBound(0);
        long UB_Y = BanArr.GetUpperBound(1);

        // 最小コスト[前の列の色]なDP表
        var PrevDP = new Dictionary<char, long>();
        PrevDP['X'] = 0;

        for (long X = 0; X <= UB_X; X++) {
            var CurrDP = new Dictionary<char, long>();
            foreach (var EachPair in PrevDP) {
                Action<char> UpdateAct = (pNewColor) =>
                {
                    if (pNewColor == EachPair.Key) return;

                    long NewVal = EachPair.Value;
                    for (long Y = 0; Y <= UB_Y; Y++) {
                        if (BanArr[X, Y] != pNewColor) NewVal++;
                    }

                    if (CurrDP.ContainsKey(pNewColor)) {
                        if (CurrDP[pNewColor] <= NewVal) {
                            return;
                        }
                    }
                    CurrDP[pNewColor] = NewVal;
                };

                UpdateAct('B');
                UpdateAct('W');
                UpdateAct('R');
            }
            PrevDP = CurrDP;
        }
        Console.WriteLine(PrevDP.Values.Min());
    }

    ////////////////////////////////////////////////////////////////
    // 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してます。