using System;
using System.Collections.Generic;
class Program
{
internal struct JyoutaiDef
{
internal int X;
internal int Y;
internal string Path;
}
static internal Stack<JyoutaiDef> stk = new Stack<JyoutaiDef>();
static void Main()
{
var sw = System.Diagnostics.Stopwatch.StartNew();
const int MaxHaba = 5;
JyoutaiDef WillPush;
WillPush.X = 0; WillPush.Y = 0; WillPush.Path = "(0,0)";
stk.Push(WillPush);
int AnsCnt = 0;
while (stk.Count > 0) {
JyoutaiDef Popped = stk.Pop();
if (Popped.X == MaxHaba && Popped.Y == MaxHaba) {
AnsCnt++;
if (AnsCnt % 20000 == 1) {
Console.WriteLine("{0}番目の解を発見。経過時間={1}", AnsCnt, sw.Elapsed);
}
//Console.WriteLine("解{0,2}={1}", AnsCnt, Popped.Path);
continue;
}
Action<int, int> WillAct = (pX, pY) =>
{
string wk = string.Format("({0},{1})", pX, pY);
if (Popped.Path.Contains(wk) == false) {
WillPush.X = pX; WillPush.Y = pY;
WillPush.Path = Popped.Path + wk;
stk.Push(WillPush);
}
};
//右に移動
if (Popped.X < MaxHaba) WillAct(Popped.X + 1, Popped.Y);
//左に移動
if (Popped.X > 0) WillAct(Popped.X - 1, Popped.Y);
//上に移動
if (Popped.Y > 0) WillAct(Popped.X, Popped.Y - 1);
//下に移動
if (Popped.Y < MaxHaba) WillAct(Popped.X, Popped.Y + 1);
}
Console.WriteLine("解は{0}通り", AnsCnt);
}
}