#include #include using namespace std; bool f(int row, int col); //When creating a multi-dimensional array, you can leave the first pair of //[backets] empty. 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'} }; int nrows {size(a)}; //how many rows int ncols {size(a[0])}; //how many columns int main() { //Find the 'B' (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 is no 'B'.\n"; return EXIT_FAILURE; } if (!f(brow, bcol)) { //If f returns false, cerr << "There is no path from the 'B' to the 'E'.\n"; return EXIT_FAILURE; } //Display the maze and the path. 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. //Return false otherwise. bool f(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; }; direction directions[] { //an array of structures { 1, 0}, //right { 0, -1}, //up {-1, 0}, //left { 0, 1} //down }; int n {size(directions)}; //how many directions for (int i {0}; i < n; ++i) { //(r, c) is a neighbor of (row, col). int r {row + directions[i].drow}; int c {col + directions[i].dcol}; //If we can step into (r, c) and it leads to a path to 'E', if ((a[r][c] == ' ' || a[r][c] == 'E') && f(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; }