70pts
查看原帖
70pts
1333328
Co_Ce楼主2025/1/30 21:41
#include<bits/stdc++.h>

using namespace std;

int n,m;

char mp[110][110];

int sum=0;

void dfs(int x,int y){
	mp[ x ][ y ] = '.' ;
	if(x+1<=n&&y+1<=m&&mp[x+1][y+1]=='W') dfs(x+1,y+1);
	if(x+1<=n&&y-1>=0&&mp[x+1][y-1]=='W') dfs(x+1,y-1);
	if(x-1>=0&&y+1<=m&&mp[x-1][y+1]=='W') dfs(x-1,y+1);
	if(x-1>=0&&y-1>=0&&mp[x-1][y-1]=='W') dfs(x-1,y-1);
	
	if(x+1<=n&&mp[x+1][y]=='W') dfs(x+1,y);
	if(x-1>=0&&mp[x+1][y]=='W') dfs(x-1,y);
	if(y+1<=m&&mp[x][y+1]=='W') dfs(x,y+1);
	if(y-1>=0&&mp[x][y-1]=='W') dfs(x,y-1);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
    	for(int j=1;j<=m;j++){
    		cin>>mp[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(mp[i][j]=='W'){
				dfs(i,j);
				sum++;
			}
		}
	}
	cout<<sum;
	return 0;
}
2025/1/30 21:41
加载中...