我知道这并不完美,而且我知道它还没有完成。棋盘是一个 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;
}