题目来源: 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:
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.
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 , 然后去掉值相同的,即为消除的块的个数。
其实这种方法不是很好, 时间和内存。
代码如下:
#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 ",sum); } return 0; }
这种方法下 , 是不能用 set 和 map 除重, 会超时。