Unable to understand fencing (FENCE)

Problem statement of FENCE is not at all clear to me. Can anyone explain in simpler a simpler way? I can’t understand what do the mean by “Two cells are adjacent if they have a common side.”, also can’t figure out how they are calculating length.

In this question, the index of plant will be given as input which forms matrix. If two plants have same side then no fencing is required. Consider 3*3 matrix. x show the index of plant and y indicates that the place is vacent.



(1, 1) (1, 2) (1,3)
(2, 1) (2, 2) (2, 3)
(3, 1) (3, 2) (3,3)

Since (1,1) and (2, 1) contains plant. So no fencing is required. (1,1) containts plant and (1, 2) don’t so here fencing is required. In same way for all other cell. Consider (2, 2) index all adjacent contains plant so no fencing is required. In case of (3, 2) cell only the adjacent (2, 2) contains plant so fencing required will be 3.
Consider the other one 1*1 matrix.


Here single plant. All side are vacent so fencing required is 4.

1 Like