C# 递归与Dijkstra';s算法

C# 递归与Dijkstra';s算法,c#,recursion,xna,dijkstra,maze,C#,Recursion,Xna,Dijkstra,Maze,我的递归算法在迷宫中导航的时间太长了。有没有关于如何加快速度以提高效率的建议?现在,它经历了所有可能的解决方案。当我试图减少这一点时,它会跳过许多解决方案,包括最短的解决方案。我如何减少解决方案的数量,或者提前结束一些解决方案,同时不跳过最短的解决方案 private static void turnsforshortestmove(Vector2 location, int[,] board, int endrow, ref Boolean done, ref int[,] BOARDCHA

我的递归算法在迷宫中导航的时间太长了。有没有关于如何加快速度以提高效率的建议?现在,它经历了所有可能的解决方案。当我试图减少这一点时,它会跳过许多解决方案,包括最短的解决方案。我如何减少解决方案的数量,或者提前结束一些解决方案,同时不跳过最短的解决方案

 private static void turnsforshortestmove(Vector2 location, int[,] board, int endrow, ref Boolean done, ref int[,] BOARDCHANGE, ref HashSet<int> h)
 //location is current location. board is the maze, endrow is the end y value to get to. it doesn't matter which x value, but as long as they get to the y value it's considered finishing.
 // done is a mistake, ignore it. BOARDCHANGE stores 
{
    //i need to compare all results for shortest
    //i need to cut off those that cant move
    if (location.Y == endrow)
    {
        h.Add(parseInt(board)); //counts how long the path is
        for (int r = 0; r < 19; r++)
            for (int c = 0; c < 19; c++)
                BOARDCHANGE[r, c] = board[r, c]; //sets the "real" board to have the path shown
    }
    else
    {

        int[,] boardCopy = new int[19, 19];
        for (int r = 0; r < 19; r++)
            for (int c = 0; c < 19; c++)
                boardCopy[r, c] = board[r, c];

        boardCopy[(int)location.X, (int)location.Y] = 8;


 //this part is saying if the square above isnt a wall, and two squares above isn't occupied, then do this function again

        if (boardCopy[(int)location.X, (int)location.Y - 1] == 1)
        {
            if (boardCopy[(int)location.X, (int)location.Y - 2] == 0)
            {
                turnsforshortestmove(new Vector2(location.X, location.Y - 2), boardCopy, endrow, ref done, ref BOARDCHANGE, ref h);
            }
        }    
        if (boardCopy[(int)location.X - 1, (int)location.Y] == 1)
        {
            if (boardCopy[(int)location.X - 2, (int)location.Y] == 0)
            {
                turnsforshortestmove(new Vector2(location.X - 2, location.Y), boardCopy, endrow, ref done, ref BOARDCHANGE, ref h);
            }
        }
        if (boardCopy[(int)location.X + 1, (int)location.Y] == 1)
        {
            if (boardCopy[(int)location.X + 2, (int)location.Y] == 0)
            {
                turnsforshortestmove(new Vector2(location.X + 2, location.Y), boardCopy, endrow, ref done, ref BOARDCHANGE, ref h);
            }
        }
        if (boardCopy[(int)location.X, (int)location.Y + 1] == 1)
        {
            if (boardCopy[(int)location.X, (int)location.Y + 2] == 0)
            {
                turnsforshortestmove(new Vector2(location.X, location.Y + 2), boardCopy, endrow, ref done, ref BOARDCHANGE, ref h);
            }
        }
    }
}
private static void turnsforshortestmove(向量2位置,int[,]板,int endrow,ref Boolean done,ref int[,]板更改,ref HashSet h)
//位置是当前位置。电路板是迷宫,endrow是要到达的末端y值。哪一个x值无关紧要,但只要它们达到y值,就被认为是完成了。
//这样做是错误的,忽略它。兑换店
{
//我需要对所有结果进行比较
//我需要切断那些不能移动的
if(location.Y==endrow)
{
h、 Add(parseInt(board));//计算路径的长度
对于(int r=0;r<19;r++)
对于(int c=0;c<19;c++)
BOARDCHANGE[r,c]=板[r,c];//设置“真实”板以显示路径
}
其他的
{
int[,]boardCopy=新int[19,19];
对于(int r=0;r<19;r++)
对于(int c=0;c<19;c++)
董事会副本[r,c]=董事会[r,c];
boardCopy[(int)location.X,(int)location.Y]=8;
//这部分是说,如果上面的正方形不是墙,上面的两个正方形没有被占用,那么再次执行此功能
if(boardCopy[(int)location.X,(int)location.Y-1]==1)
{
if(boardCopy[(int)location.X,(int)location.Y-2]==0)
{
旋转快速移动(新矢量2(位置.X,位置.Y-2),板拷贝,尾行,参考完成,参考板更改,参考h);
}
}    
if(boardCopy[(int)location.X-1,(int)location.Y]==1)
{
if(boardCopy[(int)location.X-2,(int)location.Y]==0)
{
旋转快速移动(新矢量2(位置X-2,位置Y),板拷贝,尾行,参考完成,参考板更改,参考h);
}
}
如果(boardCopy[(int)location.X+1,(int)location.Y]==1)
{
如果(boardCopy[(int)location.X+2,(int)location.Y]==0)
{
旋转快速移动(新矢量2(位置X+2,位置Y),板复制,尾行,参考完成,参考板更改,参考h);
}
}
如果(boardCopy[(int)location.X,(int)location.Y+1]==1)
{
如果(boardCopy[(int)location.X,(int)location.Y+2]==0)
{
旋转快速移动(新矢量2(位置.X,位置.Y+2),板拷贝,尾行,参考完成,参考板更改,参考h);
}
}
}
}

