我正试图从起点(绿色)到终点(红色)建立一条路线,如下图所示。
我已经制定了一种随机算法来制作路线,
以及每个顶点具有连接顶点的列表。例如[a]连接到点(c,d)
path = list of points(x,y),
#path =10
s_point = get starting point(x,y)
end_Point = get end point(x,y)
for(int i=0;i<=#paths;i++){
path.add(s_point)
checked_points.add(s_point)
next_point = s_point
while(next_point.not_equals(end_point)){
_point[] = get points connected to (next_point)
while(_point[random()].not checked){
path.add(_point[random()])
next_point = _point[random()]
}
}
}
get_use_of_the_shortest_path()
所以,问题是, 如何将随机方法替换为应该将路径直接引导到终点的方法(考虑可用的不同路径)???或有关如何制作路线算法的任何信息。
答案 0 :(得分:1)
考虑到路径上没有权重(例如Google地图,其中包含有关其道路上的交通和速度限制的信息),我建议使用BFS算法,该算法可找到边缘最少的路线
Here是Java的BFS的出色实现,如Aakash Hasija所写:
import java.io.*;
import java.util.*;
// This class represents a directed graph using adjacency list
// representation
class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Adjacency Lists
// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
// Function to add an edge into the graph
void addEdge(int v,int w)
{
adj[v].add(w);
}
// prints BFS traversal from a given source s
void BFS(int s)
{
// Mark all the vertices as not visited(By default
// set as false)
boolean visited[] = new boolean[V];
// Create a queue for BFS
LinkedList<Integer> queue = new LinkedList<Integer>();
// Mark the current node as visited and enqueue it
visited[s]=true;
queue.add(s);
while (queue.size() != 0)
{
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print(s+" ");
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}
// Driver method to
public static void main(String args[])
{
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Following is Breadth First Traversal "+
"(starting from vertex 2)");
g.BFS(2);
}
}
// This code is contributed by Aakash Hasija
答案 1 :(得分:0)
您可以将距目标的曼哈顿距离用作启发式方法,以指导达到目标的路径。选择更接近的值。