Lake Counting S

爱学习的小许?發表於2024-11-24

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

const int N = 100;
int n, m;
char g[N][N];
bool st[N][N];//存狀態:淹過or沒淹過
int res = 0;
int dx[] = {1,1,1,0,0,-1,-1,-1};//八個方向
int dy[] = { -1,0,1,1,-1,1,0,-1 };


void dfs(int x, int y)
{

	for (int i = 0; i < 8; i++)
	{
		int a = x + dx[i], b = y + dy[i];
		if (a < 0 || b < 0 || a >= n || b >= m) continue;//不能越界
		if (g[a][b] != 'W') continue;//旱地不走
		if (st[a][b])continue;//淹過不走
		st[a][b] = true;
		dfs(a, b);
	}
}

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) scanf_s("%s", g[i]);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (g[i][j] && !st[i][j])
			{
				dfs(i, j);
				res++;
			}
		}
	}

	printf("%d", res);
	return 0;
}

相關文章