Advertisement
Cinestra

Accounting for walking in a Circle

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