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. javascript设计模式(张容铭)学习笔记 - 照猫画虎-模板方法模式

    模板方法模式(Template Method):父类中定义一组操作算法骨架,而降一些实现步骤延迟到子类中,使得子类可以不改变父类的算法结构的同时可重新定义算法中某些实现步骤. 项目经理体验了各个页面的 ...

  2. Python自动化测试框架——数据驱动(从代码中读取)

    今天小编要介绍的是数据驱动最简单和最常用的一种方法,由于只是介绍方法,代码操作后的美观程度略有缺陷,介意者可以自行改动 还是以163邮箱登录为例: 设计一个存放数据的类,这个类的参数是我们需要修改的数 ...

  3. 目录扫描工具DirBuster

    DirBuster是用来探测web服务器上的目录和隐藏文件的.因为DirBuster是采用java编写的,所以运行前要安装上java的环境. 来看一下基本的使用: ①:TargetURL下输入要探测网 ...

  4. datetime模块,random模块

    6.10自我总结 1.datetime模块(用于修改日期) import datetime print(datetime.datetime.now(),type(datetime.datetime.n ...

  5. uboot的readme

    ## (C) Copyright 2000 - 2008# Wolfgang Denk, DENX Software Engineering, wd@denx.de.## See file CREDI ...

  6. Verilog学习笔记基本语法篇(五)········ 条件语句

    条件语句可以分为if_else语句和case语句两张部分. A)if_else语句 三种表达形式 1) if(表达式)          2)if(表达式)               3)if(表达 ...

  7. HDU 3790 (最短路 + 花费)

    题意: 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的. #include<bits/stdc ...

  8. strcpy与strcat函数原型

    1.strcpy函数原型 char *my_strcpy(char *dest,const char *src)    //const使在函数中不能修改*src其原先的值{ char *strDest ...

  9. Java的9种基本数据类型以及封装类

    Java的9种基本数据类型以及封装类 基本类型 大小(单位/字节) 默认值 封装类 byte 1 (byte)0 Byte short 2 (short)0 Short int 4 0 Integer ...

  10. vue 的 scroller 使用

    一 安装 使用npm 安装npm install vue-scroller -d 二 引入 import VueScroller from "vue-scroller" Vue.u ...