当前位置:   article > 正文

【C++】A*最优寻路算法(附加迷宫障碍自动生成)_迷宫寻路c++

迷宫寻路c++

目录

一、前言

二、关于迷宫(障碍)的生成

1.简单解释

 2.代码

三、A*算法

1.简单解释

 2.效果演示

3.代码

四、总代码

五、总结


一、前言

        最近上了人工智能的课程,学了A*算法,就尝试使用C++来实现这个算法。

        为了更方便的体现寻路过程,这里使用了easyx图像库来实时展示运动的过程。可以去easyx的官网下载这个库。easyx官网

        easyx库在使用中最好在:调试——调试属性——配置属性——高级——字符集        设置使用多字节字符集,否则有些函数可能会报错。

二、关于迷宫(障碍)的生成

1.简单解释

        首先我们可以用一个二维数组来存放迷宫,其大小由用户输入。用1表示墙壁,0表示路。

        开始初始化迷宫数组应该初始化为下面的状态。

        

         数组(1,1)为开始点,然后开始寻找旁边是否有通路,如果有,则随机打通一路,然后记录下当前的坐标,进行递归。函数出口就是没有找到可以打通的地方,则直接return。走过的路需要标记为走过,否则可能会出现死循环导致程序不正确。这里标记走过的路为5。

        进行完这一步后,需要将之前数组中所有的5改为0,即改为通路。

        最后将其显示,大概就是下面这个效果,当然这种算法生成的迷宫质量不是很好,不过现在也足够使用了。

           

        当然我们使用A*算法不需要迷宫,只需要一些障碍就行,我们再写一个函数,随机删除上面迷宫的一些墙,就完成生成障碍的这一步了。

         

 2.代码

存放迷宫的结构体

  1. struct AStar {
  2. int M; //M*M map
  3. int** maze; //map迷宫
  4. //int* close; //close表
  5. //int* open; //open表
  6. int O_row; //开始的行
  7. int O_col; //开始的列
  8. int E_row; //终点行
  9. int E_col; //终点列
  10. }Astar{ 0,NULL,1,1,0,0 };

初始化迷宫

  1. int main()
  2. {
  3. Astar.M = 0;
  4. cout << "请输入迷宫是几乘几的" << endl;
  5. cin >> Astar.M;
  6. if (Astar.M % 2 == 0)//如果输入不为奇数变为奇数
  7. Astar.M++;
  8. Astar.E_col = Astar.M;
  9. Astar.E_row = Astar.M;
  10. Astar.maze = new int* [Astar.M + 2];//动态创建二维数组
  11. for (int i = 0; i < Astar.M + 2; i++)
  12. Astar.maze[i] = new int[Astar.M + 2];//每一行申请一个int空间的M+2列空间
  13. }
  14. //初始化迷宫
  15. void Init_Maze(AStar& Astar)
  16. {
  17. for (int i = 0; i < Astar.M + 2; i++)//动态创建迷宫
  18. for (int j = 0; j < Astar.M + 2; j++)
  19. {
  20. if (i == 0 || i == Astar.M + 1)//1为墙壁
  21. Astar.maze[i][j] = 1;
  22. if ((i % 2 == 1) && (j % 2 == 1))
  23. Astar.maze[i][j] = 0;
  24. else Astar.maze[i][j] = 1;
  25. }
  26. }

寻找邻居

  1. bool findneighbor(AStar& Astar, int x_index, int y_index)
  2. {
  3. if ((x_index >= 3 && Astar.maze[x_index - 2][y_index] == 0) || (x_index <= Astar.M - 1 && Astar.maze[x_index + 2][y_index] == 0)
  4. || (y_index >= 3 && Astar.maze[x_index][y_index - 2] == 0) || (y_index <= Astar.M - 1 && Astar.maze[x_index][y_index + 2] == 0))
  5. return 1;
  6. else
  7. return 0;
  8. }

