Thursday, June 9, 2011

11244 - Counting Stars


#include<iostream>
#include<cstdio>
#include<cstring>
#define M 105
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 i,j,count;
    //freopen("in.txt","r",stdin);

    while(scanf("%d%d\n",&m,&n)&&(m|n))
    {
        for(i=0;i<m;i++)
            gets(graph[i]);

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

void DFS_Visit(int x,int y)
{
    int i,tx,ty;
    graph[x][y]='.';
    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]=='*')
            DFS_Visit(tx,ty);
    }
}

No comments:

Post a Comment

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