poj2002 -- 点的hash
2014-03-08 19:48
190 查看
题目:
Squares
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
Sample Output
Source
Rocky Mountain 2004
大意:给出n个点的坐标 求能组成多少个正方形。
思路:
依次枚举两个点,求出另外点的坐标,看看另外能组成正方形的点是否存在。
已知两点A(x1,y1)B(x2,y2)
则 另外两点的坐标:
x3=x1+|y1-y2|;
y3=y1-|x1-x2|;
x4=x2+|y1-y2|;
y2=y2-|x1-x2|;
最难的部分便是对点的储存了 ,
这里先给出 点储存的模板,通过hash数组和next数组的转换 达到储存点的目的:
code:
题目代码:
View Code
Squares
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 15261 | Accepted: 5792 |
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
大意:给出n个点的坐标 求能组成多少个正方形。
思路:
依次枚举两个点,求出另外点的坐标,看看另外能组成正方形的点是否存在。
已知两点A(x1,y1)B(x2,y2)
则 另外两点的坐标:
x3=x1+|y1-y2|;
y3=y1-|x1-x2|;
x4=x2+|y1-y2|;
y2=y2-|x1-x2|;
最难的部分便是对点的储存了 ,
这里先给出 点储存的模板,通过hash数组和next数组的转换 达到储存点的目的:
code:
struct point { int x; int y; }poin[maxn]; int hash[maxn+10]; int next[maxn+10]; int findkey(point p) { return abs(p.x+p.y)%maxn; } void hashinsert(int i) { int key=findkey(poin[i]); next[i]=hash[key];//最后一个next储存的是-1,其他的都指向下一个next hash[key]=i;//指向最开头的next的下标 } int hashsearch(point p) { int key=findkey(p); int i=hash[key]; while(i!=-1) { if(p.x==poin[i].x&&p.y==poin[i].y)//找到一个这样的点 return 1; i=next[i]; } return 0; }
题目代码:
#include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> #include<math.h> using namespace std; const int maxn=1031; struct point { int x; int y; }poin[maxn]; int hash[maxn+10]; int next[maxn+10]; int findkey(point p) { return abs(p.x+p.y)%maxn; } void hashinsert(int i) { int key=findkey(poin[i]); next[i]=hash[key]; hash[key]=i; } int hashsearch(point p) { int key=findkey(p); int i=hash[key]; while(i!=-1) { if(p.x==poin[i].x&&p.y==poin[i].y) return 1; i=next[i]; } return 0; } bool cmp(point p1,point p2) { if(p1.x!=p2.x) return p1.x<p2.x; else return p1.y<p2.y; } int main() { int n; int x1,x2,x3,x4; int y1,y2,y3,y4; //freopen("aa.txt","r",stdin); while(scanf("%d",&n)!=EOF) { if(n==0) break; memset(hash,-1,sizeof(hash)); memset(next,-1,sizeof(next)); point p3,p4; for(int i=0;i<n;i++) scanf("%d%d",&poin[i].x,&poin[i].y); sort(poin,poin+n,cmp); for(int i=0;i<n;i++) hashinsert(i); int ans=0; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { x1=poin[i].x; y1=poin[i].y; x2=poin[j].x; y2=poin[j].y; p3.x=x1+(y2-y1); p3.y=y1-(x2-x1); p4.x=x2+(y2-y1); p4.y=y2-(x2-x1); if(hashsearch(p3)&&hashsearch(p4)) ans++; } } printf("%d\n",ans/2); } return 0; }
View Code
相关文章推荐
- POJ 2002 几何+hash
- hash poj2002
- poj 2002 hash
- POJ 2002 Squares -- Hash
- Hash poj2002 Squares
- poj 2002 Squares,hash
- POJ 2002 点的hash
- POJ-2002-Squares(hash)
- Squares - poj 2002(hash)
- poj 2002 hash
- poj 2002 Squares Hash
- POJ 2002 Squares(hash)
- poj 2002 hash(平面上若干点能够成多少正方形)
- poj2002——Squares(hash)
- POJ 2002 (hash)
- POJ 2002 点hash
- POJ 2002(hash||二分,数学)
- poj 2002 HASH判断正方形
- poj2002 数正方形 hash
- poj 2002 Squares(hash)