4619 Warm up 2

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
bool point1[110][110];
bool xf[110][110];
bool yf[110][110];

int tmp,ans;

//int d[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
int dfs(int i,int j)
{
    if(i<0||j<0||i>110||j>110)return 0;
    if(xf[i][j]&&point1[i+1][j])
    {
        point1[i+1][j]=false;
        tmp++;
        dfs(i+1,j);
    }
    if(i>=1&&point1[i-1][j])
    {
        if(xf[i-1][j])
        {
            point1[i-1][j]=false;
            tmp++;
            dfs(i-1,j);
        }
    }
    if(yf[i][j]&&point1[i][j+1])
    {
        point1[i][j+1]=false;
        tmp++;
        dfs(i,j+1);
    }
    if(j>=1&&point1[i][j-1]&&yf[i][j-1])
    {
        point1[i][j-1]=false;
        tmp++;
        dfs(i,j-1);
    }
    return 0;
}

int main()
{
    int n,m;
    int i,j,k;
    int x,y;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)break;
        memset(point1,false,sizeof(point1));
        memset(xf,false,sizeof(xf));
        memset(yf,false,sizeof(yf));
        for(i=0; i<n; i++)
        {
            scanf("%d %d",&x,&y);
            point1[x][y]=point1[x+1][y]=true;
            xf[x][y]=true;
        }
        for(i=0;i<m;i++)
        {
            scanf("%d %d",&x,&y);
            point1[x][y]=point1[x][y+1]=true;
            yf[x][y]=true;
        }
        ans=0;
        for(i=0;i<110;i++)
        {
            for(j=0;j<110;j++)
            {
                tmp=0;
                if(point1[i][j])
                    dfs(i,j);
                //if(tmp!=0)printf("@%d@\n",tmp);
                ans+=(tmp/2);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

XXXXXX欲哭无泪的感觉!!!!!!!!!!!!!!!!!!!!!!!!!!

你可能感兴趣的:(ARM)