View Code of Problem 62

#include <stdio.h>
#include <string.h>
int f(int i,int j, int a[300][300],int q)//搜索数字一圈是否有1 有则变数 同时进行扩散 
{
	int x,y;
	if (a[i][j]==1)
	{
	a[i][j]=q;
	for (x=i-1;x<=i+1;x++)
		for (y=j-1;y<=j+1;y++)
		f(x,y,a,q);
	}	
} 
int main()
{
	int m=1,n=1,i,j,k[1000]={0},c=0;
	char p[1000];
	while (1)
	{	
	int a[300][300]={0},q=2;
	scanf("%d",&m); 
	scanf("%d",&n);
	if (m==0&&n==0)
	break;
	getchar();
	for (i=1;i<=m;i++)
	{		
	gets(p);
		for(j=1;j<=n;j++)
		{
			a[i][j]=(int)p[j-1]-48;
		}
	}
	for (i=1;i<=m;i++)
		for (j=1;j<=n;j++)
			if (a[i][j]==1)	
			{									
			f(i,j,a,q);
			q++;
			}
	int max=1;
	for (i=1;i<=m;i++)		
		for(j=1;j<=n;j++)
			if (a[i][j]>max)
			max=a[i][j];
	k[c]=max-1;
	c++;
	}
	int l;
	for (l=0;l<=c-1;l++)
	{
	printf("%d",k[l]);
	if (l!=c-1)
	printf("\n"); 
	}
}

Double click to view unformatted code.


Back to problem 62