procedure2012
It's not worth it to know you're not worth it!

[关键字]:数据结构

[题目大意]:给出一个n*n*n的立方体,然后回有m个操作,要求删去某一列,问最后删去了多少个小立方体。

//====================================================================================================================================

[分析]:对于每个操作,枚举另一个坐标,然后以x*100*1000+y*1000+z*1给坐标为(x,y,z)的立方体编号,建立Hash表查找是否已经被删过。

[代码]:

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int c[4]={0,1000*1000,1000,1};

int h[400000][15];
int N,M,test;

bool Hash(int key)
{
int k=key%399997;
for (int i=1;i<=h[k][0];i++)
if (h[k][i]==key) return 0;
h[k][++h[k][0]]=key;
return 1;
}

int Calc(char ch1,int d1,char ch2,int d2)
{
int sum=0;
for (int i=1;i<=N;i++)
{
int key=c[6-ch1-ch2]*i+c[ch1]*d1+c[ch2]*d2;
if (Hash(key)) sum++;
}
return sum;
}

int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
scanf("%d",&test);
while (test--)
{
scanf("%d%d\n",&N,&M);
int ans=0,d1,d2;
char ch1,ch2;
memset(h,0,sizeof(h));
for (int i=1;i<=M;i++)
{
scanf("%c=%d,%c=%d\n",&ch1,&d1,&ch2,&d2);
ch1=ch1-'W',ch2=ch2-'W';
//printf("%c %d %c %d\n",ch1,d1,ch2,d2);
int temp=Calc(ch1,d1,ch2,d2);
ans+=temp;
}
printf("%d\n",ans);
}
return 0;
}



posted on 2012-02-28 18:40  procedure2012  阅读(207)  评论(0编辑  收藏  举报