Advertisement
Cinestra

Failed Path Finder

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