POJ 2002 Squares(哈希表)

题目链接

给n个点,找有多少个正方形。忘记初始化了。。。纳闷哈希为毛线一直TLE呢,明显不科学啊。。。哈希跑的还是比较快的,乱搞随便一个哈希函数,跑了500+。

还有 知道对角线两点坐标,求另外两点。。。懒的手推,百度知道上有。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <string>
 7 using namespace std;
 8 #define MOD 177777
 9 #define MAXN 1000000
10 struct node
11 {
12     int data;
13     struct node *next;
14 }*head[MOD],hash[MAXN];
15 int x[1001],y[1001];
16 int judge(int xx,int yy)
17 {
18     node *p;
19     int key = xx*1000 + yy;
20     key = key%MOD;
21     if(key < 0)
22     key += MOD;
23     for(p = head[key];p != NULL;p = p->next)
24     {
25         if(x[p->data] == xx&&y[p->data] == yy)
26         return p->data;
27     }
28     return 0;
29 }
30 int main()
31 {
32     int n,i,j,num,ans,key;
33     int x3,y3,x4,y4,flag1,flag2;
34     while(scanf("%d",&n)!=EOF)
35     {
36         if(n == 0)break;
37         for(i = 0;i < MOD;i ++)
38         head[i] = NULL;
39         num = 0;
40         ans = 0;
41         for(i = 1;i <= n;i ++)
42         {
43             scanf("%d%d",&x[i],&y[i]);
44             key = x[i]*1000 + y[i];
45             node *p;
46             p = &hash[num++];
47             p -> data = i;
48             key = key%MOD;
49             if(key < 0)
50             key += MOD;
51             p -> next = head[key];
52             head[key] = p;
53         }
54         for(i = 1;i <= n-1;i ++)
55         {
56             for(j = i+1;j <= n;j ++)
57             {
58                 x3 = x[i]+x[j]+y[j]-y[i];
59                 if(x3%2 != 0) continue;
60                 x3 = x3/2;
61                 y3 = y[i]+y[j]-x[j]+x[i];
62                 if(y3%2 != 0) continue;
63                 y3 = y3/2;
64                 x4 = x[i]+x[j]+y[i]-y[j];
65                 if(x4%2 != 0) continue;
66                 x4 = x4/2;
67                 y4 = y[i]+y[j]-x[i]+x[j];
68                 if(y4%2 != 0) continue;
69                 y4 = y4/2;
70                 flag1 = judge(x3,y3);
71                 flag2 = judge(x4,y4);
72                 if(flag1&&flag2) ans ++;
73             }
74         }
75         printf("%d\n",ans/2);
76     }
77     return 0;
78 }

 

 

posted @ 2013-01-15 16:46  Naix_x  阅读(239)  评论(0编辑  收藏  举报