POJ 2002 Squares(计算几何 找正方形 hash枚举)
2013-07-21 14:09
791 查看
Squares
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 Source Rocky Mountain 2004 |
这个题目开始看的时候知道用枚举,也就是根据其中的两个点求出另外两个点,然后判断另外两个点是否在所有的点存在,如果求出来的两个点存在那么就+1
不过还要注意一下就是求出来结果除以4,因为正方形是四条边,每一条边都枚举了,所以结果要除以4
还有这个题目开始用二分的,后来改成hash好像快点!
开始这个题目不会做的原因是知道两个点的坐标求另外两个点的坐标有点一时想不起来,参考之后知道好像是公式也没证明,就当公式用吧
这个题目的hash用的是开放地址,也就是链地址法来搞定冲突问题的,学习了这个题目!
/*
已知: (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)
*/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define prime 97
struct point
{
int x;
int y;
point *next;
}po[1100],x3,x4;
point *hash[1500];
int insert_hash(point &a)
{
point *p;
p=(point*)malloc(sizeof(point));
*p=a;
p->next=hash[((a.x*a.x)+(a.y*a.y))%prime+1];
hash[((a.x*a.x)+(a.y*a.y))%prime+1]=p;
return 0;
}
int find_ans(point &a)
{
int pos=((a.x*a.x)+(a.y*a.y))%prime+1;
point *p=hash[pos];
while(p!=NULL)
{
if(p->x==a.x && p->y ==a.y)
return 1;
p=p->next;
}
return 0;
}
int n;
int main()
{
int i,j,k;
int ans;
while(scanf("%d",&n),n)
{
ans=0;
memset(hash,0,sizeof(hash));
for(i=0;i<n;i++)
{
scanf("%d%d",&po[i].x,&po[i].y);
insert_hash(po[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
x3.x=po[i].x+(po[i].y-po[j].y);
x3.y=po[i].y-(po[i].x-po[j].x);
x4.x=po[j].x+(po[i].y-po[j].y);
x4.y=po[j].y-(po[i].x-po[j].x);
if(find_ans(x3) && find_ans(x4))
ans++;
x3.x=po[i].x-(po[i].y-po[j].y);
x3.y=po[i].y+(po[i].x-po[j].x);
x4.x=po[j].x-(po[i].y-po[j].y);
x4.y=po[j].y+(po[i].x-po[j].x);
if(find_ans(x3) && find_ans(x4))
ans++;
}
printf("%d\n",ans/4);
}
return 0;
}
相关文章推荐
- POJ 2002 Squares 几何 + 二分搜索 或 hash
- POJ 2002 Squares 计算集合 点的hash
- [POJ2002]Squares(计算几何,二分)
- Poj 2002 Squares (正方形个数 Hash)
- POj 2002 UVALive - 3047 C - Squares 【计算几何+二分求解】
- poj 2002 Squares(枚举+点hash)
- POJ 2002 Squares(已知平面n点枚举正方形)
- poj 2002 Squares (hash 拉链 点集)
- poj 2002(好题 链式hash+已知正方形两点求另外两点)
- poj 2002 Squares 几何二分 || 哈希
- 【POJ】2002 - Squares(暴力枚举 & 双关键字二分查找)
- poj 2002 Squares【HASH】POJ最快
- POJ 2002 Square 计算几何
- POJ 2002 Squares -- Hash
- POJ 2002 Squares(hash)
- POJ 2002 - Squares(hash)
- poj 2002(全等三角形计算正方形另外两个点坐标)
- poj 2002 Squares(hash)
- ACM: 几何题+hash题 poj 2002
- POJ 3347 Kadj Squares (计算几何+线段相交)