View Code of Problem 62

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int digui(int i,int j,int sheep[122][122]){
	int t=sheep[i-1][j];
	int d=sheep[i+1][j];
	int l=sheep[i][j-1];
	int r=sheep[i][j+1];
	int lt=sheep[i-1][j-1];
	int rt=sheep[i-1][j+1];
	int ld=sheep[i+1][j-1];
	int rd=sheep[i+1][j+1];
	
	if(t!=1&&l!=1&&d!=1&&r!=1&&lt!=1&&rt!=1&&ld!=1&&rd!=1)
		return 0;
	else{
		if(t==1){
			sheep[i-1][j]=2;// 
			digui(i-1,j,sheep);
			
		}
		if(d==1){
			sheep[i+1][j]=2;// 
			digui(i+1,j,sheep);
			
		} 
		if(l==1){
			sheep[i][j-1]=2;// 
			digui(i,j-1,sheep);
			
		} 
		if(r==1){
			sheep[i][j+1]=2;// 
			digui(i,j+1,sheep);
			
		} 
		if(lt==1){
			digui(i-1,j-1,sheep);
			sheep[i-1][j-1]=2;// 
		} 
		if(rt==1){
			sheep[i-1][j+1]=2;// 
			digui(i-1,j+1,sheep);
			
		} 
		if(ld==1){
			sheep[i+1][j-1]=2;// 
			digui(i+1,j-1,sheep);
			
		} 
		if(rd==1){
			sheep[i+1][j+1]=2;// 
			digui(i+1,j+1,sheep);
			
		}
		return 1;
	}
}
int main(){
	int n,m;
	while(cin>>n>>m){
		if(n==0&&m==0)
			break;
		int sheep[122][122]={0};
		for(int i=1;i<=n;i++){
			string ss;
			cin>>ss;
			for(int j=1;j<=m;j++){
				sheep[i][j]=ss[j-1]-'0';
			}
		}
		
		int count=0; 
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(sheep[i][j]==1){
					digui(i,j,sheep);
					count++;
				}
			}
		}
		cout<<count<<endl;
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 62