yukicoder    次のyukicoderの問題へ    前のyukicoderの問題へ

yukicoder 2926 Gridpath


問題へのリンク


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

    static int UB_X;
    static int UB_Y;

    static int mStaX;
    static int mStaY;
    static int mGoalX;
    static int mGoalY;

    static long mAnswer = 0;
    static HashSet<string> mVisitedSet = new HashSet<string>();

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

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(InputList[0]);
        UB_X = wkArr[1] - 1;
        UB_Y = wkArr[0] - 1;

        SplitAct(InputList[1]);
        mStaX = wkArr[1] - 1;
        mStaY = wkArr[0] - 1;

        SplitAct(InputList[2]);
        mGoalX = wkArr[1] - 1;
        mGoalY = wkArr[0] - 1;

        DFS(mStaX, mStaY);
        Console.WriteLine(mAnswer);
    }

    static void DFS(int pCurrX, int pCurrY)
    {
        if (pCurrX < 0 || UB_X < pCurrX) return;
        if (pCurrY < 0 || UB_Y < pCurrY) return;

        string CurrHash = GetHash(pCurrX, pCurrY);

        // 再訪は不可
        if (mVisitedSet.Add(CurrHash) == false) {
            return;
        }

        // 新しい座標の4近傍で、2つ以上訪問済はNG
        int VisitedCnt = 0;

        Action<int, int> CntAct = (pTargetX, pTargetY) =>
        {
            string Hash = GetHash(pTargetX, pTargetY);
            if (mVisitedSet.Contains(Hash)) {
                VisitedCnt++;
            }
        };

        CntAct(pCurrX, pCurrY - 1);
        CntAct(pCurrX, pCurrY + 1);
        CntAct(pCurrX - 1, pCurrY);
        CntAct(pCurrX + 1, pCurrY);

        if (VisitedCnt >= 2) {
            mVisitedSet.Remove(CurrHash);
            return;
        }

        // クリア判定
        if (pCurrX == mGoalX && pCurrY == mGoalY) {
            mAnswer++;
        }
        else {
            DFS(pCurrX, pCurrY - 1);
            DFS(pCurrX, pCurrY + 1);
            DFS(pCurrX - 1, pCurrY);
            DFS(pCurrX + 1, pCurrY);
        }

        mVisitedSet.Remove(CurrHash);
    }

    // 座標のハッシュ値を返す
    static string GetHash(int pX, int pY)
    {
        return string.Format("{0},{1}", pX, pY);
    }
}


解説

DFS木をイメージして、再帰でDFSし、
訪問済み座標は、HashSetで管理してます。