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

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

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

    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);

        var Que = new Queue<JyoutaiDef>();
        JyoutaiDef WillEnqueue;
        WillEnqueue.CurrX = 0;
        WillEnqueue.CurrY = 0;
        WillEnqueue.Level = 1;
        Que.Enqueue(WillEnqueue);

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(GetHash(0, 0));

        int Answer = 1;
        while (Que.Count > 0) {
            JyoutaiDef Dequeued = Que.Dequeue();
            //Console.WriteLine("CurrX={0},CurrY={1}", Dequeued.CurrX, Dequeued.CurrY);

            Answer = Math.Max(Answer, Dequeued.Level);

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

                if (BanArr[pNewX, pNewY] == '#') return;

                int Hash = GetHash(pNewX, pNewY);
                if (VisitedSet.Add(Hash) == false) return;

                WillEnqueue.CurrX = pNewX;
                WillEnqueue.CurrY = pNewY;
                WillEnqueue.Level = Dequeued.Level + 1;
                Que.Enqueue(WillEnqueue);
            };

            wkAct(Dequeued.CurrX, Dequeued.CurrY + 1);
            wkAct(Dequeued.CurrX + 1, Dequeued.CurrY);
        }
        Console.WriteLine(Answer);
    }

    static int GetHash(int pX, int pY)
    {
        return pX * 1000 + pY;
    }

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


解説

BFSで解いてます。