如何使用 Dijkstra c++使用基于数组的版本的代码

发布于 2024-09-24 02:28:03 字数 1503 浏览 4 评论 0原文

我需要使用(不是实现)基于数组的 Dijkstras 算法版本。任务是给定一组线段(障碍物)和起点/终点,我必须找到并绘制从起点/终点开始的最短路径。已经完成了计算部分等..但不知道如何在我的代码中使用dijkstras。我的代码如下

class Point
{
public:
int x;
int y;
Point()
{

}

void CopyPoint(Point p)
{
this->x=p.x;
this->y=p.y;
}
};

class NeighbourInfo
{
public:
Point x;
Point y;
double distance;

NeighbourInfo()
{
distance=0.0;
}
};


class LineSegment
{
 public:
 Point Point1;
 Point Point2;
 NeighbourInfo neighbours[100];

 LineSegment()
 {

 }




 void main()//in this I use my classes and some code to fill out the datastructure
 {

 int NoOfSegments=i;

 for(int j=0;j<NoOfSegments;j++)
 {
 for(int k=0;k<NoOfSegments;k++)
 {
 if( SimpleIntersect(segments[j],segments[k]) )
 {
   segments[j].neighbours[k].distance=INFINITY;
   segments[j].neighbours[k].x.CopyPoint(segments[k].Point1);
   segments[j].neighbours[k].y.CopyPoint(segments[k].Point2);
   cout<<"Intersect"<<endl;
   cout<<segments[j].neighbours[k].distance<<endl;
 }
else
{
   segments[j].neighbours[k].distance=
EuclidianDistance(segments[j].Point1.x,segments[j].Point1.y,segments[k].Point2.x,segments[k    ].Point2.y);
   segments[j].neighbours[k].x.CopyPoint(segments[k].Point1);
   segments[j].neighbours[k].y.CopyPoint(segments[k].Point2);

}
}
}

}

现在我有从每个segmnets到所有其他段的距离,amd使用此数据(在neighbourinfo中)我想使用数组基于Dijkstras(限制)来追踪从起点/终点的最短路径。还有更多代码,但为了方便读者而缩短了问题

请帮助!!谢谢,请不要使用.net lib/代码,因为我正在使用仅限核心 C++..提前致谢

,但我需要基于数组的版本(严格来说..)我不打算使用任何其他实现。

I need to use (not implement) an array based version of Dijkstras algo .The task is that given a set of line segments(obstacles) and start/end points I have to find and draw the shortest path from start/end point.I have done the calculating part etc..but dont know how to use dijkstras with my code.My code is as follows

class Point
{
public:
int x;
int y;
Point()
{

}

void CopyPoint(Point p)
{
this->x=p.x;
this->y=p.y;
}
};

class NeighbourInfo
{
public:
Point x;
Point y;
double distance;

NeighbourInfo()
{
distance=0.0;
}
};


class LineSegment
{
 public:
 Point Point1;
 Point Point2;
 NeighbourInfo neighbours[100];

 LineSegment()
 {

 }




 void main()//in this I use my classes and some code to fill out the datastructure
 {

 int NoOfSegments=i;

 for(int j=0;j<NoOfSegments;j++)
 {
 for(int k=0;k<NoOfSegments;k++)
 {
 if( SimpleIntersect(segments[j],segments[k]) )
 {
   segments[j].neighbours[k].distance=INFINITY;
   segments[j].neighbours[k].x.CopyPoint(segments[k].Point1);
   segments[j].neighbours[k].y.CopyPoint(segments[k].Point2);
   cout<<"Intersect"<<endl;
   cout<<segments[j].neighbours[k].distance<<endl;
 }
else
{
   segments[j].neighbours[k].distance=
EuclidianDistance(segments[j].Point1.x,segments[j].Point1.y,segments[k].Point2.x,segments[k    ].Point2.y);
   segments[j].neighbours[k].x.CopyPoint(segments[k].Point1);
   segments[j].neighbours[k].y.CopyPoint(segments[k].Point2);

}
}
}

}

Now I have the distances from each segmnets to all other segments, amd using this data(in neighbourinfo) I want to use array based Dijkstras(restriction ) to trace out the shortest path from start/end points.There is more code , but have shortened the problem for the ease of the reader

Please Help!!Thanks and plz no .net lib/code as I am using core C++ only..Thanks in advance

But I need the array based version(strictly..) I am not suppose to use any other implemntation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2024-10-01 02:28:03

Dijkstra

这就是 Dijkstra 的工作原理:
它不是一个简单的算法。因此,您必须将此算法映射到您自己的代码中。
但祝你好运。

List<Nodes>    found;     // All processed nodes;
List<Nodes>    front;     // All nodes that have been reached (but not processed)
                          // This list is sorted by the cost of getting to this node.
List<Nodes>    remaining; // All nodes that have not been explored.

remaining.remove(startNode);
front.add(startNode);
startNode.setCost(0); // Cost nothing to get to start.

while(!front.empty())
{
    Node       current = front.getFirstNode();
    front.remove(current);
    found.add(current);

    if (current == endNode)
    {    return current.cost(); // we found the end
    }

    List<Edge> edges   = current.getEdges();

    for(loop = edges.begin(); loop != edges.end(); ++loop)
    {
        Node   dst = edge.getDst();
        if (found.find(dst) != found.end())
        {   continue; // If we have already processed this node ignore it.
        }


        // The cost to get here. Is the cost to get to the last node.
        // Plus the cost to traverse the edge.
        int    cost = current.cost() + loop.cost();
        Node   f    = front.find(dst);
        if (f != front.end())
        {
            f.setCost(std::min(f.cost(), cost));
            continue;  // If the node is on the front line just update the cost
                       // Then continue with the next node.
        }

        // Its a new node.
        // remove it from the remaining and add it to the front (setting the cost).
        remaining.remove(dst);
        front.add(dst);
        dst.setCost(cost);
    }
}

Dijkstras

This is how Dijkstra's works:
Its not a simple algorithm. So you will have to map this algorithm to your own code.
But good luck.

List<Nodes>    found;     // All processed nodes;
List<Nodes>    front;     // All nodes that have been reached (but not processed)
                          // This list is sorted by the cost of getting to this node.
List<Nodes>    remaining; // All nodes that have not been explored.

remaining.remove(startNode);
front.add(startNode);
startNode.setCost(0); // Cost nothing to get to start.

while(!front.empty())
{
    Node       current = front.getFirstNode();
    front.remove(current);
    found.add(current);

    if (current == endNode)
    {    return current.cost(); // we found the end
    }

    List<Edge> edges   = current.getEdges();

    for(loop = edges.begin(); loop != edges.end(); ++loop)
    {
        Node   dst = edge.getDst();
        if (found.find(dst) != found.end())
        {   continue; // If we have already processed this node ignore it.
        }


        // The cost to get here. Is the cost to get to the last node.
        // Plus the cost to traverse the edge.
        int    cost = current.cost() + loop.cost();
        Node   f    = front.find(dst);
        if (f != front.end())
        {
            f.setCost(std::min(f.cost(), cost));
            continue;  // If the node is on the front line just update the cost
                       // Then continue with the next node.
        }

        // Its a new node.
        // remove it from the remaining and add it to the front (setting the cost).
        remaining.remove(dst);
        front.add(dst);
        dst.setCost(cost);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文