UVA – 572 – Oil Deposits

#include <iostream>
#include <string.h>
#include <queue>

using namespace std;
#define MAX 150
#define FOR(ii, i0, in) for((ii)=(i0); (ii)<(in); (ii)++)
#define FORd(ii, jj, i0, j0, in, jn) FOR(ii, i0, in) FOR(jj, j0, jn)
#define reSet(ZZ) memset(ZZ, 0, sizeof ZZ)
typedef pair<int, int> Point;

int colors;
void spread(int i, int j, char g[MAX][MAX]) {
	if (g[i][j] != '@') return;
	g[i][j] = '*';
	for (int y = -1; y < 2; y++)
		for (int x = -1; x < 2; x++)
			if (x || y) spread(i + y, j + x, g);
}
int main() {
	int I, J, i, j;
	char g[MAX][MAX];
	while ((cin >> I >> J) && (I && J)) {

		memset(g, 0, sizeof g), memset(&colors, 0, 4);
		FORd(i, j, 0, 0, I, J)	cin >> g[i][j];
		FORd(i, j, 0, 0, I, J){
			if (g[i][j]!='@') continue;
			colors++;
			spread(i, j, g);
		}
		cout << colors << endl;
	}
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: