hdu 1241

1题目大意:给定一个图,上边有*@两种标记,其中@表示石油,好多@连在一起可以看成一个大的石油区,问在这个区域中有多少个石油区

#include<iostream>
using namespace std;
int n, m;
char grid[105][105]; //存储网格;

//以下定义了移动的8个方向
int dir[8][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, 1 }, { 0, -1 },
		{ 1, 1 }, { 1, 0 }, { 1, -1 } };

//从(x,y)位置进行DFS
void DFS(int x, int y) {
	int i, xx, yy;
	grid[x][y] = '*'; //进过后设置成*保证不会在经过了
	for (i = 0; i < 8; i++) {

		xx = x + dir[i][0];
		yy = y + dir[i][1];

		//判断是否坐标越界。以坐标(0,0)、(n,m)作为标准.可是总以为应该是(m,n)??有点奇怪
		if (xx >= n || yy >= m || xx < 0 || yy < 0) {
			continue;
		}
		if (grid[xx][yy] != '*') {
			DFS(xx, yy);
		}
	}
}
int main() {
	int i, j;
	int count; //统计数目
	while (cin >> n >> m, n) {
		cin.get(); //可以用来接收字符,在这里可以没有
		for (i = 0; i < n; i++) {
			for (j = 0; j < m; j++)
				cin >> grid[i][j];
			cin.get();
		}
		count = 0;
		for (i = 0; i < n; i++) {
			for (j = 0; j < m; j++) {
				if (grid[i][j] == '@') {
					DFS(i, j); //从(i,j)位置进行DFS;
					count++;//只有在所有能连成一片的油田连成一片的时候count才++
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/aukle/p/3231029.html