dalao们,p1596,哪里不对
查看原帖
dalao们,p1596,哪里不对
657500
weichen123楼主2022/7/15 16:24
#include<bits/stdc++.h>
using namespace std;
char b[101][101];
int n,m;
int fx[9]={0,0,1,1,1,0,-1,-1,-1};
int fy[9]={0,1,1,0,-1,-1,-1,0,1};
void dfs(int x,int y){
	b[x][y]='.';
	for(int i=1;i<=8;i++){
		if(x+fx[i]>=0&&x+fx[i]<=n&&y+fy[i]>=0&&y+fy[i]<=m&&b[x+fx[i]][y+fy[i]]=='w'){
			dfs(x+fx[i],y+fy[i]);
		}
	}
}
int main() {
	cin>>n>>m;
	int ans=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>b[i][j];	
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(b[i][j]=='w'){
			dfs(i,j);
			ans++;
			
			}
		}
	}
	cout<<ans;
	return 0;
}
2022/7/15 16:24
加载中...