Bstar 算法
Bstart 算法原理: 一般寻路的方向 上、下、左、右
1、根据开始和结束2个点确定移动的方向
2、每走一步都需要确定一下方向
3、如果中途遇到有障碍物,根据当前位置和终点的位置,确定移动方向,变成2条线路
4、这2 条线交替向终点方向移动
5、如果中途又碰见障碍物了,继续分2个线,此时四条线交替移动
6、最先到终点的就是最终的路线;
/**************************************************************************/
/下面的是伪代码,而且还没有完成,主要原因是,不知道该用 树 的哪个结构来存储每个路线,希望对这里比较熟悉的大佬们可以 不吝赐教 ; 小白在这里先谢过各位大神啦!!
在搞这块的老铁们,有什么见解 咱一起盘他
/**************************************************************************/
1 // 上下左右 2 enum Dir { 3 NONE, 4 UP, 5 DOWN; 6 RIGHT, 7 LEFT 8 } 9 10 // 伪代码 11 struct Point { 12 int x = 0; 13 int y = 0; 14 bool visit = false; 15 Dir dir = NONE; 16 }; 17 18 19 // 确定移动时的 方向(上下左右) 20 void move_dir(Point cur_point, const Point& end_point) { 21 auto dis_x = | cur_point.x - end_point.x | ; //取绝对值 22 auto dis_y = | cur_point.y - end_point.y | ; //取绝对值 23 if (dis_x >= dis_y) { 24 if (cur_point.x >= end_point.x) { 25 cur_point.dir = Dir::LEFT; 26 } else { 27 cur_point.dir = Dir::RIGHT; 28 } 29 } else { 30 if (cur_point.y >= end_point.y) { 31 cur_point.dir = Dir::DOWN; 32 } else { 33 cur_point.dir = Dir::UP; 34 } 35 } 36 cur_point.visit = true; 37 38 } 39 40 // 是否可走 41 bool check_can_move(const Point& next_point) { 42 // 判断障碍物列表中 是否有next_point 43 if (有) { 44 return false; 45 } 46 return true; 47 } 48 49 //不可走,重新设置方向,以及分线走 50 51 void split_cur_dir(const Point& next_point) { 52 auto path_size = paths_.size(); 53 paths_[2 * (path_size - 1) + 1] = next_point; 54 } 55 56 57 void main() { 58 Point start_point(3, 8); 59 Point end_point(10, 30); 60 Point cur_point_ = start_point; // 当前点(坐标) 61 move_dir(cur_point_, end_point); 62 vector<Point> paths_; // 路线(当做 二叉堆用) 63 paths_.push_back(cur_point_); 64 65 Point next_point_; 66 67 while(true) { 68 auto iter = paths_.end--; 69 if (iter->x == end_point.x && iter->start_point.y) { 70 //find path done, break while 71 break; 72 } else { 73 bool change_dir = false; 74 switch(cur_point_.dir) { 75 case Dir::RIGHT: { 76 next_point_ = cur_point_; 77 next_point_.x = cur_point_.x + 1; 78 change_dir = true; 79 break; 80 } 81 case Dir::LEFT: { 82 next_point_ = cur_point_; 83 next_point_.x = cur_point_.x - 1; 84 change_dir = true; 85 break; 86 } 87 case Dir::UP: { 88 next_point_ = cur_point_; 89 next_point_.y = cur_point_.y + 1; 90 change_dir = true; 91 break; 92 } 93 case Dir::DOWN: { 94 next_point_ = cur_point_; 95 next_point_.y = cur_point_.y - 1; 96 change_dir = true; 97 break; 98 } 99 default: 100 break; 101 } 102 // 判断是否有障碍物 103 if (check_can_move(next_point_)) { 104 cur_point_ = next_point_; 105 if (change_dir) { 106 move_dir(cur_point_, end_point); 107 } 108 paths_.push_back(cur_point_); 109 } else { 110 next_point_ = cur_point_; 111 split_cur_dir(next_point_); 112 } 113 } 114 } 115 116 // 最后的paths_ 就是路线 117 // cout << paths_ << endl; 118 }