创建迷宫

  1. //随机创建迷宫
  2. void creatMaze(AStar& Astar, int x_index, int y_index)
  3. {
  4. int pos, x, y, flag = 0;
  5. x = x_index;
  6. y = y_index;
  7. while (1)
  8. {
  9. flag = 0;
  10. flag = findneighbor(Astar, x, y);
  11. if (!flag)
  12. return;
  13. else {
  14. Astar.maze[x_index][y_index] = 5;
  15. x = x_index;
  16. y = y_index;
  17. while (1)
  18. {
  19. pos = rand() % (4 - 1 + 1) + 1;
  20. if (pos == 1 && x_index >= 3 && Astar.maze[x_index - 2][y_index] == 0)//上
  21. {
  22. x_index -= 2;
  23. }
  24. else if (pos == 2 && x_index <= Astar.M - 1 && Astar.maze[x_index + 2][y_index] == 0)//下
  25. {
  26. x_index += 2;
  27. }
  28. else if (pos == 3 && y_index <= Astar.M - 1 && Astar.maze[x_index][y_index + 2] == 0)
  29. {
  30. y_index += 2;
  31. }
  32. else if (pos == 4 && y_index >= 3 && Astar.maze[x_index][y_index - 2] == 0)
  33. {
  34. y_index -= 2;
  35. }
  36. Astar.maze[(x + x_index) / 2][(y + y_index) / 2] = 5;
  37. Astar.maze[x_index][y_index] = 5;
  38. // showmaze(maze, M);
  39. creatMaze(Astar, x_index, y_index);
  40. break;
  41. }
  42. }
  43. }
  44. }

 改变数组中的通路,标识入口和出口

  1. //给创建的迷宫创建标识
  2. void makeMaze(AStar& Astar)
  3. {
  4. for (int i = 0; i < Astar.M + 2; i++)
  5. for (int j = 0; j < Astar.M + 2; j++)
  6. {
  7. if (Astar.maze[i][j] == 5)
  8. {
  9. Astar.maze[i][j] = 0;
  10. }
  11. }
  12. Astar.maze[1][1] = 8;
  13. Astar.maze[Astar.M][Astar.M] = 2;
  14. }

 改变地形

  1. //改变地形,出现更多通路
  2. void MakeDifficult(AStar& Astar)
  3. {
  4. if (Astar.M > 5)
  5. {
  6. int half = (int)((0.4 * Astar.M) * (0.5 * Astar.M));
  7. int x_c, y_c = 0;
  8. for (int i = 0; i < half;)
  9. {
  10. x_c = rand() % (Astar.M - 1 + 1) + 1;
  11. y_c = rand() % (Astar.M - 1 + 1) + 1;
  12. if (Astar.maze[x_c][y_c] == 1)
  13. {
  14. Astar.maze[x_c][y_c] = 0;
  15. i++;
  16. }
  17. }
  18. }
  19. }

显示迷宫

  1. //图像显示迷宫
  2. //3为路,4为搜寻的路,1为墙,2为终点,8为开始
  3. void show(AStar& Astar)
  4. {
  5. IMAGE star, wall, ball ,ly;
  6. loadimage(&star, "./五角星.png", SIZE, SIZE);
  7. loadimage(&wall, "./墙.png", SIZE, SIZE);
  8. loadimage(&ball, "./球.png", SIZE, SIZE);
  9. loadimage(&ly, "./兰音.png", SIZE, SIZE);
  10. for (int i = 0; i < Astar.M + 2; i++)
  11. {
  12. for (int j = 0; j < Astar.M + 2; j++)
  13. {
  14. if (Astar.maze[i][j] == 1)
  15. {
  16. putimage((j + Astar.M + 2 + 1) * SIZE, (i)*SIZE, &wall);
  17. putimage(j * SIZE, i * SIZE, &wall);
  18. }
  19. else if (Astar.maze[i][j] == 4)
  20. {
  21. putimage((j + Astar.M + 2 + 1) * SIZE, (i)*SIZE, &ball);
  22. }
  23. else if (Astar.maze[i][j] == 3)
  24. {
  25. putimage(j * SIZE, (i)*SIZE, &ly);
  26. }
  27. }
  28. }
  29. putimage((Astar.M + 2 + 1 + 1) * SIZE, (SIZE), &star);
  30. putimage((Astar.M + Astar.M + 2 + 1) * SIZE, (Astar.M) * SIZE, &star);
  31. putimage(SIZE, SIZE, &star);
  32. putimage(Astar.M * SIZE, Astar.M * SIZE, &star);
  33. }

