An algorithm to group cells into rectangles
Imagine the picture above represents a 6*6 array of ints where 0 is black. Is there a quick algorithm to split the non 0 cells into rectangles? Idea开发者_如何学Pythonlly the check would be contained within a for loop, without creating any other arrays.
for(x = 0; x < 6; x++)
for(y = 0; y < 6; y++)
if(cellIsBottomRightOfRect(x,y)) {
left = getLeft(x,y);
top = getTop(x,y);
printf("Rect: %d,%d %d,%d \n", left, top, x, y);
}
How about using a recursive method which calls itself everytime it encounters a color different than the one it is trying to group at the moment?
Have you thought about using a dynamic programming approach?
Also, I think that a search algorithm (like A*) would work well here (even though it is has exponential time complexity).
精彩评论