Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 17666   Accepted: 6735

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

先枚举两个点,通过数学公式得到另外2个点,使得这四个点可以成正方形。然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形。

但这样的做法会使同一个正方形依照不同的顺序被枚举了四次。因此最后的结果要除以4.

已知: (x1,y1)  (x2,y2)

则:   x3=x1+(y1-y2)   y3= y1-(x1-x2)

x4=x2+(y1-y2)   y4= y2-(x1-x2)

x3=x1-(y1-y2)   y3= y1+(x1-x2)

x4=x2-(y1-y2)   y4= y2+(x1-x2)

能够用向量坐标来证明   对角线上俩坐标已知求还有一条对角线坐标

标记点x y时,key = (x^2+y^2)%prime

解决的地址冲突的方法,我使用了 链地址法

#include<iostream>  //1500K	 1000MS
#include<cstdio>
#include<cstring>
#include<cmath>
#define F 19999 using namespace std; struct zuo
{
int x,y;
} p[20001];
struct node
{
int x,y;
node *next;
}*head[20001];
int n;
int KK(zuo p1)
{
int key=(p1.x*p1.x+p1.y*p1.y)%F;
return key;
}
int Build(int k) //建立
{
int key=KK(p[k]);
if(!head[key])
{
head[key]=new node;
head[key]->next=NULL;
node *q;
q=new node;
q->x=p[k].x;
q->y=p[k].y;
q->next=NULL;
head[key]->next=q;
}
else
{
node *q,*top;
top=head[key];
q=head[key]->next;
while(q)
{
q=q->next;
top=top->next;
}
q=new node;
q->next=NULL;
q->x=p[k].x;
q->y=p[k].y;
top->next=q;
}
return 0;
}
int Count(zuo p1,zuo p2) //统计
{
int key1=KK(p1);
int flag=0;
int key2=KK(p2);
if(head[key1]&&head[key2]) //推断p1,p2是否在哈希表里
{
node *q=head[key1];
while(q)
{
if(q->x==p1.x&&q->y==p1.y)
{
flag=1;
break;
}
q=q->next;
}
if(flag==0)
return 0;
else
{
node *q=head[key2];
while(q)
{
if(q->x==p2.x&&q->y==p2.y)
{
return 1;
}
q=q->next;
}
}
}
return 0;
}
int main()
{
while(~scanf("%d",&n))
{
memset(head,0,sizeof(head));
if(!n)
break;
for(int i=0; i<n; i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
Build(i);
}
int num=0;
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
zuo p1,p2;
p1.x=p[i].x+(p[i].y-p[j].y);
p1.y=p[i].y-(p[i].x-p[j].x);
p2.x=p[j].x+(p[i].y-p[j].y);
p2.y=p[j].y-(p[i].x-p[j].x);
num+=Count(p1,p2); p1.x=p[i].x-(p[i].y-p[j].y);
p1.y=p[i].y+(p[i].x-p[j].x);
p2.x=p[j].x-(p[i].y-p[j].y);
p2.y=p[j].y+(p[i].x-p[j].x);
num+=Count(p1,p2);
}
}
printf("%d\n",num/4);
}
}