至此,迷宫或者障碍的创建就完成了,此时的迷宫保存在maze这个二维数组中。

三、A*算法

1.简单解释

        我们假设一开始是下面这幅图,起点为左上角,终点为右下角。A*算法的重点就是我们设置估价函数F的过程。

        F=G+H。其中G是起始节点到当前节点的代价,H是当前节点到目标节点的代价。A*算法就是在A算法上的改进,也就是要求G必须大于0,H必须不大于实际代价这两个条件。

        我们假设G为从开始移动到现在这个节点的步数,H为曼哈顿距离(即|xe-xi|+|ye-yi|,其中xe,ye是终点坐标,xi,yi是当前点坐标),为了方便起见,我们假设只能左右上下行走,不能斜着走,那么上面的F就为下面这样。起点的F值要给最大。

 

         那么A*算法就是会找到最小的那个F然后走上去,并且记录下父节点。专业的来讲就是把当前节点所有可以走的节点的F值算出来,把节点放入open表中,然后找到最小的F值的节点,放入close表中。这里我们可以将open表化为一个树,close表化为一个数组。

         当然我们前提是行走的四个方向都要提前做好确定,然后每次都寻找最后入树且代价最小的那一个节点,走上去,继续寻找后续节点。人工智能书上也给出了相应的参考框图。

 

 2.效果演示

