square869120Contest    前のsquare869120Contestの問題へ

square869120コンテスト6 C問題 Infinite Grid


問題へのリンク


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("1 3");
            WillReturn.Add("...");
            //Yay!
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("1 3");
            WillReturn.Add(".#.");
            //:(
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("3 3");
            WillReturn.Add(".##");
            WillReturn.Add("...");
            WillReturn.Add("##.");
            //Yay!
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("7 7");
            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 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);
        if (IsValid() == false) {
            Console.WriteLine(":(");
            return;
        }

        var Que = new Queue<JyoutaiDef>();
        JyoutaiDef WillEnqueue;
        WillEnqueue.Curr_X = 0;
        WillEnqueue.Curr_Y = 0;
        Que.Enqueue(WillEnqueue);

        var VisitedSet = new HashSet<int>();

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

            // クリア判定
            if (Dequeued.Curr_X == UB_X && Dequeued.Curr_Y == UB_Y) {
                Console.WriteLine("Yay!");
                return;
            }

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

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

                int Hash = GetHash(pNewX, pNewY);
                if (VisitedSet.Add(Hash)) {
                    WillEnqueue.Curr_X = pNewX;
                    WillEnqueue.Curr_Y = pNewY;
                    Que.Enqueue(WillEnqueue);
                }
            };

            EnqueueAct(Dequeued.Curr_X, Dequeued.Curr_Y + 1);
            EnqueueAct(Dequeued.Curr_X + 1, Dequeued.Curr_Y);
        }
        Console.WriteLine(":(");
    }

    // #の無い行が1行以上存在するかを判定
    static bool IsValid()
    {
        for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
            bool IsOK = true;
            for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
                if (mBanArr[LoopX, LoopY] == '#') {
                    IsOK = false;
                }
            }
            if (IsOK) {
                return true;
            }
        }
        return false;
    }

    struct JyoutaiDef
    {
        internal int Curr_X;
        internal int Curr_Y;
    }

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


解説

下記の2点を両方満たすかを判定してます
●壁がない行が少なくとも1行あること
●右端から左端に移動可能として、ゴールに到達可能であること