最后,它通过Hashset查找最短路径(number)。

在初始位置(“开始”)设置一个矩阵M,其中0和max int位于其他位置。还要创建一个职位队列

然后:


这并不特别清楚。你在这里使用Dijkstra的算法吗?@OliCharlesworth我认为他的代码现在强制解决了这个问题。这是通过基本的广度优先搜索解决的。从一个矩阵开始,初始位置为0,其他位置为max int。将初始位置放入队列中。然后,继续遍历队列,对于队列中的每个位置,如果矩阵中的非墙邻居>当前+1,则在矩阵中将邻居设置为当前+1,并将其位置添加到队列中。一旦你到达第一个位置,y=所需的y,你就可以从它回溯到一条最短的路径。因此,电路板将通过引用传递进来,这样迷宫的每个分支都将访问“主”电路板?我不明白你的意思。没有“传递”,我描述的算法不使用递归。它使用整数矩阵、位置队列和while循环。位置队列是什么意思?队列数据结构:。在C#,
队列中有一个。我所说的“位置”是指某种结构/类别,它保持电路板上单个位置的X和Y坐标。我想这就是你的
向量2
。所以您可以使用
队列
。与Java中的堆栈类似?不,队列和堆栈是相反的概念。堆栈是后进先出,队列是先进先出。使用堆栈,可以在同一端添加和获取。对于队列,您可以将其添加到一端,然后从另一端获取。
end = null
enqueue start
while queue is not empty
    p = dequeue
    if p.Y == desired_y
        end = p
        break
    for each neighbour of p // up, down, left, right
        if neighbour is not a wall and M[neighbour.X, neighbour.Y] > M[p.X, p.Y] + 1
            M[neighbour.X, neighbour.Y] = M[p.X, p.Y] + 1
            enqueue neighbour


if end == null
    return // no path exists

// now let's get the actual path, in reverse - from end to start
pos = end
path.add(pos)
while pos != start
    for each neighbour of pos
        if M[neighbour.X, neighbour.Y] == M[pos.X, pos.Y] - 1
            pos = neighbour
            path.add(pos)
            break


path.reverse // this is now your shortest path