为什么只有三十分
查看原帖
为什么只有三十分
1039112
qiaoyushu楼主2023/7/15 14:25
#include<bits/stdc++.h>
using namespace std;
const int maxn=105+10;
int n,m,cnt=0;
int s1[maxn][maxn],dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
void dfs(int x,int y)
{
	s1[x][y]=0;
	for(int i=0; i<4; i++)
	{
		x=+dx[i]; y+=dy[i];
		if(s1[x][y]!=0 && x>=1 && x<=n && y>=1 && y<=m) dfs(x,y);
		x-=dx[i]; y-=dy[i];
	} 
}
int main()
{
	//freopen(".in ","r",stdin);
	//freopen(".out ","w",stdout);
	cin>>n>>m;
	for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) cin>>s1[i][j];
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=m; j++)
		{
			if(s1[i][j]==0) continue;
			dfs(i,j);
			cnt++;
		}
	}
	cout<<cnt;
	return 0;
}


2023/7/15 14:25
加载中...