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. leetcode_1052. Grumpy Bookstore Owner

    1052. Grumpy Bookstore Owner https://leetcode.com/problems/grumpy-bookstore-owner/ 题意:每个时刻i会有custome ...

  2. WPF中Canvas使用

    首先知道Canvas有Left.Right.Top和Bottom这四个属性,放入Canvas的元素通过这四个属性来决定它们在Canvas里面的位置. 比如: Xaml: <Canvas Hori ...

  3. 机器学习(1)- 概述&线性回归&逻辑回归&正则化

    根据Andrew Ng在斯坦福的<机器学习>视频做笔记,已经通过李航<统计学习方法>获得的知识不赘述,仅列出提纲. 1 初识机器学习 1.1 监督学习(x,y) 分类(输出y是 ...

  4. |chromosomal walk |zoo blot|鉴定疾病gene|

    5.6基于外显子的保守性鉴定真核生物编码蛋白质的基因 鉴定功能性基因的流程是:1.连锁分析找到该基因的染色体的特定区域:2.在这段序列中选择一条短序列,寻找满足两个条件的基因(条件一:因为功能性基因是 ...

  5. 什么是Java内存模型中的happens-before

    Java内存模型JMM Java内存模型(即Java Memory Model , 简称JMM),本身是一种抽象的概念,并不真实存在,它描述的是一组规则或规范,通过这组规范定义了程序个各个变量(包括实 ...

  6. JDBC连接数据库详解

    JDBC连接数据库 •创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机),这通过java.la ...

  7. url地址数据参数转化JSON对象(js三种方法实现)

    当我们用get方法提交表单时,在url上会显示出请求的参数组成的字符串,例如:http://localhost:3000/index.html?phone=12345678901&pwd=12 ...

  8. 【树论 倍增】51nod1709 复杂度分析

    倍增与位运算有很多共性:这题做法有一点像「线段树上二分」和「线段树套二分」的关系. 给出一棵n个点的树(以1号点为根),定义dep[i]为点i到根路径上点的个数.众所周知,树上最近公共祖先问题可以用倍 ...

  9. [LUOGU] P1048 采药

    题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:" ...

  10. Tcp 三次握手 四次分手

    看了 余晟以为的 “tcp没那么难吧”,算是对三次握手,四次分手有了一点点理解,记录下来以方便自己以后的查看. 原文链接:https://mp.weixin.qq.com/s?__biz=MzA3MD ...