Squares - poj 2002(hash)

枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点。
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 struct node{
 5     int x,y;
 6 } point[1002];
 7 
 8 struct hash{
 9     int pos;
10     hash* next;
11 } hashtable[2017];
12 int find(int x,int y){
13 
14     hash* t;
15     int tmp=(x*x+y*y)%2017;
16     t=&hashtable[tmp];
17     if(t->pos==0){
18         return 0;
19     }else{
20         int pos=t->pos;
21         if(point[pos].x==x&&point[pos].y==y)
22             return 1;
23     }
24     while(t->next){
25         t=t->next;
26         int pos=t->pos;
27         if(point[pos].x==x&&point[pos].y==y)
28             return 1;
29     }
30     return 0;
31 }
32 int N;
33 int main() {
34     scanf("%d",&N);
35     int i,j;
36     while(N){
37         memset(hashtable,0,sizeof(hashtable));
38         memset(point,0,sizeof(point));
39         for(i=1;i<=N;i++){
40             int x,y;
41             scanf("%d %d",&x,&y);
42             point[i].x=x;
43             point[i].y=y;
44             int t=(x*x+y*y)%2017;
45             if(hashtable[t].pos==0){
46                 hashtable[t].pos=i;
47             }else{
48                 hash* tmp=(hash*)malloc(sizeof(hash));
49                 tmp->next=0;
50                 tmp->pos=i;
51                 tmp->next=hashtable[t].next;
52                 hashtable[t].next=tmp;
53             }
54         }
55         int result=0;
56         for(i=1;i<=N;i++){
57             for(j=i+1;j<=N;j++){
58                 int x1=point[i].x+(point[j].y-point[i].y);
59                 int y1=point[i].y-(point[j].x-point[i].x);
60                 int x2=point[j].x-(point[i].y-point[j].y);
61                 int y2=point[j].y+(point[i].x-point[j].x);
62                 if(find(x1,y1)&&find(x2,y2))
63                     result++;
64             }
65         }
66         for(i=1;i<=N;i++){
67             for(j=i+1;j<=N;j++){
68                 int x1=point[i].x-(point[j].y-point[i].y);
69                 int y1=point[i].y+(point[j].x-point[i].x);
70                 int x2=point[j].x+(point[i].y-point[j].y);
71                 int y2=point[j].y-(point[i].x-point[j].x);
72                 if(find(x1,y1)&&find(x2,y2))
73                     result++;
74             }
75         }
76         printf("%d\n",result/4);
77         scanf("%d",&N);
78     }
79 
80     return 0;
81 }

 

 
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 17553   Accepted: 6677

Description

A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property. 

So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates. 

Input

The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.

Output

For each test case, print on a line the number of squares one can form from the given stars.

Sample Input

4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0

Sample Output

1
6
1

 

posted @ 2015-12-18 20:22  海上的风  阅读(175)  评论(0编辑  收藏  举报