Matlab实现A*算法代码详解:高效寻路的实战代码
```matlab function [path, cost] = AStar( start, goal, graph ) % 定义启发函数(曼哈顿距离) heuristic = @(x,y) abs(x - y); openList = {}; closedList = {}; % 初始化起始节点 startNode = struct('state', start,... 'costFromStart', 0,... 'estimatedCostToGoal', heuristic(start, goal)); % 将起始节点加入开放列表 openList{end + 1} = startNode; while ~isempty(openList) % 找到开放列表中代价最小的节点 [minCost, minIndex] = min([cellfun(@(x) x.costFromStart + x.estimatedCostToGoal, openList)]); currentNode = openList{minIndex}; % 将当前节点从开放列表移除并加入关闭列表 openList(minIndex) = []; closedList{end + 1} = currentNode; % 如果到达目标节点,返回路径和代价 if currentNode.state == goal path = reconstructPath(closedList, currentNode); cost = currentNode.costFromStart; return; end % 扩展当前节点的邻居节点 neighbors = findNeighbors(currentNode.state, graph); for i = 1:length(neighbors) neighbor = neighbors(i); % 计算到邻居节点的新代价 newCost = currentNode.costFromStart + graph(currentNode.state, neighbor); % 检查邻居节点是否在关闭列表中 foundInClosed = false; for j = 1:length(closedList) if closedList{j}.state == neighbor foundInClosed = true; break; end end if foundInClosed continue; end % 检查邻居节点是否在开放列表中 foundInOpen = false; newNode = struct('state', neighbor,... 'costFromStart', newCost,... 'estimatedCostToGoal', newCost + heuristic(neighbor, goal)); for j = 1:length(openList) if openList{j}.state == neighbor foundInOpen = true; if newCost < openList{j}.costFromStart openList{j} = newNode; end break; end end if ~foundInOpen openList{end + 1} = newNode; end end end % 如果没有找到路径,返回空路径和无穷大代价 path = []; cost = Inf; end function neighbors = findNeighbors(state, graph) neighbors = []; [row, col] = ind2sub(size(graph), state); possibleMoves = [-1 0; 1 0; 0 -1; 0 1]; for i = 1:size(possibleMoves, 1) newRow = row + possibleMoves(i, 1); newCol = col + possibleMoves(i, 2); if newRow >= 1 && newRow <= size(graph, 1) && newCol >= 1 && newCol <= size(graph, 2) neighbor = sub2ind(size(graph), newRow, newCol); neighbors = [neighbors neighbor]; end end end function path = reconstructPath(closedList, goalNode) path = [goalNode.state]; currentNode = goalNode; while isfield(currentNode, 'parent') currentNode = currentNode.parent; path = [currentNode.state path]; end end ``` ######[AI问答 | 853点数解答 | 2025-05-19 10:50:12]
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)367
- 深度揭秘 Oracle Workarea Size Policy:参数调优助力数据库性能飞升(阿里通义 | 372点数解答 | 2024-05-13 10:54:45)248
- 巴黎奥运:中国女排淘汰赛对决土耳其,朱婷成致胜关键!(字节豆包 | 448点数解答 | 2024-08-06 15:59:48)176
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)337
- 用 JS 中 for 循环实现 1 到 100 相加并输出结果到页面的完整代码 ( | 240点数解答 | 2024-05-20 22:11:29)491
- 用 JS 的 while 循环实现 1 到 100 相加并输出到页面的代码揭秘( | 47点数解答 | 2024-05-20 22:16:48)377
- Python切片操作巧解经典算法:二维数组90°旋转(阿里通义 | 205点数解答 | 2023-10-31 11:05:29)349
- 揭秘iOS开发:常用算法大集合,提升应用性能与体验!(阿里通义 | 246点数解答 | 2023-11-08 00:43:08)301
- 深入解析:Vue2.x与Vuex3.x渲染器的Diff算法奥秘(阿里通义 | 222点数解答 | 2023-11-09 01:35:41)378
- 突破 long 范围限制:用 BigInteger 类实现超大数字竖式乘法运算( | 555点数解答 | 2024-03-17 10:56:49)377
- 超范围数字乘法:字符串转数组+模拟竖式运算的实现攻略( | 377点数解答 | 2024-03-17 10:58:21)375
- Java 实现:突破 long 范围限制,模拟竖式运算处理大数字乘法( | 14点数解答 | 2024-03-17 10:58:39)354