Lawnmower Patterns

Alice and Bob have a lawn in front of their house, shaped like an N metre by M metre rectangle. Each year, they try to cut the lawn in some interesting pattern. They used to do their cutting with shears, which was very time-consuming; but now they have a new automatic lawnmower with multiple settings, and they want to try it out.

The new lawnmower has a height setting - you can set it to any height h between 1 and 100 millimetres, and it will cut all the grass higher than h it encounters to height h. You run it by entering the lawn at any part of the edge of the lawn; then the lawnmower goes in a straight line, perpendicular to the edge of the lawn it entered, cutting grass in a swath 1m wide, until it exits the lawn on the other side. The lawnmower's height can be set only when it is not on the lawn.

Alice and Bob have a number of various patterns of grass that they could have on their lawn. For each of those, they want to know whether it's possible to cut the grass into this pattern with their new lawnmower. Each pattern is described by specifying the height of the grass on each 1m x 1m square of the lawn.

The grass is initially 100mm high on the whole lawn.

Write a function which takes a 2D array of integer heights and determines whether the lawn can be cut accordingly.

Here are 100 test cases from Google Code Jam. The first 35 cases should pass, the rest should not.

Shortest code wins.

• Can you point out the licence under which the Google Code Jam problems are test cases are made available? Apr 15 '13 at 14:13
• I've changed it to link to the problem rather than copying it directly. Apr 15 '13 at 15:31
• I think it is very unfortunate if the puzzle/question is presented here with only a link. The problem should be provided in full with all necessary detail. Apr 15 '13 at 15:34
• "All problem statements, input data and contest analyses are licensed under the Creative Commons Attribution License." ~From the problem page Apr 15 '13 at 22:00

J, 15 chars

   (-:>./"1<./>./)


Didn't expect this short solution.

Short explanation:

(input == ((max of rows of input) table with min of left and right (max in columns of input)))
(      -:          >./"1                        <./                       >./              )


If your function is 4 other function like in the solution: (f1 f2 f3 f4) and an input J computes it like f1(input,f3(f2(input),f4(input))) i.e. input f1 ((f2 input) f3 (f4 input)).

• please explain the algorithm (I can't read J code ^^) Apr 15 '13 at 8:21
• @OlivierDulac Just added right now. Apr 15 '13 at 8:26

APL, 15 characters

{⍵≡(⌈/⍵)∘.⌊⌈⌿⍵}


Explanation:

• ⌈⌿⍵ calculates the maximum of the columns of rhs
• ⌈/⍵ does the same with rows
• ∘.⌊ does the outer product of the previous two with respect to function minimum
• ⍵≡ compares the result to the rhs

Examples:

      {⍵≡(⌈/⍵)∘.⌊⌈⌿⍵} 3 3 ⍴ 2 1 2 1 1 1 2 1 2
1

{⍵≡(⌈/⍵)∘.⌊⌈⌿⍵} 2 3 ⍴ 2 2 2 2 1 1
0


Python, 112 104

z=zip
y=lambda l:[[max(r)]*len(r)for r in l]
f=lambda l:l==[map(min,z(*r))for r in z(y(l),z(*y(z(*l))))]

• Use map(min,z(*r)) instead of [min(a)for a in z(*r)] to save 8 characters Apr 24 '13 at 12:34