AtCoderのARC    次のARCの問題へ

ARC005-C 器物損壊!高橋君


問題へのリンク


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

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

    static void Main()
    {
        List<string> InputList = GetInputList();

        char[,] BanArr = CreateBanArr(InputList.Skip(1).ToList());
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        int StaX = -1, StaY = -1;
        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                if (BanArr[X, Y] == 's') {
                    StaX = X;
                    StaY = Y;
                }
            }
        }

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrX = StaX;
        WillPush.CurrY = StaY;
        WillPush.HakaiCnt = 0;
        Stk.Push(WillPush);

        // 最小の破壊回数[座標のハッシュ値]
        var MinHakaiCntDict = new Dictionary<int, int>();
        MinHakaiCntDict[GetHash(StaX, StaY)] = 0;

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

            if (BanArr[Popped.CurrX, Popped.CurrY] == 'g') {
                HasAnswer = true;
                break;
            }

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

                WillPush.CurrX = pNewX;
                WillPush.CurrY = pNewY;
                WillPush.HakaiCnt = Popped.HakaiCnt;

                if (BanArr[pNewX, pNewY] == '#') {
                    WillPush.HakaiCnt++;
                    if (WillPush.HakaiCnt == 3) {
                        return;
                    }
                }

                int Hash = GetHash(pNewX, pNewY);
                if (MinHakaiCntDict.ContainsKey(Hash)) {
                    if (WillPush.HakaiCnt >= MinHakaiCntDict[Hash]) {
                        return;
                    }
                }
                MinHakaiCntDict[Hash] = WillPush.HakaiCnt;
                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);
        }
        Console.WriteLine(HasAnswer ? "YES" : "NO");
    }

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

    ////////////////////////////////////////////////////////////////
    // stringのListをcharの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static char[,] CreateBanArr(List<string> pStringList)
    {
        if (pStringList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = pStringList[0].Length - 1;
        int UB_Y = pStringList.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] = pStringList[Y][X];
            }
        }
        return WillReturn;
    }
}


解説

深さ優先探索で解いてます。