Background
Being in a philosophical mood, I remembered one old idea. I think it is suitable for CG. Duplicates are not found, but it's of course just a kind of totalistic cellular automaton or erosion.
Given an infinite board filled with empty (let 0
) cells. We create a figure - a finite set of non-empty (let 1
) cells. Figure may be with holes, disconnected, random etc. But the bounding box of the figure is finite.
We call the border of a figure a subset of its cells that have at least one empty neighbor (in this model we use 4 neighbors: top, left, right, bottom).
One step of fading algorithm:
- Define a figure boundary
- Eliminate it, replacing by empty cells
Task:
Determine how many described steps it takes to completely disappear a given figure.
About size of board
Since the figure can only fade, we just have to deal with the board, just one strip padding figure' bounding box.
UPD - About minimal input
So strictly speaking the minimal board is [[0, 0], [0, 0]]
- empty board with no figure. And as mentioned in comments, you may not handling an empty array; sorry that made that point clear just now.
Input:
Figure in any appropriate form:
- binary array (all board)
- array of strings (too)
- sparse array (list of positions of non-empty cells and dimensions)
Output:
Non-negative number of steps, totally eliminating a figure.
Notes:
Of course, the literal passage of the algorithm to fixed point and counting steps is an acceptable solution. But I've got at least two addition ideas:
- Some built-ins of Mathematica ;) But I can't configure it properly (
- Perhaps one can analyze the maximum distances to the border, and get the answer only by a given array
Test cases:
[[0, 0], [0, 0]] → 0
[[0, 0 ,0], [0, 1, 0], [0, 0, 0]] → 1
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] → 3
Example of string input:
00000000
01111110
01111110
01111110 → 3
01111110
01111110
01111110
00000000
Example of sparse array input (dimensions and non-empty cells):
[16, 16],
[[2, 2], [2, 3], [2, 4], [2, 13], [2, 14], [2, 15], [3, 2], [3, 3], [3, 4], [3, 5], [3, 8], [3, 9], [3, 10], [3, 12], [3, 13], [3, 14], [3, 15], [4, 2], [4, 3], [4, 4], [4, 5], [4, 8], [4, 9], [4, 10], [4, 12], [4, 13], [4, 14], [4, 15], [5, 3], [5, 4], [5, 5], [5, 8], [5, 9], [5, 10], [5, 12], [5, 13], [5, 14], [6, 8], [6, 9], [6, 10], [7, 7], [7, 8], [7, 9], [7, 10], [8, 7], [8, 8], [8, 9], [8, 10], [9, 7], [9, 8], [9, 9], [9, 10], [11, 2], [11, 3], [11, 4], [12, 2], [12, 3], [12, 4], [12, 5], [12, 6], [12, 7], [12, 8], [12, 9], [12, 10], [12, 11], [12, 12], [12, 13], [12, 14], [13, 2], [13, 3], [13, 4], [13, 5], [13, 6], [13, 7], [13, 8], [13, 9], [13, 10], [13, 11], [13, 12], [13, 13], [13, 14], [13, 15], [14, 3], [14, 4], [14, 5], [14, 6], [14, 7], [14, 8], [14, 9], [14, 10], [14, 11], [14, 12], [14, 13], [14, 14], [14, 15], [15, 13], [15, 14], [15, 15]] → 2
[[1, 1 ,1], [1, 0, 1], [1, 1, 1]] → 1
. Because in all existing test cases, testing the neighbors without testing the cell itself gives the correct result. \$\endgroup\$