#if 1 #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) { // If this location is out of bounds, we've gone off edge, so stop if (!inBounds(numRows, numCols, row, col)) { return; } int thisElevation = map[row][col]; // There is lava entering (row,col) at the given elevation. // BUT if elevation of current cell is higher, this cell stops the lava if (thisElevation > inElevation) { return; // we stopped the lava! } // OR if this cell is already scorched, no need to handle this one again if (thisElevation == 0) { return; // already scorched } // Okay, this cell is scorched // mark with zero map[row][col] = 0; // Lava can flow from this point, at this elevation, to four surrounding areas. doLavaFlow(map, numRows, numCols, row-1, col , thisElevation); doLavaFlow(map, numRows, numCols, row+1, col , thisElevation); doLavaFlow(map, numRows, numCols, row , col-1, thisElevation); doLavaFlow(map, numRows, numCols, row , col+1, thisElevation); return; } int** readMapFromFile (string filename, int &numRows, int &numCols) { // Read number of rows and columns ifstream fin(filename.c_str()); if (!fin) { cout << "Failed to open file!" << endl; exit(1); } fin >> numRows >> numCols; // Make the 2D array int **map = new int*[numRows]; for (int i=0; i> map[i][j]; } } return map; } void printMap (int **map, int numRows, int numCols) { for (int i=0; i= numRows || col < 0 || col >= numCols) { return false; } return true; } #endif