View Code of Problem 62

#include <stdio.h>
#include "string.h"

int m,n;
int map[100][100];
int vis[100][100];
int dir[8][2]={{1,0},{0,1},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
int fit(int x,int y)
{
    if(x>=0&&x<m)
        if(y>=0&&y<n)
            return 1;
    return 0;
}
int fun(int x,int y)
{
    int nowx,nowy,tempx,tempy;
    nowx=x;
    nowy=y;
    vis[nowy][nowx]=1;
    
    for(int i=0;i<8;i++)
    {
        tempx=x+dir[i][0];
        tempy=y+dir[i][1];
        if(fit(tempx,tempy)&&vis[tempy][tempx]==0&&map[tempy][tempx]==1)
            fun(tempx,tempy);
    }
    
    return 1;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF&&(m||n))
    {
        getchar();
        int i,j,sum=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                scanf("%1d",&map[i][j]);
                vis[i][j]=0;
            }
            getchar();
        }
        
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
               if(vis[i][j]==0&&map[i][j]==1)
               {
                  
                   sum=sum+fun(j,i);
               }
        printf("%d\n",sum);
        
    }
    
}

Double click to view unformatted code.


Back to problem 62