• 深度寻路四方向


    1. #include
    2. #include "SDXL.h"
    3. using namespace std;
    4. int map[20][20] = {
    5. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
    6. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
    7. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
    8. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
    9. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0},
    10. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
    11. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
    12. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
    13. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
    14. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
    15. {3,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,4},
    16. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
    17. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
    18. {0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0},
    19. {0,0,0,0,1,0,1,1,1,1,0,0,0,0,1,0,0,0,0,0},
    20. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
    21. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
    22. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
    23. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0},
    24. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}
    25. };
    26. int main()
    27. {
    28. SDXL a(0, 10, 19, 10, 20, 20, &map[0][0]);
    29. //a.setmap();
    30. a.xunlu();
    31. a.shuchu();
    32. return 0;
    33. }
    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. using namespace std;
    7. typedef struct dian
    8. {
    9. int y;
    10. int x;
    11. struct dian* befront;
    12. }Dian;
    13. class SDXL
    14. {
    15. private:
    16. list listdian;
    17. list shifang;
    18. public:
    19. int StarX;
    20. int StarY;
    21. int EndX;
    22. int EndY;
    23. int X;
    24. int Y;
    25. int* map;
    26. SDXL(int sx, int sy, int ex, int ey, int x, int y, int* pp);
    27. void setmap();
    28. bool isCanMove(int l, int r);
    29. void xunlu();
    30. void shuchu();
    31. };
    1. #include "SDXL.h"
    2. SDXL::SDXL(int sx, int sy, int ex, int ey, int x, int y, int* pp)
    3. {
    4. this->StarX = sx;
    5. this->StarY = sy;
    6. this->EndX = ex;
    7. this->EndY = ey;
    8. this->X = x;
    9. this->Y = y;
    10. this->map = pp;
    11. }
    12. void SDXL::setmap()
    13. {
    14. system("cls");
    15. for (size_t i = 0; i < Y; i++)
    16. {
    17. for (size_t j = 0; j < X; j++)
    18. {
    19. cout << *(map + (i * X) + j);
    20. }
    21. cout << endl;
    22. }
    23. cout << endl;
    24. }
    25. bool SDXL::isCanMove(int x, int y)
    26. {
    27. if (x<0 || x>(this->X - 1) || y<0 || y>(this->Y - 1))
    28. {
    29. return false;
    30. }
    31. if (*(map + (y * this->X) + x) == 1 || *(map + (y * (this->X)) + x) == 2 || *(map + (y * this->X) + x) == 3)
    32. {
    33. return false;
    34. }
    35. return true;
    36. }
    37. void SDXL::xunlu()
    38. {
    39. int t_x;
    40. int t_y;
    41. Dian* t = new Dian;
    42. t->x = this->StarX;
    43. t->y = this->StarY;
    44. t->befront = NULL;
    45. listdian.push_back(t);
    46. shifang.push_back(t);
    47. do
    48. {
    49. if (listdian.empty())
    50. {
    51. break;
    52. }
    53. int j = 0;
    54. t = listdian.back();
    55. t_x = t->x;
    56. t_y = t->y;
    57. if (t_x == EndX && t_y == EndY)
    58. {
    59. break;
    60. }
    61. while (true)
    62. {
    63. //顺时针
    64. if (isCanMove(t_x, t_y - 1))
    65. {
    66. if (t_x == EndX && t_y - 1 == EndY)
    67. {
    68. j = 1;
    69. break;
    70. }
    71. *(map + ((t_y - 1) * this->X) + t_x) = 2;
    72. Dian* nks = new Dian();
    73. nks->x = t_x;
    74. nks->y = t_y - 1;
    75. nks->befront = t;
    76. listdian.push_back(nks);
    77. shifang.push_back(nks);
    78. nks = NULL;
    79. t_x = listdian.back()->x;
    80. t_y = listdian.back()->y;
    81. t = listdian.back();
    82. continue;
    83. }
    84. else if (isCanMove(t_x + 1, t_y))
    85. {
    86. if (t_x+1 == EndX && t_y == EndY)
    87. {
    88. j = 1;
    89. break;
    90. }
    91. *(map + (t_y * this->X) + t_x + 1) = 2;
    92. Dian* nks = new Dian();
    93. nks->x = t_x + 1;
    94. nks->y = t_y;
    95. nks->befront = t;
    96. listdian.push_back(nks);
    97. shifang.push_back(nks);
    98. nks = NULL;
    99. t_x = listdian.back()->x;
    100. t_y = listdian.back()->y;
    101. t = listdian.back();
    102. continue;
    103. }
    104. else if (isCanMove(t_x, t_y + 1))
    105. {
    106. if (t_x == EndX && t_y + 1 == EndY)
    107. {
    108. j = 1;
    109. break;
    110. }
    111. *(map + ((t_y + 1) * this->X) + t_x) = 2;
    112. Dian* nks = new Dian();
    113. nks->x = t_x;
    114. nks->y = t_y + 1;
    115. nks->befront = t;
    116. listdian.push_back(nks);
    117. shifang.push_back(nks);
    118. nks = NULL;
    119. t_x = listdian.back()->x;
    120. t_y = listdian.back()->y;
    121. t = listdian.back();
    122. continue;
    123. }
    124. else if (isCanMove(t_x - 1, t_y))
    125. {
    126. if (t_x-1 == EndX && t_y == EndY)
    127. {
    128. j = 1;
    129. break;
    130. }
    131. *(map + (t_y * (this->X)) + t_x - 1) = 2;
    132. Dian* nks = new Dian();
    133. nks->x = t_x - 1;
    134. nks->y = t_y;
    135. nks->befront = t;
    136. listdian.push_back(nks);
    137. shifang.push_back(nks);
    138. nks = NULL;
    139. t_x = listdian.back()->x;
    140. t_y = listdian.back()->y;
    141. t = listdian.back();
    142. continue;
    143. }
    144. else
    145. {
    146. break;
    147. }
    148. }
    149. t = NULL;
    150. if (j == 1)
    151. {
    152. break;
    153. }
    154. listdian.pop_back();
    155. //();
    156. if (listdian.empty())
    157. {
    158. cout << "无路可走" << endl;
    159. }
    160. //Sleep(10);
    161. } while (true);
    162. }
    163. void SDXL::shuchu()
    164. {
    165. //system("cls");
    166. Dian* luxian = NULL;
    167. if (!listdian.empty())
    168. {
    169. luxian = listdian.back();
    170. }
    171. int i = 0;
    172. while (luxian != NULL)
    173. {
    174. *(map + (luxian->y) * this->X + luxian->x) = 5;
    175. luxian = luxian->befront;
    176. i++;
    177. }
    178. while (!shifang.empty())
    179. {
    180. free(shifang.front());
    181. shifang.pop_front();
    182. }
    183. listdian.clear();
    184. shifang.clear();
    185. for (size_t i = 0; i < Y; i++)
    186. {
    187. for (size_t j = 0; j < X; j++)
    188. {
    189. //cout << (*(map + (i * X) + j));
    190. if (*(map + (i * X) + j) == 5)
    191. {
    192. cout << "*";
    193. }
    194. else if (*(map + (i * X) + j) == 1)
    195. {
    196. cout << "#";
    197. }
    198. else
    199. {
    200. cout << " ";
    201. }
    202. }
    203. cout << endl;
    204. }
    205. cout << endl;
    206. cout << i << endl;
    207. }

     

  • 相关阅读:
    Selenium —— 网页frame与多窗口处理!
    Linux系统基本使用 与 程序部署 - JavaEE初阶 - 细节狂魔
    Java面向对象高级
    k8s实战入门——Service
    黑苹果引导介绍篇
    返回二叉树中两个节点的最低公共祖先
    Git命令总结-常用-后续使用频繁的再添加~
    【Bug】8086汇编学习
    【user_key_payload、msg_msg、pipe_buffer】再探RWCTF2023-Digging-into-kernel-3
    企业怎样申请SSL证书?
  • 原文地址:https://blog.csdn.net/qq_38753888/article/details/126098281