#include #include #include using namespace std; // This program reads in a 2D description of elevations. // Each elevation is an integer between 1 and 9, for simplicity // It then simulates a lava flow from (row,col). // In particular, lava flows from a point to any other adjacent point // WITH THE SAME ELEVATION or less. // The program then prints out a "map" showing what got scorched by lava. int** readMapFromFile (string filename, int &numRows, int &numCols); void printMap (int **map, int numRows, int numCols); void doLavaFlow(int **map, int numRows, int numCols, int row, int col, int elevation); bool inBounds (int numRows, int numCols, int row, int col); int main() { int numRows, numCols; int **map = readMapFromFile("map.txt", numRows, numCols); printMap(map, numRows, numCols); // Ask user for where lava flow is int row, col; char junk; cout << "Enter flow start location like (3,0): "; cin >> junk >> row >> junk >> col >> junk; // Simulate lava flow // For simplicity, we set the elevation of each "scorched" location to 0. // This function requires that we provide the starting elevation int elevation = map[row][col]; doLavaFlow(map, numRows, numCols, row, col, elevation); // print out everywhere that got scorched printMap(map, numRows, numCols); return 0; } // Assume a lava flow is entering (row,col) from an elevation of "inElevation". // If (row,col) has a greater elevation, the lava flow is stopped. // Otherwise, (row,col) gets scorched and lava may flow up, down, left, and right from this cell. // Every "scorched" cell gets set to an elevation of zero. void doLavaFlow(int **map, int numRows, int numCols, int row, int col, int inElevation) { //base case //check for in bounds if (row < 0 || row >= numRows || col < 0 || col >= numCols) return; int myElevation = map[row][col]; //higher elevation, stop the lava if (myElevation > inElevation) { return; } //if already schorched, no need to do it again if (myElevation == 0) { return; } //else, set elevation to 0 map[row][col] = 0; //recursive case // Lava can flow from this point, at this elevation, to four surrounding areas. doLavaFlow(map, numRows, numCols, row-1, col, myElevation); doLavaFlow(map, numRows, numCols, row+1, col, myElevation); doLavaFlow(map, numRows, numCols, row, col-1, myElevation); doLavaFlow(map, numRows, numCols, row, col+1, myElevation); return; }