Consider a NxN pixel grid with up to M objects drawn on it, either squares or diamonds:
The objects may overlap, so recognition is hard. The task is to give the minimal possible numbers of objects per shape that can be "seen" in the picture and tell how many squares, how many diamonds, and how many objects with unknown shape there are.
You can find a reference algorithm that solves the problem here.
Here are some examples with their intended numbers (nsquare, ndiamond, nunknown). The examples with nunknown = 1 are those with pixels that may either come from a square or a diamond (highlighted in black, but not bearing any information that may be used).
The program with shortest mean runtime in terms of grid size N wins!
And this is a simple test case generator:
let N = 10
// initialize the grid
let grid = Array.from(Array(N), () => new Array(N));
// choose a random number of objects <= M
let n = Math.ceil(Math.random()*M)
// create objects
while (n-- > 0) {
// choose random position, but not on the border of the grid
var i = Math.ceil((N-2)*Math.random())
var j = Math.ceil((N-2)*Math.random())
if (Math.random() > 0.5) {
// draw a square
for (var k = -1; k <=1; k++) {
for (var l = -1; l <=1; l++) {
grid[i+k][j+l] = 1
}
}
} else {
// draw a diamond
for (var k = -1; k <=1; k++) {
for (var l = -1; l <=1; l++) {
if (!(k && l)) {
grid[i+k][j+l] = 1
}
}
}
}
}
Some small test cases:
(2,0,1)
examples, why isn't it 3 or 5 squares? Since the very center pixel could be the center of a square centered around it, overlapping with the other two. And the two pixels on either side of it could also be the center of a square, unless they're not allowed to touch, which doesn 't seem to be specified in the question. \$\endgroup\$