HDU 3682To Be an Dream Architect(统计规律题目 三线相交bug)

To Be an Dream Architect

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2643    Accepted Submission(s): 753


Problem Description
The “dream architect” is the key role in a team of “dream extractors” who enter other’s dreams to steal secrets. A dream architect is responsible for crafting the virtual world that the team and the target will dream into. To avoid the target noticing the world is artificial, a dream architect must have powerful 3D imagination.

Cobb uses a simple 3D imagination game to test whether a candidate has the potential to be an dream architect. He lets the candidate imagine a cube consisting of n×n×n blocks in a 3D coordinate system as Figure 1. The block at bottom left front corner is marked (1, 1, 1) and the diagonally opposite block is marked (n, n, n). Then he tells the candidate that the blocks on a certain line are eliminated. The line is always parallel to an axis. After m such block eliminations, the candidate is asked to tell how many blocks are eliminated. Note that one block can only be eliminated once even if it is on multiple lines.

Here is a sample graph according to the first test case in the sample input:

 

Input
The first line is the number of test cases.
In each test case, the first line contains two integers n and m( 1 <= n <= 1000, 0 <= m <= 1000).,meaning that the cube is n x n x n and there are m eliminations.

Each of the following m lines represents an elimination in the following format:
axis_1=a, axis_2=b
where axis_i (i=1, 2) is ‘X’ or ‘Y’, or ‘Z’ and axis_1 is not equal to axis_2. a and b are 32-bit signed integers.
 

Output
For each test case output the number of eliminated blocks.
 

Sample Input
  
  
2 3 2 Y=1,Z=3 X=3,Y=1 10 2 X=3,Y=3 Y=3,Z=3
 

Sample Output
  
  
5 19
 

Source
 


题目意思不说了,自己的想法是先假使每一条线都能消除n个点,然后再减去那些算重了的。其实当时写了数据已经验证到了三点相交只能减去两次次,但是自己却按减三次计算。这次组队赛暴露的问题比较多,不过还好不是现场赛,幸好不是现场赛,是个很大的教训!!!教训!


 题目地址:To Be an Dream Architect


AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int p[3][1005][3];
int t[3];
int n,m;

map<int,int> mp1,mp2,mp3;
map<int,int> mm;

//   x y z
// 0 5 6
// 1   6 7
// 2 5   7

int main()
{
    //freopen("in.txt","r",stdin);
    char ch1,ch2;
    int a,b,i,j;
    int tmp1;
    int sum;

    int tes;
    cin>>tes;

    while(tes--)
    {
        cin>>n>>m;
        memset(t,0,sizeof(t));
        mp1.clear();
        mp2.clear();
        mp3.clear();
        mm.clear();
        char s[20];
        for(i=1; i<=m; i++)
        {
            scanf("%s",s);
            sscanf(s,"%c=%d,%c=%d",&ch1,&a,&ch2,&b);
            if(ch1>ch2)
            {
                swap(ch1,ch2);
                swap(a,b);
            }
            
            //cout<<ch1<<" "<<ch2<<endl;
            if(a>=1&&a<=n&&b>=1&&b<=n)
            {
                if(ch1=='X'&&ch2=='Y')
                {
                    tmp1=10000*a+b;
                    if(!mp1[tmp1])
                    {
                        mp1[tmp1]=1;
                        p[0][t[0]][0]=a;
                        p[0][t[0]++][1]=b;
                    }
                }
                else if(ch1=='Y'&&ch2=='Z')
                {
                    tmp1=10000*a+b;
                    if(!mp2[tmp1])
                    {
                        mp2[tmp1]=1;
                        p[1][t[1]][1]=a;
                        p[1][t[1]++][2]=b;
                    }
                }
                else
                {
                    tmp1=10000*a+b;
                    if(!mp3[tmp1])
                    {
                        mp3[tmp1]=1;
                        p[2][t[2]][0]=a;
                        p[2][t[2]++][2]=b;
                    }
                }
            }
        }

        sum=(t[0]+t[1]+t[2])*n;
        //cout<<t[0]<<' '<<t[1]<<" "<<t[2]<<endl;

        for(i=0; i<t[0]; i++)
        {
            for(j=0; j<t[1]; j++)
            {
                int tmp;
                if(p[0][i][1]==p[1][j][1])
                {
                    tmp=p[0][i][0]*1e6+p[0][i][1]*1e3+p[1][j][2];
                    mm[tmp]=1;
                    sum--;
                }
            }
        }

        for(i=0; i<t[0]; i++)
        {
            for(j=0; j<t[2]; j++)
            {
                int tmp;
                if(p[0][i][0]==p[2][j][0])
                {
                    tmp=p[2][j][0]*1e6+p[0][i][1]*1e3+p[2][j][2];
                    if(!mm[tmp])
                    {
                        mm[tmp]=1;
                        sum--;
                    }
                    else if(mm[tmp]==1)
                    {
                        mm[tmp]=2;  //考虑到三点交在一起,只需要考虑减2,而不是减3
                        sum--;
                    }
                }
            }
        }

        for(i=0; i<t[1]; i++)
        {
            for(j=0; j<t[2]; j++)
            {
                int tmp;
                if(p[1][i][2]==p[2][j][2])
                {
                    tmp=p[2][j][0]*1e6+p[1][i][1]*1e3+p[1][j][2];
                    if(!mm[tmp])
                    {
                        mm[tmp]=1;
                        sum--;
                    }
                    else if(mm[tmp]==1)
                    {
                        mm[tmp]=2;
                        sum--;
                    }
                }
            }
        }

        cout<<sum<<endl;
    }
    return 0;
}

/*
5
1 3
Y=1,Z=1
X=1,Z=1
X=1,Y=1
1 0
1 2
X=3,Y=3
Y=3,Z=3
10 3
X=3,Y=3
Y=3,Z=3
X=3,Z=3
10 0
*/

//0MS



其实也可以暴力写的,把那些用到的点都标记一下,不过当时想的是用三位数组存,觉得存不下,这些思路当时都应该拿出来讨论一下的。直接把所有的点都映射到一维上面去处理即可。
代码:
//直接对每个点进行操作
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int max1=1005;
const int max2=max1*max1;
int p[max2];

int main()
{
    //freopen("in.txt","r",stdin);
    int tes;
    cin>>tes;
    int n,m,i,j,tmp,len,a,b;
    char s[20];
    char ch1,ch2;

    while(tes--)
    {
        len=0;
        scanf("%d%d",&n,&m);
        for(j=0; j<m; j++)
        {
            scanf("%s",s);
            sscanf(s,"%c=%d,%c=%d",&ch1,&a,&ch2,&b);
            if(ch1>ch2)
            {
                swap(ch1,ch2);
                swap(a,b);
            }

            if(ch1=='X'&&ch2=='Z')
            {
                for(i=1; i<=n; i++)
                {
                    tmp=a*max2+i*max1+b;
                    p[len++]=tmp;
                }
            }
            else if(ch1=='Y'&&ch2=='Z')
            {
                for(i=1; i<=n; i++)
                {
                    tmp=i*max2+a*max1+b;
                    p[len++]=tmp;
                }
            }
            else
            {
                for(i=1; i<=n; i++)
                {
                    tmp=a*max2+b*max1+i;
                    p[len++]=tmp;
                }
            }
        }

        sort(p,p+len);
        int t=p[0],ans=1;
        for(i=1;i<len;i++)
        {
            if(p[i]!=t)
            {
                t=p[i];
                ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

//234MS




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值