私はこれが完璧ではないことを知っています。ボードは 19 x 19 の配列で、1 は空の正方形を表します。今は真っ直ぐ下って西へ。その左側に壁がある場合、スタック オーバーフローが発生します。その理由は、壁を「登ろう」とすると、何度も何度も下に戻ってクラッシュするからです。ただし、これを修正しても、最短パスは見つかりません。私が見つけた解決策は、パスがいくつ離れているかを数えるのではなく、パスを描きます。
private static int turnsforshortestmove(Vector2 location, int[,] board, int endrow)
{
if (location.Y == endrow)
{
return 0;
}
if (board[(int)location.X, (int)location.Y - 1] == 1)
{
return 1 + turnsforshortestmove(new Vector2(location.X, location.Y - 2), board, endrow);
}
else if (board[(int)location.X - 1, (int)location.Y] == 1)
{
return 1 + turnsforshortestmove(new Vector2(location.X - 2, location.Y), board, endrow);
}
else if (board[(int)location.X, (int)location.Y + 1] == 1)
{
return 1 + turnsforshortestmove(new Vector2(location.X, location.Y + 2), board, endrow);
}
else if (board[(int)location.X + 1, (int)location.Y ] == 1)
{
return 1 + turnsforshortestmove(new Vector2(location.X + 2, location.Y), board, endrow);
}
return 0;
}