问题描述
中兴捧月数房子问题
#include<iostream>
using namespace std;
int a[1000][1000];//房子位置信息
void dfs(int x, int y){
if(a[x][y]==1){
a[x][y]=0;
dfs(x,y+1);
dfs(x,y-1);
dfs(x+1,y);
dfs(x-1,y);
}
}
int main(){
int i,j,m,n,sum=0;
cout<<"Please enter the number of rows and columns of array"<<endl;
cin>>m>>n;
cout<<"Please enter the value of array"<<endl;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
cin>>a[i][j];
for(i=0; i<m; i++)
for(j=0; j<n; j++)
if(a[i][j]==1)
{
sum++;
dfs(i,j);
}
cout<<sum<<endl;
return 0;
}