简体   繁体   中英

Finding enclosed sections of a graph algorithm

Given a 30 x 30 image of red and green pixels, stored as an array of 0s and 1s with 1 being red and 0 being green.

The image starts out as green and random patterns of red gets drawn on top.

All of the outer most pixels of the image are also painted red.

The question is how to fill every single pockets of green that's not connected to the biggest pocket of green with red?

Get/write a flood-fill algorithm that, starting at some pixel, fills all connected pixels with a different value and, while doing that, counts the pixels.

Have an int biggestSize = 0 and a Point biggestStartPoint = null variable with the given initial values.

Scan the image.

When you get at a green pixel, flood-fill with blue.

If the count of that flood-fill is bigger than biggestSize , flood-fill the old biggest area (from biggestStartPoint ) with red. Store the new count and start pixel in biggestSize and biggestStartPoint .

If the count isn't bigger, flood-fill the (now blue) region with red, and leave the variables unchanged.

Finally, flood-fill the biggest area with green.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM