#include <iostream>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int mp[110][110], vis[110], m, n, jg, res = 0;
//int fx[4]={0,-1,0,1};
//int fy[4]={-1,0,1,0};
int dfs(int x, int y)
{
if (x < 0 || x >= m || y < 0 || y >= n || mp[x][y] == 0)
{
return 0;
}
mp[x][y] = 0;
dfs(x + 1, y);
dfs(x - 1, y);
dfs(x, y + 1);
dfs(x, y - 1);
}
int main()
{
cin >> m >> n;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> mp[i][j];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (mp[i][j] == 1)
{
dfs(i, j);
res++;
}
}
}
cout << res << endl;
system("pause");
return 0;
}
这是我的全部代码,在运行的时候就会