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

ABC378-D Count Simple Paths


問題へのリンク


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 2 2");
            WillReturn.Add(".#");
            WillReturn.Add("..");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 3 1");
            WillReturn.Add(".#.");
            WillReturn.Add("#.#");
            //0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 10 11");
            WillReturn.Add("....#..#..");
            WillReturn.Add(".#.....##.");
            WillReturn.Add("..#...##..");
            WillReturn.Add("...#......");
            WillReturn.Add("......##..");
            WillReturn.Add("..#......#");
            WillReturn.Add("#........#");
            WillReturn.Add("..##......");
            WillReturn.Add(".###....#.");
            WillReturn.Add("...#.....#");
            //218070
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int mAnswer;
    static int mK;

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

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        mK = 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] == '.') {
                    mVisitedSet.Clear();
                    dfs(X, Y, 0);
                }
            }
        }
        Console.WriteLine(mAnswer);
    }

    static HashSet<int> mVisitedSet = new HashSet<int>();
    static void dfs(int pX, int pY, int pLevel)
    {
        int CurrHash = GetHash(pX, pY);
        if (mVisitedSet.Add(CurrHash) == false) {
            return;
        }

        if (pLevel == mK) {
            mAnswer++;
        }
        else {
            Action<int, int> dfsAct = (pNewX, pNewY) =>
            {
                if (pNewX < 0 || UB_X < pNewX) return;
                if (pNewY < 0 || UB_Y < pNewY) return;
                if (mBanArr[pNewX, pNewY] == '#') return;

                dfs(pNewX, pNewY, pLevel + 1);
            };

            dfsAct(pX, pY - 1);
            dfsAct(pX, pY + 1);
            dfsAct(pX - 1, pY);
            dfsAct(pX + 1, pY);
        }

        mVisitedSet.Remove(CurrHash);
    }

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


解説

再訪防止の再帰を使ったDFSで解いてます。

状態に訪問座標を持たせて、Stackクラスを使ったDFSでも解けますが
状態ごとに訪問座標を持つので、TLEのリスクが増えます。