10分求助
查看原帖
10分求助
583593
AC_bee楼主2024/3/3 17:11
#include<bits/stdc++.h>
using namespace std;
int n,m;
char o[1001][1001];
bool u[1001][1001];
int len;
void dfs(int x,int y)
{
	//cout<<x<<' '<<y<<' ';
	if(o[x+1][y]=='W' && u[x+1][y]==false)
	{
		u[x+1][y]=true;
		dfs(x+1,y);
	}
	if(o[x-1][y]=='W' && u[x-1][y]==false)
	{
		u[x-1][y]=true;
		dfs(x-1,y);
	}
	if(o[x+1][y+1]=='W' && u[x+1][y+1]==false)
	{
		u[x+1][y+1]=true;
		dfs(x+1,y+1);
	}
	if(o[x+1][y-1]=='W' && u[x+1][y-1]==false)
	{
		u[x+1][y-1]=true;
		dfs(x+1,y-1);
	}
	if(o[x][y-1]=='W' && u[x][y-1]==false)
	{
		u[x][y-1]=true;
		dfs(x,y-1);
	}
	if(o[x][y+1]=='W' && u[x][y+1]==false)
	{
		u[x][y+1]=true;
		dfs(x,y+1);
	}
	if(o[x-1][y-1]=='W' && u[x-1][y-1]==false)
	{
		u[x+1][y-1]=true;
		dfs(x+1,y-1);
	}
	if(o[x-1][y+1]=='W' && u[x-1][y+1]==false)
	{
		u[x+1][y-1]=true;
		dfs(x+1,y-1);
	}
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++){
			cin>>o[i][j];
			if(o[i][j]=='.') u[i][j]=true;
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(o[i][j]=='W' && u[i][j]==false) 
			{
				len++;
				u[i][j]=true;
				dfs(i,j);
				cout<<"\n";
			}
		}
	}
	cout<<len;
	return 0;
}
2024/3/3 17:11
加载中...