結果

問題 No.2928 Gridpath
ユーザー aketijyuuzou
提出日時 2025-02-15 00:25:47
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 3,176 bytes
コンパイル時間 5,716 ms
コンパイル使用メモリ 113,700 KB
実行使用メモリ 33,244 KB
最終ジャッジ日時 2025-02-15 00:26:11
合計ジャッジ時間 8,283 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

// https://yukicoder.me/problems/no/2928
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 Answer = 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(Answer);
    }

    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;
        }
        //Console.WriteLine("pCurrX={0},pCurrY={1}", pCurrX, pCurrY);

        // クリア判定
        if (pCurrX == mGoalX && pCurrY == mGoalY) {
            Answer++;
        }
        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);
    }
}
0