UVA 572 Oil Deposits

#include <cstdio>
char data[150][150];
int row, column, dir[8][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};

bool find(int x, int y) {
	if (data[x][y] == '*' || x < 0 || x == row || y < 0 || y == column) //遇到边界、已来过、非油田就结束
		return false;
	data[x][y] = '*';	//标记此处油田已来过
	for (int i = 0; i < 8; i++) //遍历 8 个方向
		find(x + dir[i][0], y + dir[i][1]);		
	return true;
}

int main() {
	while (scanf("%d%d", &row, &column), row) {
		for (int i = 0 ; i < row; i++)
			scanf("%s", data[i]);

		int count = 0;
		for (int i = 0; i < row; i++)
			for (int j = 0; j < column; j++)
				if (find(i, j)) //寻找互相连接的油田
					count++;

		printf("%d\n", count);
	}
	return 0;
}


你可能感兴趣的:(UVA 572 Oil Deposits)