#include #include using namespace std; bool findpath(int row, int col); /* When creating a multi-dimensional array, you can leave the first pair of [backets] empty if you have specified an initial value for every element. The remaining pairs must be filled in. */ char a[][14] { {'X', 'X', 'B', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, {'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, {'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, {'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, {'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}, {' ', ' ', ' ', 'X', 'X', 'X', 'X', ' ', ' ', ' ', ' ', ' ', ' ', 'X'}, {'X', 'X', ' ', 'X', 'X', 'X', 'X', ' ', 'X', ' ', 'X', 'X', ' ', 'X'}, {'X', 'X', ' ', ' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', ' ', ' ', 'X'}, {'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X', 'X', 'X'}, {'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X', 'X', 'X'}, {'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X', 'X', 'X'}, {'X', 'X', ' ', ' ', 'X', ' ', ' ', ' ', ' ', ' ', 'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X', 'X', ' ', ' ', ' ', ' ', 'X', 'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X'}, {'X', ' ', ' ', ' ', ' ', ' ', ' ', 'X', ' ', 'X', 'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'E', 'X', 'X', 'X', 'X', 'X'} }; const size_t nrows {size(a)}; //how many rows const size_t ncols {size(a[0])}; //how many columns int main() { //Find the 'B' at the beginning of the path. int brow {nrows}; int bcol {ncols}; for (int row {0}; row < nrows; ++row) { for (int col {0}; col < ncols; ++col) { if (a[row][col] == 'B') { brow = row; //Record the position of the 'B'. bcol = col; } } } if (brow == nrows || bcol == ncols) { cerr << "There was no 'B'.\n"; return EXIT_FAILURE; } if (!findpath(brow, bcol)) { //If findpath returned false, cerr << "There is no path from the 'B' to the 'E'.\n"; return EXIT_FAILURE; } //Display the maze, including the path that findpath drew. for (int row {0}; row < nrows; ++row) { for (int col {0}; col < ncols; ++col) { cout << a[row][col]; } cout << "\n"; //at the end of each row } return EXIT_SUCCESS; } //Return true if there is a path from (row, col) to 'E' (the end). //If so, draw the path with dots. //Otherwise, return false. bool findpath(int row, int col) { if (row < 0 || row >= nrows || col < 0 || col >= ncols) { //Error check return false; //because (row, col) is off the board } if (a[row][col] == 'E') { //If we're already at the destination, then return true; //the job is already finished. Do nothing. } //Do only the first part of the job: take just one step. //Tentatively assume that (row, col) is on a path to 'E'. if (a[row][col] == ' ') { //If (row, col) is empty, a[row][col] = '.'; //step on it. } //Now take four stabs at doing the rest of the job. struct direction { int drow; int dcol; }; const direction directions[] { //an array of 4 structures { 1, 0}, //right { 0, -1}, //up {-1, 0}, //left { 0, 1} //down }; for (const auto& direction: directions) { //(r, c) is a neighbor of (row, col). const int r {row + direction.drow}; const int c {col + direction.dcol}; //If we can step into (r, c) and it leads to a path to 'E', if ((a[r][c] == ' ' || a[r][c] == 'E') && findpath(r, c)) { return true; } } //Backtrack: erase the dot. Our tentative assumption was wrong. //It turns out that (row, col) is not on any path to 'E'. a[row][col] = ' '; return false; }