22
4
The one-dimensional version of this problem was pretty easy, so here's a harder 2D version.
You are given a 2D array of land heights on standard input, and you have to figure out where the lakes will form when it rains. The height map is just a rectangular array of the numbers 0-9, inclusive.
8888888888
5664303498
6485322898
5675373666
7875555787
You must output the same array, replacing all locations that would be underwater with *.
8888888888
566*****98
6*85***898
5675*7*666
7875555787
Water can escape diagonally, so there would be no lake in this configuration:
888
838
388
shortest code wins. Your code must handle sizes up to 80 wide and 24 high.
Three more examples:
77777 77777
75657 7*6*7
75757 => 7*7*7
77677 77677
77477 77477
599999 599999
933339 9****9
936639 => 9*66*9
935539 9*55*9
932109 9****9
999999 999999
88888888 88888888
84482288 8**8**88
84452233 => 8**5**33
84482288 8**8**88
88888888 88888888
Is trailing whitespace in the output lines allowed? – hallvabo – 2012-04-04T10:29:14.977
@hallvabo: no. Why would you want to? – Keith Randall – 2012-04-04T19:36:03.263
Keith: I had another solution where I padded the input lines to a fixed width, and saved some bytes in the algorithm. If I have to remove the padding for the output, this approach takes more bytes than my currently best solution. – hallvabo – 2012-04-04T19:53:37.080
4Some more testcases would be nice, if possible (especially input you would consider an edge case). – Ventero – 2011-05-21T00:15:46.033