Advertisement
Cinestra

Failed Finding Directions, Restarting

Mar 31st, 2023
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.06 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_col, starting_row));
  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_col][starting_row] = '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 checkSurrounndings(string maze[], int number_of_rows, int number_of_cols, int current_row, int current_col)
  98. {
  99. // First, check EAST, which is (r, c+1)
  100. if (maze[current_row][current_col + 1] == 'D' || )
  101. {
  102. stack_of_coordinates.push(Coord(current_row, current_col + 1));
  103. maze[current_row][current_col + 1] = 'D';
  104. }
  105.  
  106. // Second, check NORTH, which is (r-1, c)
  107. if (maze[current_row - 1][current_col] == '.')
  108. {
  109. stack_of_coordinates.push(Coord(current_row - 1, current_col));
  110. maze[current_row - 1][current_col] = 'D';
  111. }
  112.  
  113. // Third, check WEST, which is (r, c-1)
  114. if (maze[current_row][current_col - 1] == '.')
  115. {
  116. stack_of_coordinates.push(Coord(current_row, current_col - 1));
  117. maze[current_row][current_col - 1] = 'D';
  118. }
  119.  
  120. // Last, check SOUTH, which is (r+1, c)
  121. if (maze[current_row + 1][current_col] == '.')
  122. {
  123. stack_of_coordinates.push(Coord(current_row + 1, current_col));
  124. maze[current_row + 1][current_col] = 'D';
  125. }
  126. }
  127.  
  128. void findDirections(string maze[], int number_of_rows, int number_of_cols, int starting_row, int starting_col, int ending_row, int ending_col)
  129. {
  130. /*
  131. * Psuedo Code Approach:
  132. * First run to check if the path even exists
  133. * Checking if the path exists marks each discoverable area as a D
  134. * We will mark the start with a S and the end with an E
  135. * Let's start with the end point and check each connecting D
  136. *
  137. * For this approach because we are checking individual paths, we will use a stack
  138. *
  139. * We'll mark each point we retrace as R
  140. * Go through each D until you reach either the beginning or nowhere
  141. * If you reach the beginning, then that is the path and we will write directions
  142. * If you reach nowhere, we will mark that point as N and then go to the previous point
  143. * The previous point will check if there is another direction available (not checking the N)
  144. * If we keep changing points that lead nowhere to N, then eventually we will find a path
  145. *
  146. */
  147.  
  148. /*
  149. Psuedo Code Approach #2:
  150. * First run to check if the path even exists
  151. * Checking if the path exists marks each discoverable area as a D
  152. *
  153. *
  154. */
  155.  
  156. bool check_pathExists = pathExists(maze, number_of_rows, number_of_cols, starting_row, starting_col, ending_row, ending_col);
  157.  
  158. if (check_pathExists == false)
  159. {
  160. cout << "There are no directions because there is no path that exists." << endl;
  161. return;
  162. }
  163.  
  164. // If a path exists, but it's because you do nothing, simply say do nothing
  165. if (starting_row == ending_row && starting_col == ending_row)
  166. {
  167. cout << "There are no directions because the start point is the same as the end point." << endl;
  168. return;
  169. }
  170.  
  171. stack<Coord> stack_of_retraced_coords;
  172. stack_of_retraced_coords.push(Coord(ending_row, ending_col));
  173.  
  174. while (!stack_of_retraced_coords.empty())
  175. {
  176. // Get the coordinates of whatever is currently on top of the stack
  177. // Then make decisions based on those coordinates
  178. Coord current = stack_of_retraced_coords.top();
  179. stack_of_retraced_coords.pop();
  180.  
  181. int current_row = current.r();
  182. int current_col = current.c();
  183.  
  184. if (current_row == starting_row && current_col == starting_col)
  185. return true;
  186.  
  187. // Remember to use IF statements for the following, not else if statements
  188. // You need to put each and every possible point on the stack
  189.  
  190. // First, check EAST, which is (r, c+1)
  191. if (maze[current_row][current_col + 1] == '.')
  192. {
  193. stack_of_coordinates.push(Coord(current_row, current_col + 1));
  194. maze[current_row][current_col + 1] = 'D';
  195. }
  196.  
  197. // Second, check NORTH, which is (r-1, c)
  198. if (maze[current_row - 1][current_col] == '.')
  199. {
  200. stack_of_coordinates.push(Coord(current_row - 1, current_col));
  201. maze[current_row - 1][current_col] = 'D';
  202. }
  203.  
  204. // Third, check WEST, which is (r, c-1)
  205. if (maze[current_row][current_col - 1] == '.')
  206. {
  207. stack_of_coordinates.push(Coord(current_row, current_col - 1));
  208. maze[current_row][current_col - 1] = 'D';
  209. }
  210.  
  211. // Last, check SOUTH, which is (r+1, c)
  212. if (maze[current_row + 1][current_col] == '.')
  213. {
  214. stack_of_coordinates.push(Coord(current_row + 1, current_col));
  215. maze[current_row + 1][current_col] = 'D';
  216. }
  217. }
  218.  
  219. }
  220.  
  221. int main()
  222. {
  223. string maze1[10] = {
  224. "XXXXXXXXXX",
  225. "X.X..X...X",
  226. "X.XX.X.XXX",
  227. "X....X.X.X",
  228. "XX.X.X...X",
  229. "XXX..X.X.X",
  230. "X...X...XX",
  231. "X.XX..X.XX",
  232. "X....X...X",
  233. "XXXXXXXXXX",
  234. };
  235.  
  236. string maze2[10] = {
  237. "XXXXXXXXXX",
  238. "X.X..X...X",
  239. "XXXX.X.XXX",
  240. "X....X.X.X",
  241. "XX.X.X...X",
  242. "XXX..X.X.X",
  243. "X...X...XX",
  244. "X.XX..X.XX",
  245. "X....X...X",
  246. "XXXXXXXXXX",
  247. };
  248.  
  249. string maze3[10] = {
  250. "XXXXXXXXXX",
  251. "XX.....XXX",
  252. "X..XX....X",
  253. "X...X...XX",
  254. "X.X.XXX..X",
  255. "XXXX..X..X",
  256. "XX....X..X",
  257. "X.......XX",
  258. "X..XXXXXXX",
  259. "XXXXXXXXXX",
  260. };
  261.  
  262. string maze4[10] = {
  263. "XXXXXXXXXX",
  264. "XX.....XXX",
  265. "X..XX....X",
  266. "X...X...XX",
  267. "X.X.XXX..X",
  268. "XXXX..X..X",
  269. "XX....X..X",
  270. "X.X.....XX",
  271. "X..XXXXXXX",
  272. "XXXXXXXXXX",
  273. };
  274.  
  275. assert(pathExists(maze1, 10, 10, 8, 6, 1, 1));
  276. assert(!pathExists(maze2, 10, 10, 8, 6, 1, 1));
  277. assert(pathExists(maze3, 10, 10, 4, 3, 7, 1));
  278. assert(!pathExists(maze4, 10, 10, 4, 3, 7, 1));
  279.  
  280. printMaze(maze1, 10, 10);
  281. printMaze(maze2, 10, 10);
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement