AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

AOJ 0720 カーペット (Carpet)


問題へのリンク


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

    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 = 0;
        Que.Enqueue(WillEnqueue);

        var VisitedSet = new HashSet<int>();

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

            // クリア判定
            if (Dequeued.CurrX == UB_X && Dequeued.CurrY == UB_Y) {
                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;

                char OldChar = BanArr[Dequeued.CurrX, Dequeued.CurrY];
                int NewChar = BanArr[pNewX, pNewY];

                if (OldChar == NewChar) 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 * 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で解いてます。