HDU 3682To Be an Dream Architect(統計規律題目 三線相交bug)

果7發表於2013-11-07

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




相關文章