POJ 2002 几何+hash

题目大意:

给定1000个点,寻找有多少组四点对能组成正方形

这里的题目跟上一道做的找平行四边形类似但想法却又不相同的方法

这里找任意2个点形成的一条边,那么可以根据这两个点,找到能和他们组成正方形剩下的两个点的位置,根据hash表去搜索,如果这两个位置存在自己需要的点,说明这种方案可行

添加查找均交给hash表,这样可以实现O(n*n)的复杂度

最后因为每个正方形因为有4条边被访问了4次,所以总的答案要除以4

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5
 6 using namespace std;
 7 #define N 1010
 8 #define MOD 1000007
 9 int head[MOD+2] , k;
10
11 struct Point{
12     int x , y;
13     Point(int x=0 , int y=0):x(x),y(y){}
14     bool operator==(const Point &m)const{
15         return x==m.x&&y==m.y;
16     }
17     void input(){scanf("%d%d" , &x , &y);}
18     void print(){cout<<x<<" "<<y<<endl;}
19 }p[N];
20
21 struct HashNode{
22     int id , next;
23 }_hash[N];
24
25 void insert(int i)
26 {
27     int v = (p[i].x*p[i].x+p[i].y*p[i].y)%MOD;
28     _hash[k].id = i , _hash[k].next = head[v];
29     head[v] = k++;
30 }
31
32 bool find(Point a)
33 {
34     int v = (a.x*a.x+a.y*a.y)%MOD;
35     for(int i=head[v] ; ~i ; i=_hash[i].next){
36         if(p[_hash[i].id] == a) return true;
37     }
38     return false;
39 }
40
41 int main()
42 {
43     #ifndef ONLINE_JUDGE
44         freopen("a.in" , "r" , stdin);
45     #endif // ONLINE_JUDGE
46     int n;
47     while(scanf("%d" , &n) , n)
48     {
49         memset(head , -1 , sizeof(head));
50         k = 0;
51         for(int i=1 ; i<=n ; i++){
52             p[i].input();
53             insert(i);
54         }
55         int ret = 0;
56         for(int i=1 ; i<=n ; i++){
57             for(int j=i+1 ; j<=n ; j++){
58                 int dy = p[j].y-p[i].y;
59                 int dx = p[j].x-p[i].x;
60                 Point p1 = Point(p[i].x+dy , p[i].y-dx);
61                 Point p2 = Point(p[j].x+dy , p[j].y-dx);
62                 if(find(p1) && find(p2)) ret++;
63                 p1 = Point(p[i].x-dy , p[i].y+dx);
64                 p2 = Point(p[j].x-dy , p[j].y+dx);
65                 if(find(p1) && find(p2)) ret++;
66             }
67         }
68         printf("%d\n" , ret/4);
69     }
70 }
时间: 2025-01-04 19:12:46

POJ 2002 几何+hash的相关文章

POJ 2002 点的hash

Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15489   Accepted: 5864 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 abou

poj 2002 Squares,hash

poj 2002 Squares 给出n个点,问能组成多少个正方形? 题解: 先把每个点hash 然后枚举两点(即枚举正方形的一条边),然后通过三角形全等,可以推出正方形的另外两点,在hash表里查找这两点看是存在,存在则 Cnt +1. 最后 answer = Cnt/4 //因为同一正方形都统计了4次. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const

POJ 1840 Eqs(hash)

题意  输入a1,a2,a3,a4,a5  求有多少种不同的x1,x2,x3,x4,x5序列使得等式成立   a,x取值在-50到50之间 直接暴力的话肯定会超时的   100的五次方  10e了都    然后可以考虑将等式变一下形   把a1*x1^3+a2*x2^3移到右边   也就是-(a1*x1^3+a2^x2^3)=a3*x3^3+a4*x4^3+a5*x5^3 考虑到a1*x1^3+a2^x2^3的最大值50*50^3+50*50^3=12500000  这个数并不大  可以开这么大

POJ 2002 Squares 数学 + 必须hash

http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那么,整个正方形就确定了,有两个方向. 因为, 设枚举的坐标为(x1, y1) & (x2, y2),所求的坐标是和x1,y1这个点相连,那么有方程如下. 1.垂直,向量积是0 2.边长相等,然后距离公式化简. 即可解出剩下的两个点. 然后要注意两个点要在正方形的同一侧,不然变了平行四边形了. 唤醒了

POJ 2002 统计正方形 HASH

题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边]另外两个点的左边.例如 已知:(x1,y1) (x2,y2)则x3=x1+(y1-y2) y3= y1-(x1-x2) x4=x2+(y1-y2) y4= y2-(x1-x2)或x3=x1-(y1-y2) y3= y1+(x1-x2) x4=x2-(y1-y2) y4= y2+(x1-x2) 枚举两

poj 2002(好题 链式hash+已知正方形两点求另外两点)

Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 18493   Accepted: 7124 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 abou

POJ 2002 Squares 计算集合 点的hash

题目大意:给出平面上的n个点,问能组成多少个正方形. 思路:一开始看时间3秒半,就想用set水过,然而失败了.没办法手写hash吧.观察坐标的范围,<20000,样例中还有负的,我们读进来的时候就将点的坐标+20000,这样避免负数,方便hash.我的哈希很弱,就是把xy坐标加起来作为哈希值,想卡的话应该很轻松.但还是过得很快. CODE: #include <cstdio> #include <cstring> #include <iostream> #incl

poj2002 (极简单数学/几何+hash)

链接:http://poj.org/problem?id=2002 Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 21720   Accepted: 8321 Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also

Poj(1220),hash

题目链接:http://poj.org/problem?id=1200 这个题,我真是无限MLE,RE,WA,太伤心了,还是写一下吧.题意很简单(英语很好读),最后看了一下金海峰的思路.果然,应该是我的这个hash表有点问题,最好是用正确的算法吧,不乱创造了.karp-rabin把字符串转化成数字的算法,一个字符串有n种字符构成,把每种字符对应为0-n-1中的一个数字,把字母换成对应的数字之后,对于固定长度的串,每个串都与一个唯一的n进制数对应.这样就可以hash了. #include <ios