水洼,八连杀

#include<iostream>
#include<math.h> 
#include<algorithm> 
#include<string>
#include<string.h>
#include<sstream> 
using namespace std;
const int maxn=1e3+10;
 int n,m;
char map[maxn][maxn];

void dfs(int x,int y)
 {map[x][y]='.';
 	for(int i=-1;i<=1;i++)
 	{
 		for(int j=-1;j<=1;j++)
 		{
 			
 			if(x+i>=0&&x+i<n&&y+j>=0&&y+j<m&&map[x+i][y+j]=='W')
			 {
			 	 
			 	dfs(x+i,y+j);
			  } 
		 } 
	 }
 
 }
 
 
int main()
{
 
	 cin>>n>>m;
	 int ans=0;
	 for(int i=0; i<n; i++){
	 	for(int j = 0; j < m; j ++ ){
	 		cin>>map[i][j];
		 }
	 } 
	 for(int i=0; i<n; i++){
	 	for(int j = 0; j < m; j ++ ){
	 		
	 		if(map[i][j]=='W')
				{
					ans++;
					dfs(i,j);
					
				  
				}
			 
				}
		 	
		 		}
		 		cout<<ans;
		 		return 0;
 }
原文地址:https://www.cnblogs.com/shenxiaodou/p/12548389.html