30分求助
查看原帖
30分求助
505281
D_M_楼主2022/2/18 10:28
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,cnt,cx[4]={0,1,0,-1},cy[4]={1,0,-1,0};
char mapp[20][20];
bool flag=false;
void Dfs(ll x,ll y){
	mapp[x][y]='.';
	ll curX=0,curY=0;
	for(ll i=0;i<=3;i++){
		curX=x+cx[i];
		curY=y+cy[i];
		if(curX>=1&&curX<=n&&curY>=1&&curY<=m&&mapp[curX][curY]=='W'){
			Dfs(curX,curY); 
		}
	}
} 
int main(){
	cin >> n >> m;
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=m;j++){
			cin >> mapp[i][j];
		}
	}
	for(ll i=1;i<=n;i++){
		for(ll j=1;j<=m;j++){
			if(mapp[i][j]=='W'){
				Dfs(i,j);
				cnt++; 
			}
		}
	}
	cout << cnt;
	return 0;
}
2022/2/18 10:28
加载中...