Advertisement
Cinestra

Path finder kinda works, but needs a lot of refinement

Mar 31st, 2023
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <queue>
  4. #include <stack>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class Coord
  10. {
  11. public:
  12. Coord(int r, int c) : m_row(r), m_col(c) {}
  13. int r() const { return m_row; }
  14. int c() const { return m_col; }
  15. private:
  16. int m_row;
  17. int m_col;
  18. };
  19.  
  20. // Returns true if a path exists from (starting_row, starting_col) to (ending_row, ending_col)
  21. bool pathExists(string maze[], int number_of_rows, int number_of_cols, int starting_row, int starting_col, int ending_row, int ending_col)
  22. {
  23. // Initialize a Queue of Coordinates
  24. queue<Coord> queue_of_coordinates;
  25.  
  26. // Push the Queue by putting in the starting row and starting col
  27. // Assignment instructions that starting point is always going to be a "." and not a wall
  28. queue_of_coordinates.push(Coord(starting_row, starting_col));
  29.  
  30. // The maze is an array of strings
  31. // While we have only defined the rows of the string, the "columns" of the string are implict because they are each of the individual characters of the string
  32. // We will overwrite that as D to mark it as discovered
  33. maze[starting_row][starting_col] = 'D';
  34.  
  35. while (!queue_of_coordinates.empty())
  36. {
  37. // Pop the queue by taking the coordinate in queue and popping it
  38. // Different from stack because stack pops the most recent item whereas queue pops the item that has been there the longest
  39. Coord current = queue_of_coordinates.front();
  40. queue_of_coordinates.pop();
  41.  
  42. int current_row = current.r();
  43. int current_col = current.c();
  44.  
  45. if (current_row == ending_row && current_col == ending_col)
  46. return true;
  47.  
  48. // Remember to use IF statements for the following, not else if statements
  49. // You need to put each and every possible point on the stack
  50.  
  51. // First, check EAST, which is (r, c+1)
  52. if (maze[current_row][current_col + 1] == '.')
  53. {
  54. queue_of_coordinates.push(Coord(current_row, current_col + 1));
  55. maze[current_row][current_col + 1] = 'D';
  56. }
  57.  
  58. // Second, check NORTH, which is (r-1, c)
  59. if (maze[current_row - 1][current_col] == '.')
  60. {
  61. queue_of_coordinates.push(Coord(current_row - 1, current_col));
  62. maze[current_row - 1][current_col] = 'D';
  63. }
  64.  
  65. // Third, check WEST, which is (r, c-1)
  66. if (maze[current_row][current_col - 1] == '.')
  67. {
  68. queue_of_coordinates.push(Coord(current_row, current_col - 1));
  69. maze[current_row][current_col - 1] = 'D';
  70. }
  71.  
  72. // Finally, check SOUTH, which is (r+1, c)
  73. if (maze[current_row + 1][current_col] == '.')
  74. {
  75. queue_of_coordinates.push(Coord(current_row + 1, current_col));
  76. maze[current_row + 1][current_col] = 'D';
  77. }
  78. }
  79.  
  80. return false;
  81. }
  82.  
  83. void printMaze(string maze[], int number_of_rows, int number_of_cols)
  84. {
  85. for (int i = 0; i < number_of_rows; i++)
  86. {
  87. for (int j = 0; j < number_of_cols; j++)
  88. {
  89. cout << maze[i][j];
  90. }
  91. cout << endl;
  92. }
  93.  
  94. cout << endl;
  95. }
  96.  
  97. bool isDeadEnd(string maze[], int number_of_rows, int number_of_cols, int starting_row, int starting_col, int ending_row, int ending_col, int current_row, int current_col)
  98. {
  99. // If there are 3 directions unavailable, then it is a dead end
  100. // There will always be one direction available (the one that lead to that point).
  101. //
  102. // 3 different characters mark if a direction is unavailable: walls, N, or .
  103. // . represents a path never taken when proving if the path exists or not
  104. // x represents a wall that we obviously we cannot walk into
  105. // N represents a point we've marked that leads nowhere
  106.  
  107. if (current_row == starting_row && current_col == starting_col)
  108. return false;
  109.  
  110. if (current_row == ending_row && current_col == ending_col)
  111. return false;
  112.  
  113. int number_of_directions_unavailable = 0;
  114.  
  115. // First, check EAST, which is (r, c+1)
  116. if (maze[current_row][current_col + 1] == '.' || maze[current_row][current_col + 1] == 'X' || maze[current_row][current_col + 1] == 'N')
  117. number_of_directions_unavailable++;
  118.  
  119. // Second, check NORTH, which is (r-1, c)
  120. if (maze[current_row - 1][current_col] == '.' || maze[current_row - 1][current_col] == 'X' || maze[current_row - 1][current_col] == 'N')
  121. number_of_directions_unavailable++;
  122.  
  123. // Third, check WEST, which is (r, c-1)
  124. if (maze[current_row][current_col - 1] == '.' || maze[current_row][current_col - 1] == 'X' || maze[current_row][current_col - 1] == 'N')
  125. number_of_directions_unavailable++;
  126.  
  127. // Last, check SOUTH, which is (r+1, c)
  128. if (maze[current_row + 1][current_col] == '.' || maze[current_row + 1][current_col] == 'X' || maze[current_row + 1][current_col] == 'N')
  129. number_of_directions_unavailable++;
  130.  
  131. if (number_of_directions_unavailable > 2)
  132. {
  133. maze[current_row][current_col] = 'N';
  134. return true;
  135. }
  136.  
  137. return false;
  138.  
  139. }
  140.  
  141. bool findDirections(string maze[], int number_of_rows, int number_of_cols, int starting_row, int starting_col, int ending_row, int ending_col)
  142. {
  143. /*
  144. * Psuedo Code Approach:
  145. * First run to check if the path even exists
  146. * Checking if the path exists marks each discoverable area as a D
  147. *
  148. * For this approach because we are checking individual paths, we will use a stack
  149. *
  150. * We'll mark each point we retrace as R
  151. * Go through each D until you reach either the beginning or nowhere
  152. * If you reach the beginning, then that is the path and we will write directions
  153. * We will check the surroundings to see if it ends up in nowhere
  154. * We will then pop the stack to go back to the previous point
  155. *
  156. * One problem that arises is that if you find a path, there are still some coordinates that were pushed onto the stack that didn't lead anywhere
  157. * After we've found a path, we will write a helper program that checks every non starting and ending point to ensure that they are connected in front and behind
  158. * That is to say that each non starting and ending point R is connected to two other Rs
  159. */
  160.  
  161. /*
  162. Psuedo Code Approach #2:
  163. * First run to check if the path even exists
  164. * Checking if the path exists marks each discoverable area as a D
  165. *
  166. *
  167. */
  168.  
  169. /*
  170.  
  171. bool check_pathExists = pathExists(maze, number_of_rows, number_of_cols, starting_row, starting_col, ending_row, ending_col);
  172.  
  173. if (check_pathExists == false)
  174. {
  175. cout << "There are no directions because there is no path that exists." << endl;
  176. return false;
  177. }
  178.  
  179. */
  180.  
  181. // If a path exists, but it's because you do nothing, simply say do nothing
  182. if (starting_row == ending_row && starting_col == ending_col)
  183. {
  184. cout << "There are no directions because the start point is the same as the end point." << endl;
  185. return true;
  186. }
  187.  
  188. stack<Coord> stack_of_retraced_coords;
  189. stack_of_retraced_coords.push(Coord(ending_row, ending_col));
  190.  
  191. int num_r_tiles = 0;
  192.  
  193. // Instead of popping coordinates, we will actually keep them in the stack
  194. // We will pop coordinates when we reach a dead end or reach the end
  195. while (!stack_of_retraced_coords.empty())
  196. {
  197. // Get the coordinates of whatever is currently on top of the stack
  198. // Then make decisions based on those coordinates
  199. Coord current = stack_of_retraced_coords.top();
  200.  
  201. int current_row = current.r();
  202. int current_col = current.c();
  203.  
  204. if (current_row == starting_row && current_col == starting_col)
  205. return true;
  206.  
  207. // Remember to use IF statements for the following, not else if statements
  208. // You need to put each and every possible point on the stack
  209.  
  210. // Now that we've put all the possibilities on the stack, let's check if the current is at a dead end or not
  211. // If it is at a dead end, we will mark that point as N and then pop it from the stack
  212. // Popping the current stack will mean we will go to the previous point and then check if is also a dead end after we updated this current point to N
  213.  
  214. /*
  215. if (isDeadEnd(maze, number_of_rows, number_of_cols, starting_row, starting_col, ending_row, ending_col, current_row, current_col))
  216. {
  217. stack_of_retraced_coords.pop();
  218. num_r_tiles--;
  219. }
  220. */
  221.  
  222. while (isDeadEnd(maze, number_of_rows, number_of_cols, starting_row, starting_col, ending_row, ending_col, current_row, current_col))
  223. {
  224. stack_of_retraced_coords.pop();
  225. num_r_tiles--;
  226.  
  227. current = stack_of_retraced_coords.top();
  228. current_row = current.r();
  229. current_col = current.c();
  230. }
  231.  
  232. // First, check EAST, which is (r, c+1)
  233. if (maze[current_row][current_col + 1] == 'D')
  234. {
  235. stack_of_retraced_coords.push(Coord(current_row, current_col + 1));
  236. maze[current_row][current_col + 1] = 'R';
  237. }
  238.  
  239. // Second, check NORTH, which is (r-1, c)
  240. if (maze[current_row - 1][current_col] == 'D')
  241. {
  242. stack_of_retraced_coords.push(Coord(current_row - 1, current_col));
  243. maze[current_row - 1][current_col] = 'R';
  244. }
  245.  
  246. // Third, check WEST, which is (r, c-1)
  247. if (maze[current_row][current_col - 1] == 'D')
  248. {
  249. stack_of_retraced_coords.push(Coord(current_row, current_col - 1));
  250. maze[current_row][current_col - 1] = 'R';
  251. }
  252.  
  253. // Last, check SOUTH, which is (r+1, c)
  254. if (maze[current_row + 1][current_col] == 'D')
  255. {
  256. stack_of_retraced_coords.push(Coord(current_row + 1, current_col));
  257. maze[current_row + 1][current_col] = 'R';
  258. }
  259. }
  260.  
  261. }
  262.  
  263. int main()
  264. {
  265. string maze1[10] = {
  266. "XXXXXXXXXX",
  267. "X.X..X...X",
  268. "X.XX.X.XXX",
  269. "X....X.X.X",
  270. "XX.X.X...X",
  271. "XXX..X.X.X",
  272. "X...X...XX",
  273. "X.XX..X.XX",
  274. "X....X...X",
  275. "XXXXXXXXXX",
  276. };
  277.  
  278. string maze2[10] = {
  279. "XXXXXXXXXX",
  280. "X.X..X...X",
  281. "XXXX.X.XXX",
  282. "X....X.X.X",
  283. "XX.X.X...X",
  284. "XXX..X.X.X",
  285. "X...X...XX",
  286. "X.XX..X.XX",
  287. "X....X...X",
  288. "XXXXXXXXXX",
  289. };
  290.  
  291. string maze3[10] = {
  292. "XXXXXXXXXX",
  293. "XX.....XXX",
  294. "X..XX....X",
  295. "X...X...XX",
  296. "X.X.XXX..X",
  297. "XXXX..X..X",
  298. "XX....X..X",
  299. "X.......XX",
  300. "X..XXXXXXX",
  301. "XXXXXXXXXX",
  302. };
  303.  
  304. string maze4[10] = {
  305. "XXXXXXXXXX",
  306. "XX.....XXX",
  307. "X..XX....X",
  308. "X...X...XX",
  309. "X.X.XXX..X",
  310. "XXXX..X..X",
  311. "XX....X..X",
  312. "X.X.....XX",
  313. "X..XXXXXXX",
  314. "XXXXXXXXXX",
  315. };
  316.  
  317. string maze5[6] = {
  318. "XXXXXX",
  319. "XX.XXX",
  320. "XX.XXX",
  321. "X...XX",
  322. "XX.XXX",
  323. "XXXXXX",
  324. };
  325.  
  326. string maze6[10] = {
  327. "XXXXXXXXXX",
  328. "X.XXXXXXXX",
  329. "X...XXXX.X",
  330. "X.XXXXXX.X",
  331. "X.XXXXXX.X",
  332. "X.XXXXXX.X",
  333. "X.XXXXXX.X",
  334. "X.XXXXXX.X",
  335. "X........X",
  336. "XXXXXXXXXX",
  337. };
  338.  
  339. cout << "Maze 5" << endl;
  340. pathExists(maze5, 6, 6, 4, 2, 1, 2);
  341. printMaze(maze5, 6, 6);
  342. findDirections(maze5, 6, 6, 4, 2, 1, 2);
  343. printMaze(maze5, 6, 6);
  344.  
  345. cout << "Maze 1" << endl;
  346. pathExists(maze1, 10, 10, 8, 6, 1, 1);
  347. printMaze(maze1, 10, 10);
  348. findDirections(maze1, 10, 10, 8, 6, 1, 1);
  349. printMaze(maze1, 10, 10);
  350.  
  351. cout << "Maze 6" << endl;
  352. pathExists(maze6, 10, 10, 8, 8, 1, 1);
  353. printMaze(maze6, 10, 10);
  354. findDirections(maze6, 10, 10, 8, 8, 1, 1);
  355. printMaze(maze6, 10, 10);
  356.  
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement