I've got a two-dimmentional array filled with some data. I want to select all the fields with the same value as field [0][0] that are connected to it in a way presented on the image below:
Example of matrix with fields I want to select highlighted in green (selected fields are adjacent to each other by at least one of four sides (N,W,S,E):
Can you think of some easy algorythm that will achieve that? I don't hope for some ready to use code - more like some guidelines.
Use a recursive algorithm. Check each of the adjacent cells, and if it matches recurse with that cell being the starting point. Collect all the matching cells in a list. To avoid infinite loops, check whether the cell is already in the result list before recursing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With