40
8
The Problem
A doomsday scenario is described by three numbers on a single line, n
, m
, and p
. Following that line are n
lines with m
values per line. Each value represents the total units of water each cell can hold.
The following p
lines describe the weather for the next p
days. 1 unit of rain falls on a single cell each day. If the amount of water in a cell exceeds the amount it can hold, that cell floods. If multiple adjacent cells are at full capacity, they are treated as one cell that share common neighbors (think Minesweeper when you click on a group of blanks).
- A single middle cell has 4 neighbors
- Two adjacent, full capacity middle cells are treated as one cell that has 6 neighbors
- A single corner cell has 2 neighbors
- A single wall cell has 3 neighbors
When a cell floods, a flood event occurs. All excess water is evenly distributed to its neighbors. If that causes one or more neighbors to flood, then another flood event occurs. This continues until the water has settled, or the city has completely flooded.
Example Input
7 5 3
3 2 3 4 5
2 2 0 3 4
1 1 2 3 3
4 1 2 2 2
4 1 1 2 2
4 4 1 2 2
4 4 2 2 2
0 0
1 2
4 3
0 0
means it rained on row 1, col 11 2
means it rained on row 2, col 3 (which can hold zero water and immediately floods!)
After p
days of rain, if the city is completely flooded, output Sink. Otherwise, output Swim.
Example Output
Swim
Assumptions
- Input may be provided through stdin, read from "city.txt", or accepted as an argument. All three are allowed so as not to invalidate any answers already posted.
- Water capacities will be non-negative integers.
40+ teams of undergrad college students (from A&M, UT, LSU, Rice, Baylor, etc.) competing in a programming contest with a variety of languages available could not solve this problem in 5 hours. Because of that, I can't help but mention that there is a catch to this puzzle that makes the solution trivial. Shortest code still wins, because I am that confident that the shortest code will also solve the puzzle.
Is it
n
lines ofm
values or the other way around? Your example doesn't match the written specification. – algorithmshark – 11 years ago@algorithmshark Corrected – Rainbolt – 11 years ago
13I'm not sure, but it seems to me that you sink if the amount of rain is larger than the sum of rain that all the squares can hold; otherwise you float. Is this it? – None – 11 years ago
2@hosch250 Spoiling the fun! – Rainbolt – 11 years ago
I used to say "I sink, therefore I swim." Unfortunately, I'm now carrying enough fat that I no longer have negative buoyancy. – keshlam – 11 years ago
Yeah, you should have held off mentioning the shortcut. It's definitely facepalm-worthy if you missed it. – keshlam – 11 years ago
1"The excess water is evenly distributed to its neighbors." - that is likely to be 1 unit of water. Does it distribute as e.g.
0.25
units to each adjacent cell (assuming a single middle flooded cell)? – Neil Slater – 11 years agoOK, got it, that doesn't really matter, does it? – Neil Slater – 11 years ago
@NeilSlater 0.25 to each neighbor if they aren't already flooded and if it is a middle cell. If one neighbor is flooded, they are treated as one and the water is evenly distributed to ALL neighbors of both cells. For example, two adjacent flooded cells have 6 neighbors. A wall cell would distribute 0.3 repeating to each neighbor, and a corner would distribute 0.5. – Rainbolt – 11 years ago
@John: Thanks, got it. The trap is trying to run the described process as a simulation as opposed to spotting a key simplifying property of it. – Neil Slater – 11 years ago