做网站的公司好坑啊金融网站开发文档下载
做网站的公司好坑啊,金融网站开发文档下载,网络课程网站模板,网站域名怎么缴费A*算法和Dijkstra算法是两种常用的路径搜索算法#xff0c;用于在图形结构中寻找最短路径。它们都属于单源最短路径算法#xff0c;可以用于解决各种寻路问题。 A算法是一种启发式搜索算法#xff0c;同时考虑了实际移动代价和估计距离代价#xff0c;通过估计代价来指导搜… A*算法和Dijkstra算法是两种常用的路径搜索算法用于在图形结构中寻找最短路径。它们都属于单源最短路径算法可以用于解决各种寻路问题。 A算法是一种启发式搜索算法同时考虑了实际移动代价和估计距离代价通过估计代价来指导搜索方向并选择最优的路径。A算法通过估价函数值 f(n) g(n) h(n) 来评估节点的优先级其中 g(n) 是实际移动代价h(n) 是从当前节点到目标节点的估计代价。A算法使用优先队列通常是最小堆来管理待处理的节点每次选择具有最小 f(n) 值的节点进行扩展。A算法在最优条件下即 h(n) 函数是准确的保证能够找到最短路径。 Dijkstra算法是一种经典的图搜索算法通过不断选择最短路径的节点进行扩展逐步确定从起点到达其他节点的最短路径。Dijkstra算法使用一个距离数组来记录起始节点到每个节点的最短距离通过选择当前距离最小的节点进行扩展更新与该节点相邻节点的距离值。Dijkstra算法在没有负权边的情况下保证能够找到最短路径。 总结A*算法通过使用估计函数来指导搜索方向具有较高的效率和准确性特别适用于路径搜索问题。Dijkstra算法是一种经典的最短路径算法适用于图中存在负权边但不能有负权环的场景。根据实际情况选择合适的算法可以高效地寻找最短路径。
A*路径搜索算法的完整代码
using System;
using System.Collections.Generic;public class AStarPathfinding
{// 定义节点类public class Node{public int x;public int y;public bool isWalkable;public ListNode neighbors;public Node parent;public int gCost;public int hCost;public Node(int x, int y, bool isWalkable){this.x x;this.y y;this.isWalkable isWalkable;neighbors new ListNode();}public int fCost { get { return gCost hCost; } }}// 寻找路径public static ListNode FindPath(Node startNode, Node endNode){ListNode openSet new ListNode();HashSetNode closedSet new HashSetNode();openSet.Add(startNode);while (openSet.Count 0){Node currentNode openSet[0];for (int i 1; i openSet.Count; i){if (openSet[i].fCost currentNode.fCost || openSet[i].fCost currentNode.fCost openSet[i].hCost currentNode.hCost){currentNode openSet[i];}}openSet.Remove(currentNode);closedSet.Add(currentNode);if (currentNode endNode){return GeneratePath(startNode, endNode);}foreach (Node neighbor in currentNode.neighbors){if (!neighbor.isWalkable || closedSet.Contains(neighbor)){continue;}int newCostToNeighbor currentNode.gCost GetDistance(currentNode, neighbor);if (newCostToNeighbor neighbor.gCost || !openSet.Contains(neighbor)){neighbor.gCost newCostToNeighbor;neighbor.hCost GetDistance(neighbor, endNode);neighbor.parent currentNode;if (!openSet.Contains(neighbor)){openSet.Add(neighbor);}}}}return null;}// 计算两个节点之间的距离public static int GetDistance(Node nodeA, Node nodeB){int distanceX Math.Abs(nodeA.x - nodeB.x);int distanceY Math.Abs(nodeA.y - nodeB.y);return distanceX distanceY;}// 生成路径public static ListNode GeneratePath(Node startNode, Node endNode){ListNode path new ListNode();Node currentNode endNode;while (currentNode ! startNode){path.Add(currentNode);currentNode currentNode.parent;}path.Reverse();return path;}// 使用示例static void Main(string[] args){// 创建地图Node[,] map new Node[10, 10];for (int i 0; i 10; i){for (int j 0; j 10; j){bool isWalkable true; // 是否可以行走根据实际情况设置map[i, j] new Node(i, j, isWalkable);}}// 设置邻居节点for (int i 0; i 10; i){for (int j 0; j 10; j){Node node map[i, j];if (i 0){node.neighbors.Add(map[i - 1, j]);}if (i 9){node.neighbors.Add(map[i 1, j]);}if (j 0){node.neighbors.Add(map[i, j - 1]);}if (j 9){node.neighbors.Add(map[i, j 1]);}}}// 测试寻路Node startNode map[0, 0];Node endNode map[9, 9];ListNode path FindPath(startNode, endNode);if (path ! null){Console.WriteLine(Path Found:);foreach (Node node in path){Console.WriteLine(( node.x , node.y ));}}else{Console.WriteLine(No Path Found.);}}
}
using System; using System.Collections.Generic;
public class AStarPathfinding { // 定义节点类 public class Node { public int x; public int y; public bool isWalkable; public ListNode neighbors; public Node parent; public int gCost; public int hCost; public Node(int x, int y, bool isWalkable) { this.x x; this.y y; this.isWalkable isWalkable; neighbors new ListNode(); } public int fCost { get { return gCost hCost; } } } // 寻找路径 public static ListNode FindPath(Node startNode, Node endNode) { ListNode openSet new ListNode(); HashSetNode closedSet new HashSetNode(); openSet.Add(startNode); while (openSet.Count 0) { Node currentNode openSet[0]; for (int i 1; i openSet.Count; i) { if (openSet[i].fCost currentNode.fCost || openSet[i].fCost currentNode.fCost openSet[i].hCost currentNode.hCost) { currentNode openSet[i]; } } openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode endNode) { return GeneratePath(startNode, endNode); } foreach (Node neighbor in currentNode.neighbors) { if (!neighbor.isWalkable || closedSet.Contains(neighbor)) { continue; } int newCostToNeighbor currentNode.gCost GetDistance(currentNode, neighbor); if (newCostToNeighbor neighbor.gCost || !openSet.Contains(neighbor)) { neighbor.gCost newCostToNeighbor; neighbor.hCost GetDistance(neighbor, endNode); neighbor.parent currentNode; if (!openSet.Contains(neighbor)) { openSet.Add(neighbor); } } } } return null; } // 计算两个节点之间的距离 public static int GetDistance(Node nodeA, Node nodeB) { int distanceX Math.Abs(nodeA.x - nodeB.x); int distanceY Math.Abs(nodeA.y - nodeB.y); return distanceX distanceY; } // 生成路径 public static ListNode GeneratePath(Node startNode, Node endNode) { ListNode path new ListNode(); Node currentNode endNode; while (currentNode ! startNode) { path.Add(currentNode); currentNode currentNode.parent; } path.Reverse(); return path; } // 使用示例 static void Main(string[] args) { // 创建地图 Node[,] map new Node[10, 10]; for (int i 0; i 10; i) { for (int j 0; j 10; j) { bool isWalkable true; // 是否可以行走根据实际情况设置 map[i, j] new Node(i, j, isWalkable); } } // 设置邻居节点 for (int i 0; i 10; i) { for (int j 0; j 10; j) { Node node map[i, j]; if (i 0) { node.neighbors.Add(map[i - 1, j]); } if (i 9) { node.neighbors.Add(map[i 1, j]); } if (j 0) { node.neighbors.Add(map[i, j - 1]); } if (j 9) { node.neighbors.Add(map[i, j 1]); } } } // 测试寻路 Node startNode map[0, 0]; Node endNode map[9, 9]; ListNode path FindPath(startNode, endNode); if (path ! null) { Console.WriteLine(Path Found:); foreach (Node node in path) { Console.WriteLine(( node.x , node.y )); } } else { Console.WriteLine(No Path Found.); } } } 在示例代码中我们定义了一个 AStarPathfinding 类实现A*路径搜索的相关逻辑。代码中包含了一个 Node 类用于表示节点对象以及 FindPath 函数用于寻找路径GetDistance 函数用于计算两个节点之间的距离GeneratePath 函数用于生成路径。 在 Main 函数中我们首先创建了一个 10x10 的地图设置各个节点的可行走状态并为每个节点设置邻居节点。然后我们以 (0, 0) 作为起始节点(9, 9) 作为目标节点调用 FindPath 函数寻找路径。如果找到了路径则输出路径上的节点坐标否则输出 No Path Found.。 请注意这只是一个简单的示例实际的路径搜索算法根据具体的场景和需求可能需要更复杂的实现。
Dijkstra算法的完整代码
using System;
using System.Collections.Generic;public class DijkstraAlgorithm
{// 定义节点类public class Node{public int id;public int distance;public bool visited;public ListEdge edges;public Node(int id){this.id id;this.distance int.MaxValue;this.visited false;this.edges new ListEdge();}}// 定义边类public class Edge{public Node source;public Node destination;public int weight;public Edge(Node source, Node destination, int weight){this.source source;this.destination destination;this.weight weight;}}// Dijkstra算法public static void Dijkstra(Node startNode){startNode.distance 0;PriorityQueueNode queue new PriorityQueueNode();queue.Enqueue(startNode);while (queue.Count 0){Node currentNode queue.Dequeue();currentNode.visited true;foreach (Edge edge in currentNode.edges){Node neighborNode edge.destination;int distance currentNode.distance edge.weight;if (distance neighborNode.distance){neighborNode.distance distance;if (!neighborNode.visited){queue.Enqueue(neighborNode);}}}}}// 示例用法public static void Main(){// 创建节点Node nodeA new Node(0);Node nodeB new Node(1);Node nodeC new Node(2);Node nodeD new Node(3);// 创建边Edge edgeAB new Edge(nodeA, nodeB, 2);Edge edgeAC new Edge(nodeA, nodeC, 4);Edge edgeBC new Edge(nodeB, nodeC, 1);Edge edgeCD new Edge(nodeC, nodeD, 3);// 添加边到节点的边列表nodeA.edges.Add(edgeAB);nodeA.edges.Add(edgeAC);nodeB.edges.Add(edgeBC);nodeC.edges.Add(edgeCD);// 运行Dijkstra算法Dijkstra(nodeA);// 输出最短距离Console.WriteLine(Shortest distance from nodeA to nodeD: nodeD.distance);}
} 这个代码示例创建了一组示例节点和边然后运行Dijkstra算法来找到从起点到终点的最短路径。在这个示例中节点使用id来唯一标识边包含源节点目标节点和权重。Dijkstra算法使用优先队列来管理待处理的节点通过不断选择最短距离的节点进行扩展更新相邻节点的距离值。最后输出从起点到终点的最短距离。 请注意这只是一个简单的示例实际使用时可能需要根据具体情况进行修改和扩展。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/92713.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!