求助,萌新过不了,20分
查看原帖
求助,萌新过不了,20分
1054475
w_god楼主2023/9/25 13:28
#include<bits/stdc++.h>
using namespace std;
char a[110][110];
int n , m;
void dfs(int x , int y)
{
	if (a[x][y] == '.')
	{
		return;
	}
	if (x < 0 || y < 0 || x > (n - 1) || y > (m - 1))
	{
		return;
	}
	a[x][y] = '.';
	if (a[x + 1][y + 1] != '.')
	{
		dfs(x + 1 , y + 1);
	}
	if (a[x + 1][y - 1] != '.')
	{
		dfs(x + 1 , y - 1);
	}
	if (a[x - 1][y - 1] != '.')
	{
		dfs(x - 1 , y - 1);
	}
	if (a[x - 1][y + 1] != '.')
	{
		dfs(x - 1 , y + 1);
	}
	if (a[x][y - 1] != '.')
	{
		dfs(x , y - 1);
	}
	if (a[x][y + 1] != '.')
	{
		dfs(x , y + 1);
	}
	if (a[x + 1][y] != '.')
	{
		dfs(x + 1 , y);
	}
	if (a[x - 1][y] != '.')
	{
		dfs(x - 1 , y);
	}
	return;
}
int main(void)
{
	int n , m;
	scanf("%d%d" , &n , &m);      
	for (int i = 0 ; i < n ; i++)
	{
		scanf("%s" , a[i]); 
	}
	int ans = 0;
	for (int i = 0 ; i < n ; i++)
	{
		for (int j = 0 ; j < m ; j++)
		{
			if (a[i][j] == 'W')
			{
				ans++;
			}
			dfs(i , j);
		}
	}
	printf("%d" , ans);
}
2023/9/25 13:28
加载中...