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

ABC317-E Avoid Eye Contact


問題へのリンク


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 7");
            WillReturn.Add("....Sv.");
            WillReturn.Add(".>.....");
            WillReturn.Add(".......");
            WillReturn.Add(">..<.#<");
            WillReturn.Add("^G....>");
            //15
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 3");
            WillReturn.Add("S..");
            WillReturn.Add(".<.");
            WillReturn.Add(".>.");
            WillReturn.Add("..G");
            //-1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

    static int mGoalX;
    static int mGoalY;
    static int mStaX;
    static int mStaY;

    static void Main()
    {
        List<string> InputList = GetInputList();
        mBanArr = CreateBanArr(InputList.Skip(1));
        UB_X = mBanArr.GetUpperBound(0);
        UB_Y = mBanArr.GetUpperBound(1);

        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (mBanArr[X, Y] == '>') InitMap(X, Y, 1, 0);
                if (mBanArr[X, Y] == '<') InitMap(X, Y, -1, 0);
                if (mBanArr[X, Y] == 'v') InitMap(X, Y, 0, 1);
                if (mBanArr[X, Y] == '^') InitMap(X, Y, 0, -1);
            }
        }

        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (mBanArr[X, Y] == 'S') {
                    mStaX = X;
                    mStaY = Y;
                }
                if (mBanArr[X, Y] == 'G') {
                    mGoalX = X;
                    mGoalY = Y;
                }
            }
        }

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

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

        while (Que.Count > 0) {
            JyoutaiDef Dequeued = Que.Dequeue();

            if (Dequeued.CurrX == mGoalX && Dequeued.CurrY == mGoalY) {
                Console.WriteLine(Dequeued.Level);
                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] != '.' && mBanArr[pNewX, pNewY] != 'G') {
                    return;
                }

                int Hash = GetHash(pNewX, pNewY);
                if (VisitedSet.Add(Hash)) {
                    WillEnqueue.CurrX = pNewX;
                    WillEnqueue.CurrY = pNewY;
                    WillEnqueue.Level = Dequeued.Level + 1;
                    Que.Enqueue(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);
        }
        Console.WriteLine(-1);
    }

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

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

    static void InitMap(int pBaseX, int pBaseY, int pVectX, int pVectY)
    {
        int CurrX = pBaseX + pVectX;
        int CurrY = pBaseY + pVectY;

        while (true) {
            if (CurrX < 0 || UB_X < CurrX) return;
            if (CurrY < 0 || UB_Y < CurrY) return;
            if (mBanArr[CurrX, CurrY] == '.' || mBanArr[CurrX, CurrY] == '!') {
                mBanArr[CurrX, CurrY] = '!';
            }
            else {
                return;
            }
            CurrX += pVectX;
            CurrY += pVectY;
        }
    }

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