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

ABC213-E Stronger Takahashi


問題へのリンク


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

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

    struct PointDef
    {
        internal int X;
        internal int Y;
    }

    static char[,] mBanArr;
    static int UB_X;
    static int UB_Y;

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

        mBanArr = CreateBanArr(InputList.Skip(1));
        UB_X = mBanArr.GetUpperBound(0);
        UB_Y = mBanArr.GetUpperBound(1);

        var InsLinkedList = new LinkedList<JyoutaiDef>();
        JyoutaiDef WillEnqueue;
        WillEnqueue.CurrX = 0;
        WillEnqueue.CurrY = 0;
        WillEnqueue.Cost = 0;
        InsLinkedList.AddFirst(WillEnqueue);

        var MinCostDict = new Dictionary<int, int>();
        MinCostDict[0] = 0;

        while (InsLinkedList.Count > 0) {
            JyoutaiDef Dequeued = InsLinkedList.First.Value;
            //Console.WriteLine("X={0},Y={1}", Dequeued.CurrX, Dequeued.CurrY);
            InsLinkedList.RemoveFirst();

            if (Dequeued.CurrX == UB_X && Dequeued.CurrY == UB_Y) {
                Console.WriteLine(Dequeued.Cost);
                return;
            }

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

                if (mBanArr[pNewX, pNewY] == '#') {
                    List<PointDef> KinbouList = Derive8KinbouList(pNewX, pNewY);
                    foreach (PointDef EachPoint in KinbouList) {
                        int Hash = EachPoint.X * 1000 + EachPoint.Y;
                        WillEnqueue.CurrX = EachPoint.X;
                        WillEnqueue.CurrY = EachPoint.Y;
                        WillEnqueue.Cost = Dequeued.Cost + 1;

                        if (MinCostDict.ContainsKey(Hash)) {
                            if (MinCostDict[Hash] <= WillEnqueue.Cost) {
                                continue;
                            }
                        }
                        MinCostDict[Hash] = WillEnqueue.Cost;
                        InsLinkedList.AddLast(WillEnqueue);
                    }
                }
                else {
                    int Hash = pNewX * 1000 + pNewY;
                    WillEnqueue.CurrX = pNewX;
                    WillEnqueue.CurrY = pNewY;
                    WillEnqueue.Cost = Dequeued.Cost;

                    if (MinCostDict.ContainsKey(Hash)) {
                        if (MinCostDict[Hash] <= WillEnqueue.Cost) {
                            return;
                        }
                    }
                    MinCostDict[Hash] = WillEnqueue.Cost;

                    InsLinkedList.AddFirst(WillEnqueue);
                }
            };

            EnqueueAct(Dequeued.CurrX, Dequeued.CurrY - 1);
            EnqueueAct(Dequeued.CurrX, Dequeued.CurrY + 1);
            EnqueueAct(Dequeued.CurrX - 1, Dequeued.CurrY);
            EnqueueAct(Dequeued.CurrX + 1, Dequeued.CurrY);
        }
    }

    // 座標を引数として8近傍を返す
    static List<PointDef> Derive8KinbouList(int pBaseX, int pBaseY)
    {
        var WillReturn = new List<PointDef>();

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

            PointDef WillAdd;
            WillAdd.X = pNewX;
            WillAdd.Y = pNewY;
            WillReturn.Add(WillAdd);
        };
        AddAct(pBaseX - 1, pBaseY - 1);
        AddAct(pBaseX - 1, pBaseY);
        AddAct(pBaseX - 1, pBaseY + 1);
        AddAct(pBaseX, pBaseY - 1);
        AddAct(pBaseX, pBaseY);
        AddAct(pBaseX, pBaseY + 1);
        AddAct(pBaseX + 1, pBaseY - 1);
        AddAct(pBaseX + 1, pBaseY);
        AddAct(pBaseX + 1, pBaseY + 1);
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // 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;
    }
}


解説

01BFSで解いてます。