diff --git a/astar/astar.cpp b/astar/astar.cpp index c7c1678..839d58a 100644 --- a/astar/astar.cpp +++ b/astar/astar.cpp @@ -102,22 +102,23 @@ bool AStar::get_node_index(Node *node, size_t *index) } // 二叉堆上滤 -void AStar::percolate_up(size_t hole) +void AStar::percolate_up(size_t hole, Node *node) { size_t parent = 0; while (hole > 0) { parent = (hole - 1) / 2; - if (open_list_[hole]->f() < open_list_[parent]->f()) + if (node->f() < open_list_[parent]->f()) { - std::swap(open_list_[hole], open_list_[parent]); + open_list_[hole] = open_list_[parent]; hole = parent; } else { - return; + break; } } + open_list_[hole] = node; } // 计算G值 @@ -224,7 +225,7 @@ void AStar::handle_found_node(Node *current, Node *destination) size_t index = 0; if (get_node_index(destination, &index)) { - percolate_up(index); + percolate_up(index, destination); } else { diff --git a/astar/astar.h b/astar/astar.h index e17f61e..6877997 100644 --- a/astar/astar.h +++ b/astar/astar.h @@ -150,7 +150,7 @@ class AStar /** * 二叉堆上滤 */ - void percolate_up(size_t hole); + void percolate_up(size_t hole, Node *node); /** * 获取节点索引