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. Ace 在HTML中使用方法

    <!DOCTYPE html> <html> <head> <title>Demo of ACE Editor</title> <!- ...

  2. selenium+chrome浏览器驱动-爬取百度图片

    百度图片网页中中,当页面滚动到底部,页面会加载新的内容. 我们通过selenium和谷歌浏览器驱动,执行js,是浏览器不断加载页面,通过抓取页面的图片路径来下载图片. from selenium im ...

  3. CSS3与弹性盒布局

    1.弹性盒布局对齐模式 1.1.弹性盒子 在规定弹性盒子之中的子级元素换行显示之前父级元素必须是弹性盒子模型,也就是设置 display 为 flex 代码如下: <!DOCTYPE html& ...

  4. jwt 登录

    /* eslint-disable */ 'use strict'; const Controller = require('egg').Controller; const jwt = require ...

  5. bzoj3545 [ONTAK2010]Peaks、bzoj3551 [ONTAK2010]Peaks加强版

    题目描述: bzoj3545,luogu bzoj3551 题解: 重构树+线段树合并. 可以算是板子了吧. 代码(非强制在线): #include<cstdio> #include< ...

  6. [php扩展] php安装扩展注意事项

    添加扩展的时候注意此3项 用的编译器版本:VC11... 安装的php版本:x86/x64 是否线程安全:enabled / disabled

  7. leepcode(斐波那契数列与float("inf")无穷大)解析

    12.加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 ...

  8. linux中test的意义 又可以表示为[]

    测试标志 代表意义 文件名.文件类型 -e 该文件名是否存在 -f 该文件名是否存在且为file -d 该文件名是否存在且为目录 -b 该文件名是否存在且为一个block -c 该文件名是否存在且为一 ...

  9. bootstap 折叠

    data-toggle="collapse" 添加到您想要展开或折叠的组件的链接上. href 或 data-target 属性添加到父组件,它的值是子组件的 id

  10. centos 装 android studio (2)

    这里,我打算安装 JDK 1.8. $ sudo add-apt-repository ppa:webupd8team/java $ sudoapt-get update $ sudoapt-get ...