AtCoderのAGC    次のAGCの問題へ    前のAGCの問題へ

AGC043-A Range Flip Find Route


問題へのリンク


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("##.");
            //1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 2");
            WillReturn.Add("#.");
            WillReturn.Add(".#");
            //2
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("4 4");
            WillReturn.Add("..##");
            WillReturn.Add("#...");
            WillReturn.Add("###.");
            WillReturn.Add("###.");
            //0
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("5 5");
            WillReturn.Add(".#.#.");
            WillReturn.Add("#.#.#");
            WillReturn.Add(".#.#.");
            WillReturn.Add("#.#.#");
            WillReturn.Add(".#.#.");
            //4
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct JyoutaiDef
    {
        internal int CurrX;
        internal int CurrY;
        internal int Cost;
    }

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

        char[,] BanArr = CreateBanArr(InputList.Skip(1).ToList());

        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrX = 0;
        WillPush.CurrY = 0;
        WillPush.Cost = 0;
        if (BanArr[0, 0] == '#') {
            WillPush.Cost++;
        }
        Stk.Push(WillPush);

        // 最小コスト[座標のハッシュ値]
        var MinCostDict = new Dictionary<int, int>();

        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

            char CurrColor = BanArr[Popped.CurrX, Popped.CurrY];

            Action<int, int> PushAct = (pNewX, pNewY) =>
            {
                if (pNewX < 0 || UB_X < pNewX) return;
                if (pNewY < 0 || UB_Y < pNewY) return;

                char NewChar = BanArr[pNewX, pNewY];

                WillPush.CurrX = pNewX;
                WillPush.CurrY = pNewY;
                WillPush.Cost = Popped.Cost;
                if (CurrColor == '.' && NewChar == '#') {
                    WillPush.Cost++;
                }

                int Hash = WillPush.CurrX * 1000 + WillPush.CurrY;
                if (MinCostDict.ContainsKey(Hash)) {
                    if (WillPush.Cost >= MinCostDict[Hash]) {
                        return;
                    }
                }
                MinCostDict[Hash] = WillPush.Cost;

                Stk.Push(WillPush);
            };

            PushAct(Popped.CurrX, Popped.CurrY + 1);
            PushAct(Popped.CurrX + 1, Popped.CurrY);
        }
        int AnswerHash = UB_X * 1000 + UB_Y;
        Console.WriteLine(MinCostDict[AnswerHash]);
    }

    // stringのListをcharの2次元配列に設定する
    static char[,] CreateBanArr(List<string> pStringList)
    {
        if (pStringList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = pStringList[0].Length - 1;
        int UB_Y = pStringList.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] = pStringList[Y][X];
            }
        }
        return WillReturn;
    }

    // 2次元配列(char型)のデバッグ出力
    static void PrintBan(char[,] pBanArr)
    {
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                Console.Write(pBanArr[X, Y]);
            }
            Console.WriteLine();
        }
    }
}


解説

白マスから黒マスに遷移したらコストが1増えるとして、
深さ優先探索してます。

その座標までの最小コストでの枝切りもしてます。