3.代码

  1. int main()
  2. {
  3. treeNode* pRoot = NULL; //准备一棵树
  4. vector<treeNode*> open; //准备一个数组
  5. vector<treeNode*>::iterator it; //迭代器
  6. vector<treeNode*>::iterator itmin; //迭代器
  7. pRoot = creatTreeNode(Astar.O_row, Astar.O_col); //起点入树
  8. treeNode* Now = pRoot;
  9. pRoot->pos.F = Astar.M * Astar.M;
  10. open.push_back(pRoot);
  11. while (1)
  12. {
  13. //1.把当前能走的点找出来
  14. for (int i = 0; i < 4; i++)
  15. {
  16. treeNode* pChild = creatTreeNode(Now->pos.row, Now->pos.col);
  17. switch (i)
  18. {
  19. case Up:
  20. pChild->pos.row--;
  21. pChild->pos.g = Now->pos.g + 1;
  22. break;
  23. case Down:
  24. pChild->pos.row++;
  25. pChild->pos.g = Now->pos.g + 1;
  26. break;
  27. case Left:
  28. pChild->pos.col--;
  29. pChild->pos.g = Now->pos.g + 1;
  30. break;
  31. case Right:
  32. pChild->pos.col++;
  33. pChild->pos.g = Now->pos.g + 1;
  34. break;
  35. default:
  36. break;
  37. }
  38. //2.判断能不能走
  39. if (Astar.maze[pChild->pos.row][pChild->pos.col] == 0 || Astar.maze[pChild->pos.row][pChild->pos.col] == 8
  40. || Astar.maze[pChild->pos.row][pChild->pos.col] == 2) //如果是路则计算F
  41. {
  42. //标记走过
  43. Astar.maze[pChild->pos.row][pChild->pos.col] = 4;
  44. pChild->pos.h = getH(pChild->pos, Astar);//计算H
  45. pChild->pos.F = pChild->pos.g + pChild->pos.h;//计算F
  46. //入树
  47. Now->child.push_back(pChild);
  48. pChild->parent = Now;
  49. //存入数组
  50. open.push_back(pChild);
  51. }
  52. else {
  53. delete pChild;
  54. }
  55. }
  56. //3.找出数组最小的点,走上去
  57. //cout << open.size() << endl;
  58. itmin = open.begin();//假设第一个F最小 时间复杂度O(n)
  59. for (it = open.begin(); it != open.end(); it++)
  60. {
  61. itmin = ((*it)->pos.F <= (*itmin)->pos.F) ? it : itmin; //细节 加上等于
  62. }
  63. //sort(open.begin(), open.end(),cmp);
  64. //itmin = open.begin();
  65. Now = *itmin;
  66. //标记走过
  67. //Astar.maze[Now->pos.row][Now->pos.col] = -1;
  68. show(Astar);
  69. //删除
  70. open.erase(itmin);
  71. //4.判断是否找到终点
  72. if (Now->pos.row == Astar.E_row && Now->pos.col == Astar.E_col)
  73. {
  74. flag = 1;
  75. break;
  76. }
  77. //5.如果为数组为空也退出
  78. if (0 == open.size()) break;
  79. }
  80. //成功则打印
  81. if (flag)
  82. {
  83. cout << "success" << endl;
  84. while (Now)
  85. {
  86. cout << "(" << Now->pos.row << "," << Now->pos.col << ")";
  87. printf("(g:%d,h:%d,f:%d)\n", Now->pos.g, Now->pos.h, Now->pos.F);
  88. Astar.maze[Now->pos.row][Now->pos.col] = 3;
  89. Now = Now->parent;
  90. }
  91. }
  92. else
  93. {
  94. cout << "not found" << endl;
  95. }
  96. while (1)
  97. {
  98. BeginBatchDraw();
  99. show(Astar);
  100. FlushBatchDraw();
  101. }//循环绘图防止BUG
  102. closegraph();
  103. }

