backtrace 获取函数信息

backtrace通过当前栈的信息,逐步回溯到崩溃地方

其实完全可以 实现,通过第一个变量的偏移实现,这种方法有个缺点,就是VC可能不能通用,GCC可以。

还有种方法是通知函数的参数,这种方法叫绝。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用曼哈顿距离作为导航函数的Matlab代码示例: ```matlab function [path, cost] = Astar(start, goal, obstacles) % 输入参数: % start - 起点坐标 % goal - 终点坐标 % obstacles - 障碍物二维数组,1表示障碍物,0表示可通过 % 输出参数: % path - 路径数组,每个元素为一个坐标点 % cost - 路径代价 % 计算曼哈顿距离 h = @(x,y) abs(x-goal(1)) + abs(y-goal(2)); % 初始化起点和终点节点 start_node = struct('pos', start, 'g', 0, 'h', h(start(1), start(2)), 'parent', []); goal_node = struct('pos', goal, 'g', Inf, 'h', 0, 'parent', []); % 初始化open和closed列表 open_list = [start_node]; closed_list = []; while ~isempty(open_list) % 从open列表中找到f值最小的节点 [min_f, min_idx] = min([open_list.g] + [open_list.h]); current_node = open_list(min_idx); % 如果当前节点是终点,则返回路径 if isequal(current_node.pos, goal_node.pos) path = backtrack_path(current_node); cost = current_node.g; return; end % 将当前节点从open列表中移除,并添加到closed列表中 open_list(min_idx) = []; closed_list(end+1) = current_node; % 扩展当前节点的邻居节点 neighbors = get_neighbors(current_node, obstacles); for i = 1:length(neighbors) neighbor = neighbors(i); % 如果邻居节点已经在closed列表中,则跳过 if ismember(neighbor, closed_list) continue; end % 计算邻居节点的g值 new_g = current_node.g + 1; % 如果邻居节点已经在open列表中,则更新g值和parent open_idx = find(ismember(open_list, neighbor)); if ~isempty(open_idx) if new_g < open_list(open_idx).g open_list(open_idx).g = new_g; open_list(open_idx).parent = current_node; end % 否则添加邻居节点到open列表中 else neighbor.g = new_g; neighbor.h = h(neighbor.pos(1), neighbor.pos(2)); neighbor.parent = current_node; open_list(end+1) = neighbor; end end end % 如果open列表已经空了,但是仍然没有找到路径,则返回空 path = []; cost = Inf; end % 获取当前节点的邻居节点 function neighbors = get_neighbors(node, obstacles) x = node.pos(1); y = node.pos(2); neighbors = []; if x > 1 && ~obstacles(x-1, y) neighbors(end+1) = struct('pos', [x-1 y], 'g', Inf, 'h', 0, 'parent', []); end if x < size(obstacles, 1) && ~obstacles(x+1, y) neighbors(end+1) = struct('pos', [x+1 y], 'g', Inf, 'h', 0, 'parent', []); end if y > 1 && ~obstacles(x, y-1) neighbors(end+1) = struct('pos', [x y-1], 'g', Inf, 'h', 0, 'parent', []); end if y < size(obstacles, 2) && ~obstacles(x, y+1) neighbors(end+1) = struct('pos', [x y+1], 'g', Inf, 'h', 0, 'parent', []); end end % 回溯路径 function path = backtrack_path(node) path = [node.pos]; while ~isempty(node.parent) node = node.parent; path = [node.pos; path]; end end ``` 上述代码实现了一个基于A*算法的路径规划器,其中使用曼哈顿距离作为导航函数。该代码可以在二维障碍物地图上进行路径规划,返回一条从起点到终点的最短路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值