Re: Finding blocks of symbols on a 2dimensional array



"efialtis" <efialtis@xxxxxxxxxxx> wrote in message
news:pan.2006.01.09.16.41.59.756751@xxxxxxxxxxxxxx
> I am creating a program which plays a game. The game is played
> on a NxN square board. In every position of the board we may have
> a ball or not. Take for example the following 5x5 board ( x : ball,
> o : empty space).
>
> x x o o x
> x o o o o
> o o x x o
> o o o x x
> x x o o o
>
> I want to write a function which calculates the number of ball blocks
> on a specific board. For example the above board has 4 ball blocks
> (1 or more balls being surrounded by empty spaces).

It looks like you need a "flood-fill" algorithm. I'm sure you can find
something suitable from Google (it's quite simple to do recursively).

Suppose you write that function with prototype:

void flood_fill(int **board, int size, int x, int y, int val);

Then, to count the blocks, you can do something like:

int count_blocks(int **board, int size) {
int blocks = 0;
/* find and count blocks */
for (y = 0; y < size; ++y) for (x = 0; x < size; ++x) {
if (board[y][x] == BALL) {
++blocks;
flood_fill(board, size, x, y, DONE);
}
}
/* undo flood-fills */
for (y = 0; y < size; ++y) for (x = 0; x < size; ++x) {
if (board[y][x] == DONE) board[y][x] = BALL;
}
return blocks;
}

Alex


.



Relevant Pages

  • Re: Finding blocks of symbols on a 2dimensional array
    ... > I am creating a program which plays a game. ... > o: empty space). ... For example the above board has 4 ball blocks ... > The problem is that i cannot think of any elegant algorithm to ...
    (comp.lang.c)
  • Re: Finding blocks of symbols on a 2dimensional array
    ... > I am creating a program which plays a game. ... > on a NxN square board. ... > o: empty space). ... For example the above board has 4 ball blocks ...
    (comp.lang.c)
  • Finding blocks of symbols on a 2dimensional array
    ... I am creating a program which plays a game. ... on a NxN square board. ... o: empty space). ... I want to write a function which calculates the number of ball blocks ...
    (comp.lang.c)
  • Re: CryptRL 0.6340 first ASCII release
    ... taken more personally than general game comments. ... which has over 300 public members. ... int getDexterite() ... void setDexterite_Mod ...
    (rec.games.roguelike.development)
  • Re: CryptRL 0.6340 first ASCII release
    ... taken more personally than general game comments. ... int getDexterite() ... void setDexterite_Mod ... These 12 members can be replaced with just a few: ...
    (rec.games.roguelike.development)