28
6
Introduction
Knowledgeable code golfers prepared us for the doomsday flood. Areas at risk were evacuated, and the population moved to high ground.
We underestimated the flood (or perhaps there was a bug in @user12345's code). Some high-ground areas are rapidly approaching sea level. Walls must be erected in order to ensure the survival of the now densely populated encampments. Sadly, the government has a limited supply of walls.
Problem
Our doomsday scenario is described by two numbers on a single line, n
and m
. Following that line are n
lines with m
values per line, separated only by a single space. Each value will be one of four characters.
x
Impassable. Water cannot flow here. Walls cannot be erected here.-
Unstable. Water can flow through this here. Walls cannot be erected here..
Stable. Water can flow through here. Walls can be erected here.o
Encampment. Water can flow through here. If it does, everyone dies. Walls cannot be built here.
Water will flow from all edges of the map, unless the edge is impassable or a wall is constructed on the tile. Write a program that can output the minimum number of walls required to protect an encampment.
Example Input
6 7
x . . x x x x
x . . x - - x
x . x x - - x
x . o o o - .
x . o o o - .
x x x x x x x
Example Output
3
Assumptions
- Water only flows orthogonally
- Encampments only exist as one orthonagonally contiguous block per scenario
- A solution will always exist (although it may require copious amounts of walls)
- Encampments cannot be located on an edge, as the scenario would then have no solution
- 2 <
n
< 16 - 2 <
m
< 16 - Input may be provided from stdin, read from "city.txt", or accepted as a single argument
Shortest code wins!
2Would it be acceptable for the program to be correct, yet take longer than the known universe has existed to provide a solution for certain instances of the problem? – Claudiu – 2014-03-07T19:53:24.393
@Claudiu I am somewhat new to Code Golf. My requirement failed to specify a time limit, so one does not exist. The burden falls to the answer to prove that a solution is correct for all instances of the problem. If you have a solution that solves some (but not all) instances in a clever/cool way, I still encourage you to post it just for fun. – Rainbolt – 2014-03-07T20:06:40.423
2Code golf typically does not require time restrictions. – None – 2014-03-07T20:37:21.517
Cool! Another Q: is input required to be as you specified or can we put it in another form? – Claudiu – 2014-03-07T21:47:21.017
@Claudiu I can't accept anything outside of the requirements. You can, however, suggest an edit to the requirements using the edit button. Seeing as there are no answers yet, I'll probably accept the edit right away. – Rainbolt – 2014-03-07T21:54:59.290
@Rusher: Ok I suggested the edit. What it will serve to do is remove the parsing-the-input part from the problem. So python could slurp all that with
input()
instead of having to parse it. up to you if you like the idea! – Claudiu – 2014-03-07T22:00:36.480@Claudiu The edit "allow for other forms of input" wasn't nearly specific enough. I think the current allowance is fair, so let's keep it that way. – Rainbolt – 2014-03-07T22:04:17.213
+1, interesting challenge. The requirement that input filename have length 8 seems rather arbitrary to me though. – Kaya – 2014-03-08T02:26:53.873
I wonder if brute force will always be shorter, or if doing a proper algorithm (I think it's a max-flow/min-cut problem?) could be shorter.. – Claudiu – 2014-03-08T02:56:02.443
@kaya Sorry, this is my second question here. I couldn't imagine all the crazy places people would pull their inputs from if I didn't specify, but I'll leave the file name open next time. Do you think the spec would look good then? (Sorry.. I know this is kind of chatty!) – Rainbolt – 2014-03-09T06:29:04.507