hdu 3682 To Be an Dream Architect (hash)

To Be an Dream Architect

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


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


题意:
给你一个三维的立方体,每次去掉一列,去掉m列之后问你总共去掉了多少个1*1*1的小立方体。

思路:
用hash的思想,对立方体进行离散化,将三维看成一个三位的n进制数,用map复杂度为O(m*n*log(n)),但是会有多个case,会TLE。想到先把每个数保存下来,最后去重,复杂度为O(m*n)+O(m*n**log(m*n)),感觉复杂度差不多,但这个可以过...想不通...

感想:
开始还以为是求直线的交点,不过好像异面直线的交点不是太好处理。
恩,其实是可以的,而且这种方法才是正解,因为直线只有三种,所以可以考虑将直线存下来
贴个这种方法的链接:莫莫博客

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 1005
#define mod 1000007
#define INF 0xx3f3f3f3f
typedef long long ll;
using namespace std;

int n,m,ans;
int xx[maxn],yy[5];
vector<int>vec;
char ss[maxn];

void solve()
{
    int i,j,k,tt;
    vec.clear();
    char c1,c2;
    for(i=1;i<=m;i++)
    {
        scanf("%s",ss);
        c1=ss[0];
        tt=0;
        xx['X']=xx['Y']=xx['Z']=0;
        for(j=2;ss[j]!=',';j++)
        {
            tt=tt*10+ss[j]-'0';
        }
        xx[c1]=tt;
        j++;
        c2=ss[j];
        j+=2;
        tt=0;
        for( ;ss[j]!='\0';j++)
        {
            tt=tt*10+ss[j]-'0';
        }
        xx[c2]=tt;
        if(xx[c1]<1||xx[c1]>n||xx[c2]<1||xx[c2]>n) continue ;
        for(j=1;j<=3;j++)
        {
            if(xx[yy[j]]) continue ;
            for(k=1;k<=n;k++)
            {
                xx[yy[j]]=k;
                tt=xx['X']+xx['Y']*n+xx['Z']*n*n;
                vec.push_back(tt);
            }
        }
    }
    sort(vec.begin(),vec.end());
    ans=unique(vec.begin(),vec.end())-vec.begin();
}
int main()
{
    int i,j,t;
    yy[1]='X',yy[2]='Y',yy[3]='Z';
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        solve();
        printf("%d\n",ans);
    }
    return 0;
}







 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值