Advertisement
Cinestra

Path finder works, but I want even more improvement

Mar 31st, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.86 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 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. bool findDirections(string maze[], int number_of_rows, int number_of_cols, int starting_row, int starting_col, int ending_row, int ending_col)
  141. {
  142. /*
  143. * Psuedo Code Approach:
  144. * First run to check if the path even exists
  145. * Checking if the path exists marks each discoverable area as a D
  146. *
  147. * For this approach because we are checking individual paths, we will use a stack
  148. *
  149. * We'll mark each point we retrace as R
  150. * Go through each D until you reach either the beginning or nowhere
  151. * If you reach the beginning, then that is the path and we will write directions
  152. * We will check the surroundings to see if it ends up in nowhere
  153. * We will then pop the stack to go back to the previous point
  154. *
  155. * 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
  156. * 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
  157. * That is to say that each non starting and ending point R is connected to two other Rs
  158. */
  159.  
  160. /*
  161. Psuedo Code Approach #2:
  162. * First run to check if the path even exists
  163. * Checking if the path exists marks each discoverable area as a D
  164. *
  165. *
  166. */
  167.  
  168. /*
  169.  
  170. bool check_pathExists = pathExists(maze, number_of_rows, number_of_cols, starting_row, starting_col, ending_row, ending_col);
  171.  
  172. if (check_pathExists == false)
  173. {
  174. cout << "There are no directions because there is no path that exists." << endl;
  175. return false;
  176. }
  177.  
  178. */
  179.  
  180. // If a path exists, but it's because you do nothing, simply say do nothing
  181. if (starting_row == ending_row && starting_col == ending_col)
  182. {
  183. cout << "There are no directions because the start point is the same as the end point." << endl;
  184. return true;
  185. }
  186.  
  187. stack<Coord> stack_of_retraced_coords;
  188. stack_of_retraced_coords.push(Coord(ending_row, ending_col));
  189.  
  190. int num_r_tiles = 0;
  191.  
  192. // Instead of popping coordinates, we will actually keep them in the stack
  193. // We will pop coordinates when we reach a dead end or reach the end
  194. while (!stack_of_retraced_coords.empty())
  195. {
  196. // Get the coordinates of whatever is currently on top of the stack
  197. // Then make decisions based on those coordinates
  198. Coord current = stack_of_retraced_coords.top();
  199.  
  200. int current_row = current.r();
  201. int current_col = current.c();
  202.  
  203. if (current_row == starting_row && current_col == starting_col)
  204. break;
  205.  
  206. // Remember to use IF statements for the following, not else if statements
  207. // You need to put each and every possible point on the stack
  208.  
  209. // Now that we've put all the possibilities on the stack, let's check if the current is at a dead end or not
  210. // If it is at a dead end, we will mark that point as N and then pop it from the stack
  211. // 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
  212.  
  213. /*
  214. if (isDeadEnd(maze, number_of_rows, number_of_cols, starting_row, starting_col, ending_row, ending_col, current_row, current_col))
  215. {
  216. stack_of_retraced_coords.pop();
  217. num_r_tiles--;
  218. }
  219. */
  220.  
  221. while (isDeadEnd(maze, starting_row, starting_col, ending_row, ending_col, current_row, current_col))
  222. {
  223. stack_of_retraced_coords.pop();
  224. num_r_tiles--;
  225.  
  226. current = stack_of_retraced_coords.top();
  227. current_row = current.r();
  228. current_col = current.c();
  229. }
  230.  
  231. // First, check EAST, which is (r, c+1)
  232. if (maze[current_row][current_col + 1] == 'D')
  233. {
  234. stack_of_retraced_coords.push(Coord(current_row, current_col + 1));
  235. maze[current_row][current_col + 1] = 'R';
  236. }
  237.  
  238. // Second, check NORTH, which is (r-1, c)
  239. if (maze[current_row - 1][current_col] == 'D')
  240. {
  241. stack_of_retraced_coords.push(Coord(current_row - 1, current_col));
  242. maze[current_row - 1][current_col] = 'R';
  243. }
  244.  
  245. // Third, check WEST, which is (r, c-1)
  246. if (maze[current_row][current_col - 1] == 'D')
  247. {
  248. stack_of_retraced_coords.push(Coord(current_row, current_col - 1));
  249. maze[current_row][current_col - 1] = 'R';
  250. }
  251.  
  252. // Last, check SOUTH, which is (r+1, c)
  253. if (maze[current_row + 1][current_col] == 'D')
  254. {
  255. stack_of_retraced_coords.push(Coord(current_row + 1, current_col));
  256. maze[current_row + 1][current_col] = 'R';
  257. }
  258. }
  259.  
  260. while (!stack_of_retraced_coords.empty())
  261. {
  262. Coord current = stack_of_retraced_coords.top();
  263. int current_row = current.r();
  264. int current_col = current.c();
  265. isDeadEnd(maze, starting_row, starting_col, ending_row, ending_col, current_row, current_col);
  266. stack_of_retraced_coords.pop();
  267. }
  268.  
  269. }
  270.  
  271. int main()
  272. {
  273. string maze1[10] = {
  274. "XXXXXXXXXX",
  275. "X.X..X...X",
  276. "X.XX.X.XXX",
  277. "X....X.X.X",
  278. "XX.X.X...X",
  279. "XXX..X.X.X",
  280. "X...X...XX",
  281. "X.XX..X.XX",
  282. "X....X...X",
  283. "XXXXXXXXXX",
  284. };
  285.  
  286. string maze2[10] = {
  287. "XXXXXXXXXX",
  288. "X.X..X...X",
  289. "XXXX.X.XXX",
  290. "X....X.X.X",
  291. "XX.X.X...X",
  292. "XXX..X.X.X",
  293. "X...X...XX",
  294. "X.XX..X.XX",
  295. "X....X...X",
  296. "XXXXXXXXXX",
  297. };
  298.  
  299. string maze3[10] = {
  300. "XXXXXXXXXX",
  301. "XX.....XXX",
  302. "X..XX....X",
  303. "X...X...XX",
  304. "X.X.XXX..X",
  305. "XXXX..X..X",
  306. "XX....X..X",
  307. "X.......XX",
  308. "X..XXXXXXX",
  309. "XXXXXXXXXX",
  310. };
  311.  
  312. string maze4[10] = {
  313. "XXXXXXXXXX",
  314. "XX.....XXX",
  315. "X..XX....X",
  316. "X...X...XX",
  317. "X.X.XXX..X",
  318. "XXXX..X..X",
  319. "XX....X..X",
  320. "X.X.....XX",
  321. "X..XXXXXXX",
  322. "XXXXXXXXXX",
  323. };
  324.  
  325. string maze5[6] = {
  326. "XXXXXX",
  327. "XX.XXX",
  328. "XX.XXX",
  329. "X...XX",
  330. "XX.XXX",
  331. "XXXXXX",
  332. };
  333.  
  334. string maze6[10] = {
  335. "XXXXXXXXXX",
  336. "X.XXXXXXXX",
  337. "X...XXXX.X",
  338. "X.XXXXXX.X",
  339. "X.XXXXXX.X",
  340. "X.XXXXXX.X",
  341. "X.XXXXXX.X",
  342. "X.XXXXXX.X",
  343. "X........X",
  344. "XXXXXXXXXX",
  345. };
  346.  
  347. cout << "Maze 5" << endl;
  348. pathExists(maze5, 6, 6, 4, 2, 1, 2);
  349. printMaze(maze5, 6, 6);
  350. findDirections(maze5, 6, 6, 4, 2, 1, 2);
  351. printMaze(maze5, 6, 6);
  352.  
  353. cout << "Maze 1" << endl;
  354. pathExists(maze1, 10, 10, 8, 6, 1, 1);
  355. printMaze(maze1, 10, 10);
  356. findDirections(maze1, 10, 10, 8, 6, 1, 1);
  357. printMaze(maze1, 10, 10);
  358.  
  359. cout << "Maze 6" << endl;
  360. pathExists(maze6, 10, 10, 8, 8, 1, 1);
  361. printMaze(maze6, 10, 10);
  362. findDirections(maze6, 10, 10, 8, 8, 1, 1);
  363. printMaze(maze6, 10, 10);
  364.  
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement