Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 寻路码的优化_C#_Linq_Iteration - Fatal编程技术网

C# 寻路码的优化

C# 寻路码的优化,c#,linq,iteration,C#,Linq,Iteration,先讲一点背景知识。我正在开发一个系统,在不同地点之间生成一条“路线”。各个地点都有一个预定义的邻居列表,不限于与其相邻的邻居。搜索可以安全地假设,通过选择距离目标目的地最近的邻居(数字),它正在朝着目标地进行最佳移动 我的工作代码如下所示: public Route GetRoute(int StartPoint, int Destination) { Route returnRoute = new Route(); returnRoute.steps = new List<

先讲一点背景知识。我正在开发一个系统,在不同地点之间生成一条“路线”。各个地点都有一个预定义的邻居列表,不限于与其相邻的邻居。搜索可以安全地假设,通过选择距离目标目的地最近的邻居(数字),它正在朝着目标地进行最佳移动

我的工作代码如下所示:

public Route GetRoute(int StartPoint, int Destination)
{
    Route returnRoute = new Route();
    returnRoute.steps = new List<int>();
    bool locationReached = false;
    int selectedNeighbour;
    int distanceFromTarget;
    int currentPoint = StartPoint; // set the current point to the start point

    while (!locationReached)
    {
        selectedNeighbour = 0;
        distanceFromTarget = 5000; // nominal amount guaranteed to be overwritten

        var neighbours = locations.FirstOrDefault(l => l.LocationID == currentPoint).Neighbours;

        for (int i = 0; i < neighbours.Length; i++)
        {
            // get the current neighbours, then check proximity
            int currentNeighbour = neighbours[i];
            int tempDistance = Math.Abs( currentNeighbour - Destination );

            // if nearer than previous neighbour, set it as the chosen location
            if ( tempDistance < distanceFromTarget )
            {
                distanceFromTarget = tempDistance;
                selectedNeighbour = currentNeighbour;

                // if the selected neighbour is the destination, we're done
                if ( selectedNeighbour == Destination )
                    locationReached = true;
            }

        } // for

        // add the selected neighbour if we found one
        if ( selectedNeighbour != 0 )
        {
            currentPoint = selectedNeighbour;
            returnRoute.steps.Add(selectedNeighbour);
        }
        else 
        {
            Debug.Log ("No Route Found");
            return returnRoute; 
        }

    } // while

    return returnRoute;
}
public Route GetRoute(int起始点,int目的地)
{
路线返回路线=新路线();
returnRoute.steps=新列表();
bool locationReached=false;
int选择邻居;
int距离目标;
int currentPoint=StartPoint;//将当前点设置为起点
而(!LocationReach)
{
selectednexter=0;
distanceFromTarget=5000;//保证覆盖的名义金额
var neights=locations.FirstOrDefault(l=>l.LocationID==currentPoint);
for(int i=0;i<0.Length;i++)
{
//获取当前邻居,然后检查是否接近
int currentNeighbor=邻居[i];
int tempdestance=Math.Abs(当前邻居-目的地);
//如果比前一个邻居更近,则将其设置为所选位置
if(tempDistance<距离目标)
{
距离目标=tempDistance;
SelectedNeighbor=CurrentNeighbor;
//如果选定的邻居是目的地,我们就完成了
如果(SelectedNeighbor==目的地)
位置达到=真;
}
}//为了
//如果找到一个邻居,请添加选定的邻居
如果(SelectedNeighbor!=0)
{
currentPoint=所选邻居;
returnRoute.steps.Add(SelectedNeighbor);
}
其他的
{
Debug.Log(“未找到路由”);
返回路线;
}
}//而
返回路线;
}
我的问题是关于邻居(int[])变量的循环。如何最好地优化这一点?我见过linq和排序的一些用法,但也有评论说这种方法可能效率低下。我需要效率而不是整洁


非常感谢。

使用LINQ代替基本循环几乎总是强调代码的清晰性。如果您的代码正常工作,那么似乎没有什么理由切换到LINQ。如果您想要任何类型的优化,请尝试查看Dijkstra的算法: