AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第5回PAST H コンベア


問題へのリンク


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("1 1");
            WillReturn.Add("..#");
            WillReturn.Add("^^.");
            WillReturn.Add("><.");
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10 12");
            WillReturn.Add("9 1");
            WillReturn.Add("#.^<..><<...");
            WillReturn.Add("#<>.#<^.<<.^");
            WillReturn.Add("^.<>.^.^.^>.");
            WillReturn.Add("^.>#^><#....");
            WillReturn.Add(".>.^>#...<<>");
            WillReturn.Add("....^^.#<.<.");
            WillReturn.Add(".>^..^#><#.^");
            WillReturn.Add("......#>....");
            WillReturn.Add("..<#<...^>^.");
            WillReturn.Add("<..^>^^...^<");
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("15 20");
            WillReturn.Add("13 9");
            WillReturn.Add("####..<#^>#>.<<><^..");
            WillReturn.Add("#.>#>.^#^.>><>...^..");
            WillReturn.Add(">..<>.#.>.>.>...#..<");
            WillReturn.Add("<^>.#..<>^#<#.>.<.^.");
            WillReturn.Add(">#<^>.>#^>#^.^.#^><^");
            WillReturn.Add("<^.^.#<...<.><#>...#");
            WillReturn.Add(".<>....^..#>>#..>>><");
            WillReturn.Add(".<#<^#.>#>^^.>.##.^<");
            WillReturn.Add(".#.^.....<<#^#><^<<<");
            WillReturn.Add("^.#>.#^.>.^.^<<>..><");
            WillReturn.Add(".^#^<^^^<......^>.#^");
            WillReturn.Add(".<..#>...^>^.^<..<.^");
            WillReturn.Add("#.^.#..#.....>#.^^.>");
            WillReturn.Add(".#^..>>><>>>^..#^.^^");
            WillReturn.Add(".>#..<..<>.#>..^.#.^");
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int mGoalX;
    static int mGoalY;

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

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        mGoalX = wkArr[1] - 1;
        mGoalY = wkArr[0] - 1;

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

        HashSet<long> VisitedSet = ExecDFS();

        var sb = new System.Text.StringBuilder();
        for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
            for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
                if (mBanArr[LoopX, LoopY] == '#') {
                    sb.Append('#');
                    continue;
                }
                long CurrHash = GetHash(LoopX, LoopY);
                if (VisitedSet.Contains(CurrHash)) {
                    sb.Append('o');
                }
                else {
                    sb.Append('x');
                }
            }
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }

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

    // DFSを行い、到達可能な座標のSetを返す
    static HashSet<long> ExecDFS()
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrX = mGoalX;
        WillPush.CurrY = mGoalY;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<long>();
        VisitedSet.Add(GetHash(mGoalX, mGoalY));

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

            Action<int, int> PushAct = (pNewX, pNewY) =>
            {
                if (pNewX < 0 || UB_X < pNewX) return;
                if (pNewY < 0 || UB_Y < pNewY) return;
                if (mBanArr[pNewX, pNewY] == '#') return;

                // 変位ベクトル
                int Vect_X = pNewX - Popped.CurrX;
                int Vect_Y = pNewY - Popped.CurrY;

                if (mBanArr[pNewX, pNewY] == '^') {
                    if (Vect_X != 0 || Vect_Y != 1) return;
                }
                if (mBanArr[pNewX, pNewY] == '>') {
                    if (Vect_X != -1 || Vect_Y != 0) return;
                }
                if (mBanArr[pNewX, pNewY] == 'v') {
                    if (Vect_X != 0 || Vect_Y != -1) return;
                }
                if (mBanArr[pNewX, pNewY] == '<') {
                    if (Vect_X != 1 || Vect_Y != 0) return;
                }

                long Hash = GetHash(pNewX, pNewY);
                if (VisitedSet.Add(Hash)) {
                    WillPush.CurrX = pNewX;
                    WillPush.CurrY = pNewY;
                    Stk.Push(WillPush);
                }
            };
            PushAct(Popped.CurrX, Popped.CurrY - 1);
            PushAct(Popped.CurrX, Popped.CurrY + 1);
            PushAct(Popped.CurrX - 1, Popped.CurrY);
            PushAct(Popped.CurrX + 1, Popped.CurrY);
        }
        return VisitedSet;
    }

    static long GetHash(int pX, int pY)
    {
        return (long)pX * 10000000 + 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;
    }
}


解説

1手後に移動可能なマスに遷移するロジックで
ゴールから逆方向に探索してます。