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

ABC020-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("2 3 10");
            WillReturn.Add("S##");
            WillReturn.Add(".#G");
            //8
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 4 7");
            WillReturn.Add("S##G");
            WillReturn.Add(".##.");
            WillReturn.Add("..#.");
            //3
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("4 4 1000000000");
            WillReturn.Add("S###");
            WillReturn.Add("####");
            WillReturn.Add("####");
            WillReturn.Add("###G");
            //199999999
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long mT;

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

    static int mStaX;
    static int mStaY;

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

        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        mT = wkArr[2];

        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] == 'S') {
                    mStaX = X; mStaY = Y;
                }
            }
        }
        long Answer = ExecNibunhou();
        Console.WriteLine(Answer);
    }

    // 二分法で、最大の黒マスのコストを返す
    static long ExecNibunhou()
    {
        long L = 1;
        long R = mT + 1;

        while (L + 1 < R) {
            long Mid = (L + R) / 2;

            if (CanGoal(Mid)) {
                L = Mid;
            }
            else {
                R = Mid;
            }
        }
        return L;
    }
    struct JyoutaiDef
    {
        internal int CurrX;
        internal int CurrY;
        internal long CostSum;
    }

    // 黒マスのコストを引数として、DFSでゴールまで到達可能かを判定
    static bool CanGoal(long pBlackCost)
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrX = mStaX;
        WillPush.CurrY = mStaY;
        WillPush.CostSum = 0;
        Stk.Push(WillPush);

        var MinCostDict = new Dictionary<int, long>();
        MinCostDict[GetHash(mStaX, mStaY)] = 0;

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

            // クリア判定
            if (mBanArr[Popped.CurrX, Popped.CurrY] == 'G') {
                return true;
            }

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

                WillPush.CostSum = Popped.CostSum;
                if (mBanArr[pNewX, pNewY] == '#') {
                    WillPush.CostSum += pBlackCost;
                }
                else {
                    WillPush.CostSum++;
                }

                if (WillPush.CostSum > mT) return;

                int Hash = GetHash(pNewX, pNewY);
                if (MinCostDict.ContainsKey(Hash)) {
                    if (MinCostDict[Hash] <= WillPush.CostSum) {
                        return;
                    }
                }
                MinCostDict[Hash] = WillPush.CostSum;

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

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


解説

黒マスに設定可能な最大コストを二分法で求めてます。