首页 > 代码库 > HDU 3682 水模拟

HDU 3682 水模拟

n*n*n的图形,m条线,每条线上的方格被删除,问一共删除了多少个方格

ans=m*n ,然后判断一下直线相交的交点,去重即可

#include "stdio.h"
#include "string.h"
int xy[1010][1010],xz[1010][1010],yz[1010][1010],hash[1010];

struct node
{
    int x,y,z;
} mark[1010];
int main()
{
    int Case,n,m,i,j,cnt,a,b,ans,sum;
    char ch1,ch2;
    scanf("%d",&Case);
    while (Case--)
    {
        scanf("%d%d",&n,&m);
        if (m==0)
        {
            printf("0\n");
            continue;
        }
        memset(xy,0,sizeof(xy));
        memset(xz,0,sizeof(xz));
        memset(yz,0,sizeof(yz));

        cnt=0;
        while (m--) // 去重边
        {
            getchar();
            scanf("%c=%d,%c=%d",&ch1,&a,&ch2,&b);
            if (ch1=='X' && ch2=='Y' && xy[a][b]==0)
            {
                mark[cnt].x=a;
                mark[cnt].y=b;
                mark[cnt++].z=-1;
                xy[a][b]=1;
            }
            if (ch1=='Y' && ch2=='X' && xy[b][a]==0)
            {
                mark[cnt].x=b;
                mark[cnt].y=a;
                mark[cnt++].z=-1;
                xy[b][a]=1;
            }
            if (ch1=='X' && ch2=='Z' && xz[a][b]==0)
            {
                mark[cnt].x=a;
                mark[cnt].z=b;
                mark[cnt++].y=-1;
                xz[a][b]=1;
            }
            if (ch1=='Z' && ch2=='X' && xz[b][a]==0)
            {
                mark[cnt].x=b;
                mark[cnt].z=a;
                mark[cnt++].y=-1;
                xz[b][a]=1;
            }
            if (ch1=='Y' && ch2=='Z' && yz[a][b]==0)
            {
                mark[cnt].y=a;
                mark[cnt].z=b;
                mark[cnt++].x=-1;
                yz[a][b]=1;
            }
            if (ch1=='Z' && ch2=='Y' && yz[b][a]==0)
            {
                mark[cnt].y=b;
                mark[cnt].z=a;
                mark[cnt++].x=-1;
                yz[b][a]=1;
            }
        }

        ans=n;
        for (i=1; i<cnt; i++) //去重点
        {
            memset(hash,0,sizeof(hash));
            sum=n;
            for (j=0; j<i; j++) 
            {
                if (mark[i].x==mark[j].x && mark[i].x!=-1)
                {
                    if (mark[i].y==-1 && mark[j].y!=-1)
                    {
                        if (hash[mark[j].y]==0)
                            sum--;
                        hash[mark[j].y]=1;
                    }
                    if (mark[i].z==-1 && mark[j].z!=-1)
                    {
                        if (hash[mark[j].z]==0)
                            sum--;
                        hash[mark[j].z]=1;
                    }
                }

                if (mark[i].y==mark[j].y && mark[i].y!=-1)
                {
                    if (mark[i].x==-1 && mark[j].x!=-1)
                    {
                        if (hash[mark[j].x]==0)
                            sum--;
                        hash[mark[j].x]=1;
                    }
                    if (mark[i].z==-1 && mark[j].z!=-1)
                    {
                        if (hash[mark[j].z]==0)
                            sum--;
                        hash[mark[j].z]=1;
                    }
                }

                if (mark[i].z==mark[j].z && mark[i].z!=-1)
                {
                    if (mark[i].x==-1 && mark[j].x!=-1)
                    {
                        if (hash[mark[j].x]==0)
                            sum--;
                        hash[mark[j].x]=1;
                    }
                    if (mark[i].y==-1 && mark[j].y!=-1)
                    {
                        if (hash[mark[j].y]==0)
                            sum--;
                        hash[mark[j].y]=1;
                    }
                }
            }

            ans+=sum;
        }
        printf("%d\n",ans);
    }
    return 0;
}


HDU 3682 水模拟