四、总代码

  1. #include<iostream>
  2. #include<iomanip>
  3. #include<graphics.h>
  4. #include<time.h>
  5. #include<easyx.h>
  6. #include<algorithm>
  7. #include<vector>
  8. using namespace std;
  9. //1为墙壁,0为路
  10. #define SIZE 10
  11. bool flag = 0;
  12. enum dirct {
  13. Up,
  14. Down,
  15. Left,
  16. Right,
  17. };
  18. struct AStar {
  19. int M; //M*M map
  20. int** maze; //map迷宫
  21. //int* close; //close表
  22. //int* open; //open表
  23. int O_row; //开始的行
  24. int O_col; //开始的列
  25. int E_row; //终点行
  26. int E_col; //终点列
  27. }Astar{ 0,NULL,1,1,0,0 };
  28. struct position {
  29. int row, col;
  30. int g; //路径代价 行走的步数
  31. int h; //启发函数 曼哈顿距离
  32. int F = h + g; //估价函数
  33. };
  34. struct treeNode { //close类似
  35. position pos;
  36. vector<treeNode*> child;
  37. treeNode* parent;
  38. };
  39. treeNode* creatTreeNode(int row, int col); //创建树节点
  40. int getH(position pos, AStar a); //计算H
  41. void Init_Maze(AStar& Astar); //初始化迷宫
  42. void show(AStar& Astar); //图像显示迷宫
  43. void creatMaze(AStar& Astar, int x_index, int y_index); //随机创建迷宫
  44. void makeMaze(AStar& Astar); //给创建的迷宫创建标识
  45. bool findneighbor(AStar& Astar, int x_index, int y_index); //寻找邻居
  46. void MakeDifficult(AStar& Astar); //改变地形,出现更多通路
  47. void ConsoleShow(AStar& Astar); //控制台显示迷宫
  48. ///*排序方式*/
  49. //bool cmp(treeNode* m1, treeNode* m2)
  50. //{
  51. // return m1->pos.F < m2->pos.F;
  52. //}
  53. int main()
  54. {
  55. treeNode* pRoot = NULL; //准备一棵树
  56. vector<treeNode*> open; //准备一个数组
  57. vector<treeNode*>::iterator it; //迭代器
  58. vector<treeNode*>::iterator itmin; //迭代器
  59. Astar.M = 0;
  60. cout << "请输入迷宫是几乘几的" << endl;
  61. cin >> Astar.M;
  62. if (Astar.M % 2 == 0)//如果输入不为奇数变为奇数
  63. Astar.M++;
  64. Astar.E_col = Astar.M;
  65. Astar.E_row = Astar.M;
  66. pRoot = creatTreeNode(Astar.O_row, Astar.O_col); //起点入树
  67. srand((unsigned)time(0));//随机播种
  68. initgraph(((Astar.M + 2) * 2 + 1) * SIZE, (Astar.M + 2) * SIZE, SHOWCONSOLE);//打开绘图
  69. Astar.maze = new int* [Astar.M + 2];//动态创建二维数组
  70. for (int i = 0; i < Astar.M + 2; i++)
  71. Astar.maze[i] = new int[Astar.M + 2];//每一行申请一个int空间的M+2列空间
  72. Init_Maze(Astar);
  73. creatMaze(Astar, 1, 1);
  74. makeMaze(Astar);
  75. MakeDifficult(Astar);
  76. //show(Astar);
  77. treeNode* Now = pRoot;
  78. pRoot->pos.F = Astar.M * Astar.M;
  79. open.push_back(pRoot);
  80. while (1)
  81. {
  82. //1.把当前能走的点找出来
  83. for (int i = 0; i < 4; i++)
  84. {
  85. treeNode* pChild = creatTreeNode(Now->pos.row, Now->pos.col);
  86. switch (i)
  87. {
  88. case Up:
  89. pChild->pos.row--;
  90. pChild->pos.g = Now->pos.g + 1;
  91. break;
  92. case Down:
  93. pChild->pos.row++;
  94. pChild->pos.g = Now->pos.g + 1;
  95. break;
  96. case Left:
  97. pChild->pos.col--;
  98. pChild->pos.g = Now->pos.g + 1;
  99. break;
  100. case Right:
  101. pChild->pos.col++;
  102. pChild->pos.g = Now->pos.g + 1;
  103. break;
  104. default:
  105. break;
  106. }
  107. //2.判断能不能走
  108. if (Astar.maze[pChild->pos.row][pChild->pos.col] == 0 || Astar.maze[pChild->pos.row][pChild->pos.col] == 8
  109. || Astar.maze[pChild->pos.row][pChild->pos.col] == 2) //如果是路则计算F
  110. {
  111. //标记走过
  112. Astar.maze[pChild->pos.row][pChild->pos.col] = 4;
  113. pChild->pos.h = getH(pChild->pos, Astar);//计算H
  114. pChild->pos.F = pChild->pos.g + pChild->pos.h;//计算F
  115. //入树
  116. Now->child.push_back(pChild);
  117. pChild->parent = Now;
  118. //存入数组
  119. open.push_back(pChild);
  120. }
  121. else {
  122. delete pChild;
  123. }
  124. }
  125. //3.找出数组最小的点,走上去
  126. //cout << open.size() << endl;
  127. itmin = open.begin();//假设第一个F最小 时间复杂度O(n)
  128. for (it = open.begin(); it != open.end(); it++)
  129. {
  130. itmin = ((*it)->pos.F <= (*itmin)->pos.F) ? it : itmin; //细节 加上等于
  131. }
  132. //sort(open.begin(), open.end(),cmp);
  133. //itmin = open.begin();
  134. Now = *itmin;
  135. //标记走过
  136. //Astar.maze[Now->pos.row][Now->pos.col] = -1;
  137. show(Astar);
  138. //删除
  139. open.erase(itmin);
  140. //4.判断是否找到终点
  141. if (Now->pos.row == Astar.E_row && Now->pos.col == Astar.E_col)
  142. {
  143. flag = 1;
  144. break;
  145. }
  146. //5.如果为数组为空也退出
  147. if (0 == open.size()) break;
  148. }
  149. //成功则打印
  150. if (flag)
  151. {
  152. cout << "success" << endl;
  153. while (Now)
  154. {
  155. cout << "(" << Now->pos.row << "," << Now->pos.col << ")";
  156. printf("(g:%d,h:%d,f:%d)\n", Now->pos.g, Now->pos.h, Now->pos.F);
  157. Astar.maze[Now->pos.row][Now->pos.col] = 3;
  158. Now = Now->parent;
  159. }
  160. }
  161. else
  162. {
  163. cout << "not found" << endl;
  164. }
  165. while (1)
  166. {
  167. BeginBatchDraw();
  168. show(Astar);
  169. FlushBatchDraw();
  170. }//循环绘图防止BUG
  171. closegraph();
  172. }
  173. //初始化迷宫
  174. void Init_Maze(AStar& Astar)
  175. {
  176. for (int i = 0; i < Astar.M + 2; i++)//动态创建迷宫
  177. for (int j = 0; j < Astar.M + 2; j++)
  178. {
  179. if (i == 0 || i == Astar.M + 1)//1为墙壁
  180. Astar.maze[i][j] = 1;
  181. if ((i % 2 == 1) && (j % 2 == 1))
  182. Astar.maze[i][j] = 0;
  183. else Astar.maze[i][j] = 1;
  184. }
  185. }
  186. //图像显示迷宫
  187. //3为路,4为搜寻的路,1为墙,2为终点,8为开始
  188. void show(AStar& Astar)
  189. {
  190. IMAGE star, wall, ball ,ly;
  191. loadimage(&star, "./五角星.png", SIZE, SIZE);
  192. loadimage(&wall, "./墙.png", SIZE, SIZE);
  193. loadimage(&ball, "./球.png", SIZE, SIZE);
  194. loadimage(&ly, "./兰音.png", SIZE, SIZE);
  195. for (int i = 0; i < Astar.M + 2; i++)
  196. {
  197. for (int j = 0; j < Astar.M + 2; j++)
  198. {
  199. if (Astar.maze[i][j] == 1)
  200. {
  201. putimage((j + Astar.M + 2 + 1) * SIZE, (i)*SIZE, &wall);
  202. putimage(j * SIZE, i * SIZE, &wall);
  203. }
  204. else if (Astar.maze[i][j] == 4)
  205. {
  206. putimage((j + Astar.M + 2 + 1) * SIZE, (i)*SIZE, &ball);
  207. }
  208. else if (Astar.maze[i][j] == 3)
  209. {
  210. putimage(j * SIZE, (i)*SIZE, &ly);
  211. }
  212. }
  213. }
  214. putimage((Astar.M + 2 + 1 + 1) * SIZE, (SIZE), &star);
  215. putimage((Astar.M + Astar.M + 2 + 1) * SIZE, (Astar.M) * SIZE, &star);
  216. putimage(SIZE, SIZE, &star);
  217. putimage(Astar.M * SIZE, Astar.M * SIZE, &star);
  218. }
  219. //寻找邻居
  220. bool findneighbor(AStar& Astar, int x_index, int y_index)
  221. {
  222. if ((x_index >= 3 && Astar.maze[x_index - 2][y_index] == 0) || (x_index <= Astar.M - 1 && Astar.maze[x_index + 2][y_index] == 0)
  223. || (y_index >= 3 && Astar.maze[x_index][y_index - 2] == 0) || (y_index <= Astar.M - 1 && Astar.maze[x_index][y_index + 2] == 0))
  224. return 1;
  225. else
  226. return 0;
  227. }
  228. //随机创建迷宫
  229. void creatMaze(AStar& Astar, int x_index, int y_index)
  230. {
  231. int pos, x, y, flag = 0;
  232. x = x_index;
  233. y = y_index;
  234. while (1)
  235. {
  236. flag = 0;
  237. flag = findneighbor(Astar, x, y);
  238. if (!flag)
  239. return;
  240. else {
  241. Astar.maze[x_index][y_index] = 5;
  242. x = x_index;
  243. y = y_index;
  244. while (1)
  245. {
  246. pos = rand() % (4 - 1 + 1) + 1;
  247. if (pos == 1 && x_index >= 3 && Astar.maze[x_index - 2][y_index] == 0)//上
  248. {
  249. x_index -= 2;
  250. }
  251. else if (pos == 2 && x_index <= Astar.M - 1 && Astar.maze[x_index + 2][y_index] == 0)//下
  252. {
  253. x_index += 2;
  254. }
  255. else if (pos == 3 && y_index <= Astar.M - 1 && Astar.maze[x_index][y_index + 2] == 0)
  256. {
  257. y_index += 2;
  258. }
  259. else if (pos == 4 && y_index >= 3 && Astar.maze[x_index][y_index - 2] == 0)
  260. {
  261. y_index -= 2;
  262. }
  263. Astar.maze[(x + x_index) / 2][(y + y_index) / 2] = 5;
  264. Astar.maze[x_index][y_index] = 5;
  265. // showmaze(maze, M);
  266. creatMaze(Astar, x_index, y_index);
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. //给创建的迷宫创建标识
  273. void makeMaze(AStar& Astar)
  274. {
  275. for (int i = 0; i < Astar.M + 2; i++)
  276. for (int j = 0; j < Astar.M + 2; j++)
  277. {
  278. if (Astar.maze[i][j] == 5)
  279. {
  280. Astar.maze[i][j] = 0;
  281. }
  282. }
  283. Astar.maze[1][1] = 8;
  284. Astar.maze[Astar.M][Astar.M] = 2;
  285. }
  286. //改变地形,出现更多通路
  287. void MakeDifficult(AStar& Astar)
  288. {
  289. if (Astar.M > 5)
  290. {
  291. int half = (int)((0.4 * Astar.M) * (0.5 * Astar.M));
  292. int x_c, y_c = 0;
  293. for (int i = 0; i < half;)
  294. {
  295. x_c = rand() % (Astar.M - 1 + 1) + 1;
  296. y_c = rand() % (Astar.M - 1 + 1) + 1;
  297. if (Astar.maze[x_c][y_c] == 1)
  298. {
  299. Astar.maze[x_c][y_c] = 0;
  300. i++;
  301. }
  302. }
  303. }
  304. }
  305. //计算H
  306. int getH(position pos, AStar a) {
  307. return (a.E_row - pos.row) + (a.E_col - pos.col);
  308. }
  309. //创建树节点
  310. treeNode* creatTreeNode(int row, int col)
  311. {
  312. treeNode* pNew = new treeNode;//开内存
  313. memset(pNew, 0, sizeof(treeNode)); //全部赋值为0
  314. pNew->pos.col = col;
  315. pNew->pos.row = row;
  316. return pNew;
  317. }
  318. //控制台显示迷宫
  319. void ConsoleShow(AStar& Astar)
  320. {
  321. cout << endl << "3为走出迷宫的路,1为墙壁,2为出口" << endl;;
  322. for (int i = 0; i < Astar.M + 2; i++)
  323. {
  324. for (int j = 0; j < Astar.M + 2; j++)
  325. {
  326. cout << setw(3) << Astar.maze[i][j];
  327. }
  328. cout << endl;
  329. }
  330. }

五、总结

        这份代码还有一些莫名其妙的bug,但已经改不动了。希望可以帮助到大家。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/526219
推荐阅读
相关标签
  

闽ICP备14008679号