杭电1241 Oil Deposits-CSDN博客

35 阅读1分钟
#include<iostream>
#include<string>
using namespace std;


const int maxn = 105;
string map[maxn];
int row, column, res;


void dfs(int x, int y)
{//深度搜索 
	map[x][y] = '*';//遍历过的做上记号 
	for (int dx = -1; dx <= 1; dx++) {
		for (int dy = -1; dy <= 1; dy++) {
			int nx = x + dx;
			int ny = y + dy;
			if (0 <= nx&&nx < row && 0 <= column&&ny < column&&map[nx][ny] == '@')
				dfs(nx, ny);
		}//*米 3*3 =9 但是map[x][y]本身被标记了,所以是八个方向 
	}
	return;
}


void solve()
{
	res = 0;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < column; j++)
		{
			if (map[i][j] == '@') {
				dfs(i, j);
				res++;
			}
		}
	}
}


int main()
{
	//freopen("data.in","r",stdin); 
	while (cin >> row >> column, row || column)
	{
		for (int i = 0; i < row; i++)
			cin >> map[i];
		solve();
		cout << res << endl;
	}
	return 0;
}

\