tags:

views:

51

answers:

1

I have a grid like this:

  1234567
1 ACBDBAB
2 ABDBABC
3 ABADBAB
4 BABDAAB
5 BABCDBA
6 BDBABCB
7 ABBCBAB

Given a certain coordinate, for example (3:4), I'd like to find all the other blocks with the same letter that have at least one common side with the original block and one of those block (recursively). On my example, I'd like the following blocks:

  1234567
1 .......
2 .......
3 .......
4 ..B....
5 ..B....
6 ..B....
7 .BB....

My current idea is to check the original column up and down, by incrementing and decrementing the row number until the letter is different, in my example this would give me row numbers (4, 5, 6, 7). Then, I increment the column number and check my previous row numbers, in my example, none of them are of the original letter, so I start decrementing, I check 4, 5, 6 and 7 at column 2, and I see only 7 matches, so I continue to column 1 and I check row 7 and so on.

+2  A: 

I believe you are looking for the flood fill algorithm.

Edit: I gave some thought to your proposed algorithm and realized why it wouldn't work. The problem is that it only detects convex areas. Say you have a grid like this:

BAB
BBB
BAB

And you would like to replace all the B's with C's. If you started your algorithm from the B in the center of the grid, you'd get this:

BAB
CCC
BAB
int3