using System; using System.Collections.Generic; using System.Linq; // https://yukicoder.me/problems/no/2928 class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); 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 mVisitedSet = new HashSet(); static void Main() { List InputList = GetInputList(); int[] wkArr = { }; Action 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 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); } }