19
2
Imagine a two-dimensional array of boolean values, where the 0s represent squares of grass on a rectangular plot of land and the 1s represent fences.
Write a function that accepts the 2D array as an input and determines whether you can travel from any one area of grass to any other area of grass, using only north/east/west/south movements, without running into a fence.
If any area of grass in the array is fully enclosed by fences (meaning you can't travel N/E/W/S to reach every other area of grass in the array) the function should return false; otherwise, it should return true.
Below are two sample arrays you can use as inputs, although your function should be able to handle not just these but any 2D array of boolean values:
0 0 0 0 0
0 1 0 0 0
0 1 1 1 1
0 0 0 0 0
0 0 0 1 1
(should return true)
0 1 0 1 0
0 1 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 1 1 0
(should return false, since the middle 0 in the top row is fully enclosed)
Shortest working code wins. I'll pick the winner after either a week has passed or there have been no new submissions within 24 hours.
Can you also forbid binary operators? I'd love to see what people would come up with. – Pierre Arlaud – 2014-01-02T14:28:09.243
I do believe this is very similar to a USACO problem from last year (2012/2013 season). There are some huge test cases that can be accessed there... – apnorton – 2014-01-02T14:52:05.600
Will the size of the array always be 5*5? – ProgramFOX – 2014-01-02T15:02:41.763
Is it also allowed to output '1' instead of 'true' and '0' instead of 'false'? – ProgramFOX – 2014-01-02T15:10:10.673
1@ProgramFOX Assume array could be any height, any width. And sure, output anything boolean. – jawns317 – 2014-01-02T16:02:40.000
1what about the 3X3 matrix
1 1 1
;1 0 1
;1 1 1
? There is one grass cell in the center. Visually the grass cell in the center is fully enclosed by fences, but by your definition it is not. – emory – 2014-01-03T14:37:04.017Similar: http://codegolf.stackexchange.com/questions/801/is-this-figure-connected - test if all
– Howard – 2014-01-04T13:16:58.2170
are connected.@jawns317: I've added some test cases (see "ungolved version"). Is that what you supposed to get? – Martin Thoma – 2014-01-06T17:10:00.357
Can you reformulate the task to: "Starting at a 0: Is there any 0 that you cannot reach?"? – Martin Thoma – 2014-01-06T17:13:05.403
What if the array is all
1
s? What should it return then? – Joe – 2014-01-06T18:10:20.603