Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 16631   Accepted: 6328

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

Source

题解:枚举两个点,算出另外两点坐标,看是否在给的点集里。

具体实现用hash,hash碰撞了就放在链表中,然后在链表里查找~(天猫所说的  pascal拉链哈希,,,

就写了个哈希函数然后对函数值指针拉链出来哈希)

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
#define ll long long
#define N 1005
#define mod 2007 using namespace std; int n;
int x[*N];
int y[*N];
//map<pair<int,int>,int>mp;
int ans;
int head[*N];
int next[*N];
int m; void insert(int i)
{
int key=(x[i]*x[i]+y[i]*y[i])%mod;
next[m]=head[key];
x[m]=x[i];
y[m]=y[i];
head[key]=m++;
} void ini()
{
ans=;
m=N;
memset(head,-,sizeof(head));
//mp.clear();
int i;
for(i=;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
insert(i);
//mp[ make_pair(x[i],y[i]) ]=i;
}
} int find(int xx,int yy)
{
int key=(xx*xx+yy*yy)%mod;
int i;
for(i=head[key];i!=-;i=next[i]){
if(x[i]==xx && y[i]==yy){
return ;
}
}
return ;
} int ok(int i,int j)
{
int mx,my;
int x3,x4,y3,y4;
int he,cha;
mx=x[i]+x[j];
my=y[i]+y[j];
he=mx+my;cha=my-mx;
if(he%!= || cha%!=) return ;
he/=;cha/=;
x3=he-y[i];
// x3=mx+my-y[i];
y3=cha+x[i];
// y3=my-(mx-x[i]);
if(find(x3,y3)==) return ;
x4=y[i]-cha;
// x4=mx-(my-y[i]);
// y4=my+(mx-x[i]);
y4=he-x[i];
if(find(x4,y4)==) return ; return ;
} void solve()
{
int i,j;
for(i=;i<n;i++){
for(j=i+;j<=n;j++){
if(ok(i,j)==){
ans++;
}
}
}
} void out()
{
ans/=;
printf("%d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
// scanf("%d",&T);
//while(T--){
while(scanf("%d",&n)!=EOF){
if(n==) break;
ini();
solve();
out();
}
return ;
}

POJ 2002 Squares [hash]的更多相关文章

  1. POJ 2002 Squares【值得摸索的一道二分+点旋转】

    id=2002">Squares 很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点. 题意:给出最多有1000个点,问这些点能够组成多少个正方形 分析:先想想 ...

  2. POJ 2002 Squares 数学 + 必须hash

    http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...

  3. POJ 2002 Squares 哈希

    题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...

  4. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  5. POJ 2002 点hash

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15489   Accepted: 5864 Descript ...

  6. POJ 2002 Squares 几何, 水题 难度: 0

    题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...

  7. poj 2002 Squares 几何二分 || 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 5749 Descript ...

  8. POJ 2002 Squares

    二分.... Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5488 Descr ...

  9. POJ 2002 几何+hash

    题目大意: 给定1000个点,寻找有多少组四点对能组成正方形 这里的题目跟上一道做的找平行四边形类似但想法却又不相同的方法 这里找任意2个点形成的一条边,那么可以根据这两个点,找到能和他们组成正方形剩 ...

随机推荐

  1. elasticsearch最全详细使用教程:搜索详解

    一.搜索API 1. 搜索API 端点地址从索引tweet里面搜索字段user为kimchy的记录 GET /twitter/_search?q=user:kimchy从索引tweet,user里面搜 ...

  2. js 控制台输出

    var a = 'string'; var b = 123; console.log("The %s jumped over %d tall buildings", a, b); ...

  3. gendiff - 致力于创建无错的 diff 文件的工具

    SYNOPSIS gendiff <directory> <diff-extension> DESCRIPTION gendiff 是一个简单的脚本,目标是根据单一的目录生成一 ...

  4. Linux 标准 I/O 库

    为什么要设计标准 I/O 库? 直接使用 API 进行文件访问时,需要考虑许多细节问题,例如:read . write 时,缓冲区的大小该如何确定,才能使效率最优 read 和 write 等底层系统 ...

  5. 在tomcat中配置连接池

    在tomcat的conf/Catalina/localhost目录下配置项目路径,tomcat启动是会直接根据配置去加载项目. 虽然配置就一句话,但经常忘,今天记下来. 如果你的项目成名是:mypro ...

  6. jquery操作滚动条滚动到指定元素位置 scrollTop

    $('.brand_t a').bind('click',function(){ if($(this).attr('title1')){ var toChar = $(this).attr('titl ...

  7. Vue开发微信公众号默认背景为灰色

    最近公司有一个项目,使用Vue开发微信公众号,开发过程遇到一个问题,即设计图的整体背景是白色的,但是公众号里默认的背景是浅灰色,如果某个页面高度没能占满一屏,就会露出浅灰色的默认背景,会显得很不协调. ...

  8. 【树形背包】bzoj4033: [HAOI2015]树上染色

    仔细思考后会发现和51nod1677 treecnt有异曲同工之妙 Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 ...

  9. 对Fiddler设置【Decrypt HTTPS traffic】后火狐浏览器打开https【您的连接并不安全】的解决方法

    火狐浏览器在打开https页面的时候出现[您的连接并不安全]的提示页面: 在设置Fiddler的HTTPS解密的时候,会对下面图中的红线框的选项点击一次生成一个Fiddler 根证书在桌面上: 点击火 ...

  10. IE6,7,8,9还有火狐浏览器的兼容

    /*FF.Opear等支持Web标准的浏览器*/#header {        margin-top: 23px;        margin-bottom: 23px;}/*IE6浏览器*/*ht ...