poj2002 哈希的更多相关文章

  1. poj2002 数正方形 (哈希+几何)

    题目传送门 题目大意:给你一堆点,问你能组成几个正方形. 思路:一开始想的是用对角线的长度来当哈希的key,但判断正方形会太复杂,然后就去找了一下正方形的判断方法,发现 已知: (x1,y1) (x2 ...

  2. POJ2002 二分查找&哈希

    问题重述: 给定整数n,以及n个点的坐标xi, yi.求这n个点可以组成的正方形的数目(每个点可重复使用). 分析: 根据正方形的性质,给定两个点就能确定可能构成的两个正方形的另外两个顶点.因此,只需 ...

  3. POJ-2002 Squares,哈希模板+数学公式!

                                                           Squares 题意:二维坐标轴给出n个点求有多少个正方形. 要是平时做比赛的话毫无疑问会 ...

  4. [PHP内核探索]PHP中的哈希表

    在PHP内核中,其中一个很重要的数据结构就是HashTable.我们常用的数组,在内核中就是用HashTable来实现.那么,PHP的HashTable是怎么实现的呢?最近在看HashTable的数据 ...

  5. java单向加密算法小结(2)--MD5哈希算法

    上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...

  6. Java 哈希表运用-LeetCode 1 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  7. 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希

    据说今天520是个好日子,为什么我想起的是502.500.404这些?还好服务器没事! 一.Base64编码 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之 ...

  8. Oracle 哈希连接原理

    <基于Oracle的sql优化>里关于哈希连接的原理介绍如下: 哈希连接(HASH JOIN)是一种两个表在做表连接时主要依靠哈希运算来得到连接结果集的表连接方法. 在Oracle 7.3 ...

  9. SQL连接操作符介绍(循环嵌套, 哈希匹配和合并连接)

    今天我将介绍在SQLServer 中的三种连接操作符类型,分别是:循环嵌套.哈希匹配和合并连接.主要对这三种连接的不同.复杂度用范例的形式一一介绍. 本文中使用了示例数据库AdventureWorks ...

随机推荐

  1. 手把手教你如何新建scrapy爬虫框架的第一个项目(上)

    前几天给大家分享了如何在Windows下创建网络爬虫虚拟环境及如何安装Scrapy,还有Scrapy安装过程中常见的问题总结及其对应的解决方法,感兴趣的小伙伴可以戳链接进去查看.关于Scrapy的介绍 ...

  2. Bedrock Linux 0.7.3 发布

    Bedrock Linux是一种元分发,允许用户利用其他通常互斥的Linux发行版的功能,并让它们无缝地一起工作.该项目发布了其0.7.x系列,Bedrock Linux 0.7.3的更新. 新的更新 ...

  3. Centos7&docker-ce&compose&wordpress

    如题,最近帮人装个WordPress,想起来用docker方便,这里做个记录. 原文:https://my.oschina.net/finchxu/blog/2877580 因为docker要求lin ...

  4. 普通码农和CTO之间的差距

    虚心 学习的第一步是--"我不懂".一个空是水杯才能装水,如果是满的就没有办法装水了."自我肯定"是一种非常难克服的习惯,经常会有朋友看到某个技术或者实现之后不 ...

  5. 【转】Hook钩子C#实例

    [转]Hook钩子C#实例 转过来的文章,出处已经不知道了,但只这篇步骤比较清晰,就贴出来了. 一.写在最前 本文的内容只想以最通俗的语言说明钩子的使用方法,具体到钩子的详细介绍可以参照下面的网址: ...

  6. 为什么不能往Android的Application对象里存储数据

    在一个App里面总有一些数据需要在多个地方用到.这些数据可能是一个 session token,一次费时计算的结果等.通常为了避免activity之间传递对象的开销 ,这些数据一般都会保存到持久化存储 ...

  7. Android干货大放送:书籍、教程、工具各种全

    最全干货分享,本文收集整理了Android开发所需的书籍.教程.工具.资讯和周刊各种资源,它们能让你在Android开发之旅的各个阶段都受益. 入门 <Learning Android(中文版) ...

  8. C# Winform 模拟QQ新闻弹出框

    一开始做的时候,觉得这个太简单了.真心做的时候还是遇到了不少的坑啊. 1)循环播放新闻内容,建议使用showdialog(),不要用show(),不太好控制前后之间的停顿. 2)窗口的初始位置为有下角 ...

  9. No mapping found for HTTP request with URI [/test2/test/add.json] in DispatcherServlet with name 'dispatcher'

    查看spring-mvc.xml中扫描包路径配置是否正确: <!-- 扫描controller(controller层注入) --> <context:component-scan ...

  10. Chromium Graphics: Multithreaded Rasterization

    Multithreaded Rasterization @nduca, @enne, @vangelis (and many others) Implementation status: crbug. ...