125k views
2 votes
Flood!

There is a flood happening and you have to figure out what areas will be affected. You'll be provided a map indicating where the flooding is coming from and where the high grounds and low grounds are in order to figure which area will be flooded.
File Format



Start row and column are where flooding begins
Water amount is how many spaces this flood could fill
The row and columns coordinates range from 0 to (numRows-1) and 0 to (numCols-1)
The top left of the map would be considered 0,0
The map consists of:
Spaces (' ') = low ground, the areas that water can move into
H = High ground, water cannot flood or cross over
A file is invalid and the program should end with an error message if...
if file doesn't exist
if numRows are less than 1
if numCols are less than 1
if start position is not valid (i.e. off map or on high ground)
You may assume the map characters match the parameters given above.
Flooding Rules
Flooding starts at the start position
Water only moves orthogonally (up, right, down, or left) not diagonally
You must check for valid moves in the order: up, right, down, left
You cannot move through the high ground
If you run out of water the flooding stops
If no more spaces can be flooded, the flooding stops
Remember, the water cannot cross the high ground!
Output
After receiving a file from command you will output the path of the flood to the screen. Every space that was flooded will have a '~' to indicate it was flooded.
Also, indicate if the flood ran out of water or if it flooded the accessible area completely.
I've provided a few examples below.
Lab Discussion Points
Before starting implementation, design your code base. Design a class that will be in charge of reading the file.
Make a list of methods and member variables you think it should have
Design a class that, given a valid map, will traverse it.
Make a list of member variables and method you think it should have.
Discuss how you plan to detect the need for backtracking should you reach a deadend in the map. What will variables/objects/arrays will need to be updated when you backtrack?
Discuss how you plan to detect flooding all possible spaces OR running out of water
Additional Requirements
Do not use a stack object to solve the problem; use recursion.
Rubric
75% Correctly floods map
If possible floods until all reachable spaces are flooded
Correctly floods until out of water
All movement rules followed
5% Correctly detects invalid start positions
10% Code design
10% Attendance
Sample Runs
map.txt
5 4
1000
HHHHHHH
H
HH HH H
H HH H
H HHHHH
H HH
HHHHHHH
H H
Command line
./Lab09 map.txt
Output
Size: 8,7
Starting position: 5,4
HHHHHHH
~~~~~~H
HH~HH~H
H~~HH~H
H~HHHHH
H~~~~HH
HHHHHHH
H H
Flood complete.
Sample Run that runs out of water
waterLimit.txt
0 4
8
HHHH HH H
HH HH H
HH H
H HHHHH
H H
HHHHHHH
Command line
./Lab09 waterLimit.txt Output
Size: 7,7
Starting position: 0,4
HHHH~HH ~~~~H
HH~HH~H
HH~H
H HHHHH
H H
HHHHHHH
Flood ran out of water.
Sample Run with bad start position
badStart.txt
10 10
6
HHHHHH
HH HH HH H HHHH
H H
Command Line
./Lab09 badStart.txt Output
Invalid starting position

User AConsumer
by
7.5k points

1 Answer

0 votes

Answer:

To figure out which areas will be affected by the flood, you'll need to follow certain rules and guidelines. Here's how you can approach the problem:

1. Read the file: Design a class that reads the file and retrieves the necessary information like the map dimensions, start position, and water amount. This class should have methods to validate the file and check if it meets the specified requirements (e.g., file existence, valid start position, etc.).

2. Traverse the map: Design another class that will traverse the map based on the flooding rules. This class should have member variables to keep track of the current position and the remaining water amount. It should also have methods to check for valid moves and update the map accordingly.

3. Detect backtracking: When you reach a dead end on the map, you'll need to backtrack. To do this, you'll need to update variables, objects, or arrays that keep track of the previous positions and the paths taken. These variables will allow you to backtrack to a previous valid position and continue exploring other possible paths.

4. Detect flooding and water shortage: While traversing the map, you should check for two conditions:

a. If no more spaces can be flooded: If you find that there are no more low ground spaces adjacent to the current position, the flooding stops.

b. If you run out of water: If the remaining water amount becomes zero, the flooding stops.

5. Flood the map: Using recursion, start the flood at the specified start position. Move orthogonally (up, right, down, or left) to the adjacent spaces and check if they meet the flooding rules. If a space can be flooded, mark it with a '~' symbol to indicate it has been flooded. Continue this process until the flooding stops due to either running out of water or no more spaces to flood.

6. Output the flood path: After the flood is complete, print the updated map to the screen. The flooded spaces will be marked with '~'. Additionally, indicate whether the flood ran out of water or if it completely flooded the accessible area.

Remember to consider all the given rules, such as not crossing high ground, checking for valid moves in a specific order, and validating the start position. By following these steps, you should be able to accurately flood the map and provide the desired output.

Step-by-step explanation:

<3

User Intelfx
by
7.6k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.

9.4m questions

12.2m answers

Categories