encircling and counting no of objects in a grid
There is a square grid in which there are empty and filled blocks. A no of contiguous blocks form an object. My bot can find out the status of it's neighbouring 8 blocks (whether they are filled or not). It can move to a neighbouring position if there is no filled block over there.
My code works for the case w开发者_JS百科hen there are no tight spaces (i.e., when objects have atleast 2 empty blocks between them). But, in the case when there can be a single space between 2 blocks, since my bot has no way of knowing if the neighbouring filled blocks belong to the same or different objects, it fails to encircle the object.
Is there a way to get around this problem?
How about this: when the bot is encircling an object, it is modifying its internal map, changing the border squares of the object from "unknown" to "filled". If it finds itself adjacent to two (or more) sold blocks, the one it should pay attention to is the one marked "unknown" with a "filled" neighbor.
There are still some peculiar cases to deal with, and a lot depends on some design choices-- whether diagonal neighbors are "contiguous", and if so how to deal with checkerboards, whether to mark a square before or after finding the next one, and so on. But the combination of map marks and visible blocks should give enough information for the bot to keep its bearings.
精彩评论