Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- #include <queue>
- #include <stack>
- #include <string>
- using namespace std;
- class Coord
- {
- public:
- Coord(int r, int c) : m_row(r), m_col(c) {}
- int r() const { return m_row; }
- int c() const { return m_col; }
- private:
- int m_row;
- int m_col;
- };
- // Returns true if a path exists from (starting_row, starting_col) to (ending_row, ending_col)
- bool pathExists(string maze[], int number_of_rows, int number_of_cols, int starting_row, int starting_col, int ending_row, int ending_col)
- {
- // Initialize a Queue of Coordinates
- queue<Coord> queue_of_coordinates;
- // Push the Queue by putting in the starting row and starting col
- // Assignment instructions that starting point is always going to be a "." and not a wall
- queue_of_coordinates.push(Coord(starting_col, starting_row));
- // The maze is an array of strings
- // 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
- // We will overwrite that as D to mark it as discovered
- maze[starting_col][starting_row] = 'D';
- while (!queue_of_coordinates.empty())
- {
- // Pop the queue by taking the coordinate in queue and popping it
- // Different from stack because stack pops the most recent item whereas queue pops the item that has been there the longest
- Coord current = queue_of_coordinates.front();
- queue_of_coordinates.pop();
- int current_row = current.r();
- int current_col = current.c();
- if (current_row == ending_row && current_col == ending_col)
- return true;
- // Remember to use IF statements for the following, not else if statements
- // You need to put each and every possible point on the stack
- // First, check EAST, which is (r, c+1)
- if (maze[current_row][current_col + 1] == '.')
- {
- queue_of_coordinates.push(Coord(current_row, current_col + 1));
- maze[current_row][current_col + 1] = 'D';
- }
- // Second, check NORTH, which is (r-1, c)
- if (maze[current_row - 1][current_col] == '.')
- {
- queue_of_coordinates.push(Coord(current_row - 1, current_col));
- maze[current_row - 1][current_col] = 'D';
- }
- // Third, check WEST, which is (r, c-1)
- if (maze[current_row][current_col - 1] == '.')
- {
- queue_of_coordinates.push(Coord(current_row, current_col - 1));
- maze[current_row][current_col - 1] = 'D';
- }
- // Finally, check SOUTH, which is (r+1, c)
- if (maze[current_row + 1][current_col] == '.')
- {
- queue_of_coordinates.push(Coord(current_row + 1, current_col));
- maze[current_row + 1][current_col] = 'D';
- }
- }
- return false;
- }
- void printMaze(string maze[], int number_of_rows, int number_of_cols)
- {
- for (int i = 0; i < number_of_rows; i++)
- {
- for (int j = 0; j < number_of_cols; j++)
- {
- cout << maze[i][j];
- }
- cout << endl;
- }
- cout << endl;
- }
- bool checkSurrounndings(string maze[], int number_of_rows, int number_of_cols, int current_row, int current_col)
- {
- // First, check EAST, which is (r, c+1)
- if (maze[current_row][current_col + 1] == 'D' || )
- {
- stack_of_coordinates.push(Coord(current_row, current_col + 1));
- maze[current_row][current_col + 1] = 'D';
- }
- // Second, check NORTH, which is (r-1, c)
- if (maze[current_row - 1][current_col] == '.')
- {
- stack_of_coordinates.push(Coord(current_row - 1, current_col));
- maze[current_row - 1][current_col] = 'D';
- }
- // Third, check WEST, which is (r, c-1)
- if (maze[current_row][current_col - 1] == '.')
- {
- stack_of_coordinates.push(Coord(current_row, current_col - 1));
- maze[current_row][current_col - 1] = 'D';
- }
- // Last, check SOUTH, which is (r+1, c)
- if (maze[current_row + 1][current_col] == '.')
- {
- stack_of_coordinates.push(Coord(current_row + 1, current_col));
- maze[current_row + 1][current_col] = 'D';
- }
- }
- void findDirections(string maze[], int number_of_rows, int number_of_cols, int starting_row, int starting_col, int ending_row, int ending_col)
- {
- /*
- * Psuedo Code Approach:
- * First run to check if the path even exists
- * Checking if the path exists marks each discoverable area as a D
- * We will mark the start with a S and the end with an E
- * Let's start with the end point and check each connecting D
- *
- * For this approach because we are checking individual paths, we will use a stack
- *
- * We'll mark each point we retrace as R
- * Go through each D until you reach either the beginning or nowhere
- * If you reach the beginning, then that is the path and we will write directions
- * If you reach nowhere, we will mark that point as N and then go to the previous point
- * The previous point will check if there is another direction available (not checking the N)
- * If we keep changing points that lead nowhere to N, then eventually we will find a path
- *
- */
- /*
- Psuedo Code Approach #2:
- * First run to check if the path even exists
- * Checking if the path exists marks each discoverable area as a D
- *
- *
- */
- bool check_pathExists = pathExists(maze, number_of_rows, number_of_cols, starting_row, starting_col, ending_row, ending_col);
- if (check_pathExists == false)
- {
- cout << "There are no directions because there is no path that exists." << endl;
- return;
- }
- // If a path exists, but it's because you do nothing, simply say do nothing
- if (starting_row == ending_row && starting_col == ending_row)
- {
- cout << "There are no directions because the start point is the same as the end point." << endl;
- return;
- }
- stack<Coord> stack_of_retraced_coords;
- stack_of_retraced_coords.push(Coord(ending_row, ending_col));
- while (!stack_of_retraced_coords.empty())
- {
- // Get the coordinates of whatever is currently on top of the stack
- // Then make decisions based on those coordinates
- Coord current = stack_of_retraced_coords.top();
- stack_of_retraced_coords.pop();
- int current_row = current.r();
- int current_col = current.c();
- if (current_row == starting_row && current_col == starting_col)
- return true;
- // Remember to use IF statements for the following, not else if statements
- // You need to put each and every possible point on the stack
- // First, check EAST, which is (r, c+1)
- if (maze[current_row][current_col + 1] == '.')
- {
- stack_of_coordinates.push(Coord(current_row, current_col + 1));
- maze[current_row][current_col + 1] = 'D';
- }
- // Second, check NORTH, which is (r-1, c)
- if (maze[current_row - 1][current_col] == '.')
- {
- stack_of_coordinates.push(Coord(current_row - 1, current_col));
- maze[current_row - 1][current_col] = 'D';
- }
- // Third, check WEST, which is (r, c-1)
- if (maze[current_row][current_col - 1] == '.')
- {
- stack_of_coordinates.push(Coord(current_row, current_col - 1));
- maze[current_row][current_col - 1] = 'D';
- }
- // Last, check SOUTH, which is (r+1, c)
- if (maze[current_row + 1][current_col] == '.')
- {
- stack_of_coordinates.push(Coord(current_row + 1, current_col));
- maze[current_row + 1][current_col] = 'D';
- }
- }
- }
- int main()
- {
- string maze1[10] = {
- "XXXXXXXXXX",
- "X.X..X...X",
- "X.XX.X.XXX",
- "X....X.X.X",
- "XX.X.X...X",
- "XXX..X.X.X",
- "X...X...XX",
- "X.XX..X.XX",
- "X....X...X",
- "XXXXXXXXXX",
- };
- string maze2[10] = {
- "XXXXXXXXXX",
- "X.X..X...X",
- "XXXX.X.XXX",
- "X....X.X.X",
- "XX.X.X...X",
- "XXX..X.X.X",
- "X...X...XX",
- "X.XX..X.XX",
- "X....X...X",
- "XXXXXXXXXX",
- };
- string maze3[10] = {
- "XXXXXXXXXX",
- "XX.....XXX",
- "X..XX....X",
- "X...X...XX",
- "X.X.XXX..X",
- "XXXX..X..X",
- "XX....X..X",
- "X.......XX",
- "X..XXXXXXX",
- "XXXXXXXXXX",
- };
- string maze4[10] = {
- "XXXXXXXXXX",
- "XX.....XXX",
- "X..XX....X",
- "X...X...XX",
- "X.X.XXX..X",
- "XXXX..X..X",
- "XX....X..X",
- "X.X.....XX",
- "X..XXXXXXX",
- "XXXXXXXXXX",
- };
- assert(pathExists(maze1, 10, 10, 8, 6, 1, 1));
- assert(!pathExists(maze2, 10, 10, 8, 6, 1, 1));
- assert(pathExists(maze3, 10, 10, 4, 3, 7, 1));
- assert(!pathExists(maze4, 10, 10, 4, 3, 7, 1));
- printMaze(maze1, 10, 10);
- printMaze(maze2, 10, 10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement