Given a Minecraft terrain as a 2-dimensional array of integers or similar datastructure, determine the maximum number of cacti that can be planted on it.
The integers represent the height (y) of the terrain at each location (x and z). A cactus cannot be planted next to higher terrain or next to another cactus at the same height (next to = either x or z deviates by 1 and the other coordinates are the same).
You can optionally make any of the following assumptions:
- at least some terrain exists
- all heights are between 1 and 100
Test cases
[[3, 3, 3, 3], [3, 3, 3, 3], [3, 3, 3, 3], [3, 3, 3, 3]] -> 8
[[7, 6, 5, 4], [6, 5, 4, 3], [5, 4, 3, 2], [4, 3, 2, 1]] -> 1
[[4, 3, 4, 1, 1, 3, 2, 4], [2, 4, 4, 2, 1, 2, 2, 3], [1, 1, 3, 2, 1, 2, 3, 2], [4, 4, 3, 1, 2, 2, 3, 3], [3, 2, 2, 2, 1, 1, 4, 4], [3, 2, 3, 1, 1, 1, 4, 1], [1, 3, 2, 2, 1, 3, 4, 3], [4, 4, 1, 1, 1, 1, 3, 2]] -> 17
[[5, 5, 5, 4, 5, 4, 4, 3, 1, 1, 4, 4, 4, 4, 3, 4], [5, 4, 4, 4, 4, 4, 3, 1, 1, 2, 4, 4, 3, 4, 4, 4], [5, 5, 4, 4, 5, 4, 4, 2, 1, 4, 4, 4, 4, 4, 2, 4], [4, 4, 4, 4, 4, 4, 3, 3, 3, 4, 3, 2, 4, 3, 2, 4], [4, 4, 4, 5, 4, 4, 4, 4, 4, 2, 1, 1, 1, 1, 3, 2], [4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 3, 2, 2, 4, 3, 3], [4, 4, 1, 2, 3, 4, 5, 5, 4, 4, 4, 3, 3, 4, 5, 4], [4, 4, 4, 3, 4, 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4], [4, 4, 2, 4, 4, 4, 3, 4, 5, 4, 4, 4, 5, 5, 5, 4], [4, 4, 3, 4, 3, 2, 3, 4, 4, 4, 4, 4, 5, 4, 4, 4], [4, 4, 4, 3, 1, 2, 2, 3, 4, 4, 4, 4, 5, 4, 4, 4], [4, 4, 3, 1, 2, 3, 2, 4, 4, 3, 3, 3, 4, 4, 4, 4], [4, 4, 4, 1, 1, 1, 1, 1, 3, 3, 2, 3, 4, 3, 3, 4], [4, 4, 4, 4, 3, 1, 2, 3, 2, 3, 2, 2, 4, 4, 3, 4], [4, 4, 5, 4, 4, 4, 4, 4, 4, 3, 1, 4, 4, 4, 4, 4], [5, 4, 5, 4, 4, 4, 4, 3, 2, 3, 3, 4, 4, 3, 4, 4]] -> 80
Scoring
This is code-golf
, the shortest submission (in bytes) for each language wins!
Lower bounds (approximations)
Eliminate all locations next to higher terrain and fill in the remaining areas with the checkerboard pattern. (thanks to Bubbler)
Eliminate all locations next to higher terrain, and repeatedly remove locations with minimal remaining neighbours along with their neighbours while counting them. (thanks to Jonathan Allan)