首页 > 其他 > 详细

hdu 3682 To Be an Dream Architect hash

时间:2014-04-02 16:52:22      阅读:460      评论:0      收藏:0      [点我收藏+]

题目来源:  http://acm.hdu.edu.cn/showproblem.php?pid=3682

 

To Be an Dream Architect

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


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:
bubuko.com,布布扣
 

 

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
 
 
分析: 将立方体的 每个点 (x,y,z)用 hash 值 对应
val = x * n*n + y *n + z , 然后去掉值相同的,即为消除的块的个数。
其实这种方法不是很好, 时间和内存。
 
代码如下:
bubuko.com,布布扣
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <stack>
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <set>
#include <math.h>
#include <cmath>
#include <map>
#include <stack>
#include <queue>
using namespace std ;
typedef long long LL;
set<int>myset;
set<int>::iterator it;
const int Max_N =1001;
LL num[Max_N * Max_N];
int main()
{
    int t,n,m;
    char a,b;
    int va,vb;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int len=0;
        for(int i=0 ; i<m ;i++)
        {
            getchar();
            scanf("%c=%d,%c=%d",&a,&va,&b,&vb);
            if(a==X)
            {
                if(b==Y)
                    for(int j=1 ;j <=n ;j++)
                        num[len++]= (va*n*n +vb*n + j );
                else
                    for(int j=1 ;j <=n ;j++)
                        num[len++]= (va*n*n +j*n + vb );
            }
            else if(a==Y)
            {
                if(b==X)
                    for(int j=1 ;j <=n ;j++)
                        num[len++]= (vb*n*n +va*n + j );
                else
                    for(int j=1 ;j <=n ;j++)
                       num[len++]= (j*n*n +va*n + vb );
            }
            else
            {
                if(b==X)
                    for(int j=1 ;j <=n ;j++)
                        num[len++]= (vb*n*n +j*n + va );
                else
                    for(int j=1 ;j <=n ;j++)
                        num[len++]= (j*n*n +vb*n + va );
            }
        }
        sort(num,num+len);
        LL sum=1;

        for(int i=1 ; i< len ; i++)
        {
            if(num[i] != num[i-1])
                sum++;
        }
        printf("%I64d\n",sum);

    }
    return 0;
}
bubuko.com,布布扣

这种方法下 , 是不能用 set 和 map 除重, 会超时。

 

 

 

hdu 3682 To Be an Dream Architect hash,布布扣,bubuko.com

hdu 3682 To Be an Dream Architect hash

原文:http://www.cnblogs.com/zn505119020/p/3640827.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!