我知道这并不完美,我知道它还没有完成。该板是一个19乘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;
}