Thursday, June 9, 2011

871 - Counting Cells in a Blob


#include<iostream>
#include<cstdio>
#include<cstring>
#define M 26
using namespace std;

char graph[M][M];
int size,hv,m,n;
int next[8][2]={{-1,0},{-1,-1},{0,-1},{1,1},{1,0},{1,-1},{0,1},{-1,1}};
void DFS_Visit(int,int);

int main()
{
    int t,i,j,max;
    freopen("in.txt","r",stdin);
    scanf("%d\n\n",&t);

    while(t--)
    {
        m=0;
        while(1)
        {
            gets(graph[m]);
            if(!strcmp(graph[m++],""))
                break;
        }
        n = strlen(graph[0]);

        max = 0;
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                if(graph[i][j]=='1')
                {
                    size=0;
                    DFS_Visit(i,j);
                    if(max<size)max=size;
                }
        if(t)
            printf("%d\n\n",max);
        else
            printf("%d\n",max);
       
    }
    return 0;
}

void DFS_Visit(int x,int y)
{
    int i,tx,ty;
    graph[x][y]='0';

    size++;
    for(i=0;i<8;i++)
    {
        tx=x+next[i][0],ty=y+next[i][1];
        if(tx>=0&&tx<m&&ty>=0&&ty<n&&graph[tx][ty]=='1')
            DFS_Visit(tx,ty);
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.