View Code of Problem 62

#include <bits/stdc++.h>
using namespace std;
int x[3]={-1,0,1},y[3]={-1,0,1};
void dfs(int a[121][121],int f[][121],int i,int j){
	if(a[i][j]==1&&f[i][j]==0) f[i][j]=1;
	else return ;
	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++){
			dfs(a,f,i+x[i],j+y[j]);
		}
}
int main(){
	int m,n;
	while(cin>>m>>n&&(m!=0&&n!=0)){
		int a[121][121]={0},count=0;
		int flag[121][121]={0};
		for(int i=1;i<=m;i++){
			string s;
			cin>>s;
			for(int j=1;j<=n;j++)
				a[i][j]=s[j-1]-'0';
		}
		for(int i=1;i<=m;i++)
			for(int j=1;j<=n;j++){
				if(flag[i][j]==0&&a[i][j]==1){
					count++;
					dfs(a,flag,i,j);
				}
			}		
		cout<<count<<endl;		
	}		
} 

Double click to view unformatted code.


Back to problem 62