题目传送门

题目大意:给你一堆点,问你能组成几个正方形。

思路:一开始想的是用对角线的长度来当哈希的key,但判断正方形会太复杂,然后就去找了一下正方形的判断方法,发现

已知: (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)

就是枚举两个点,然后算出另外两个点。在哈希表中看看能不能找到这两个点。如果只采取其中一个公式的话,切记点的坐标要排序再哈希(具体原因我也不知道,但自己举了很多样例确实是这样),你也可以把两个公式都用上,两个if得到答案。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const double PI=acos(-1.0);
int fact[10]= {1,1,2,6,24,120,720,5040,40320,362880};
const int maxn= 1010;
const int mod = 100019;
int n;
struct dian {
int x,y;
} a[maxn];
int hash[mod+10];
int next[maxn];
int gethash(int i) {
return (a[i].x*a[i].x%mod+a[i].y*a[i].y%mod)%mod;
}
bool find(int x,int y){
int hashval=(x*x%mod+y*y%mod)%mod;
for(int i=hash[hashval];i!=-1;i=next[i])
{
if(a[i].x==x&&a[i].y==y)return true;
}
return false;
}
bool cmp(dian aa,dian bb){
if(aa.x!=bb.x)
return aa.x<bb.x;
return aa.y<bb.y;
}
int main(){
while(scanf("%d",&n),n)
{
int ans=0;
memset(hash,-1,sizeof(hash));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+1,a+1+n,cmp);//非常重要 没有则wa
for(int i=1;i<=n;i++)
{
int hashval=gethash(i);//映射到哈希表里
next[i]=hash[hashval];
hash[hashval]=i;
}
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
int x=a[i].x-a[j].y+a[i].y;//算出第一个点
int y=a[i].y+a[j].x-a[i].x;
if(!find(x,y))continue;//查找
x=a[j].x-a[j].y+a[i].y;//第二个点
y=a[j].y+a[j].x-a[i].x;
if(!find(x,y))continue;//查找
ans++;
}
}
printf("%d\n",ans/2);//由于有重复计算 所以要除以二
}
}
Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 21208   Accepted: 8136

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

poj2002 数正方形 (哈希+几何)的更多相关文章

  1. CODEVS1533 互斥的数(哈希表)

    给定一个集合,要求一个最大子集,满足两两之间不互斥.对两个数x,y互斥的定义是,y=p*x. 先对集合中的数从小到大排序后线性扫,若一个数x可以取则取,取完之后p*x这个数不可取.由于数字较大,使用哈 ...

  2. HDU 1280 前m大的数【哈希入门】

    题意:中文的题目= =将各种组合可能得到的和作为下标,然后因为不同组合得到的和可能是一样的, 所以再用一个数组num[]数组,就可以将相同的和都记录下来 #include<iostream> ...

  3. POJ2002 二分查找&哈希

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

  4. 前m大的数(哈希入门)&&sort

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 普通方法(625ms) #include <stdio.h> #include <str ...

  5. UVA - 1643 Angle and Squares (角度和正方形)(几何)

    题意:第一象限里有一个角,把n(n <= 10)个给定边长的正方形摆在这个角里(角度任意),使得阴影部分面积尽量大. 分析:当n个正方形的对角线在一条直线上时,阴影部分面积最大. 1.通过给定的 ...

  6. [CSP-S模拟测试]:飞(fly)(数状数组+简单几何)

    题目描述 $liu\_runda$决定提高一下知识水平,于是他去请教郭神.郭神随手就给了$liu\_runda$一道神题,$liu\_runda$并不会做,于是把这个题扔到联考里给高二的做.郭神有$n ...

  7. 在Hekaton里,正确选择哈希存储桶数

    今天我使用2048的桶数的哈希索引,往Hakaton里插入100万的记录,测试下在哈希桶数里,哈希冲突(Hash Collision)是如何影响Hekaton的工作量——结果非常非常有意思.首先我想介 ...

  8. Java数据结构和算法(十三)——哈希表

    Hash表也称散列表,也有直接译作哈希表,Hash表是一种根据关键字值(key - value)而直接进行访问的数据结构.它基于数组,通过把关键字映射到数组的某个下标来加快查找速度,但是又和数组.链表 ...

  9. Java数据结构和算法 - 哈希表

    Q: 如何快速地存取员工的信息? A: 假设现在要写一个程序,存取一个公司的员工记录,这个小公司大约有1000个员工,每个员工记录需要1024个字节的存储空间,因此整个数据库的大小约为1MB.一般的计 ...

随机推荐

  1. 【MySQL】20个经典面试题(转)

    原文链接:http://bbs.51cto.com/thread-1470880-1.html Part2:经典题目 1.MySQL的复制原理以及流程 基本原理流程,3个线程以及之间的关联: 2.my ...

  2. Eclipse Maven 编译错误 Dynamic Web Module 3.0 requires Java 1.6 or newer 解决方案

    Eclipse Maven 开发一个 jee 项目时,编译时遇到以下错误:Description Resource Path Location TypeDynamic Web Module 3.0 r ...

  3. 使用LaTeX按IEEE模板写论文时的参考文献管理方法(BibTeX使用小结)

    之前用LaTeX写论文时,参考文献都是手动添加管理的,真是让人很抓狂.所以这次趁着假期,简单看了一下怎么使用BibTeX对参考文献进行管理,这里以IEEE的最新模板为例. 首先说明,我之前用的是MiK ...

  4. No bean class specified on bean definition 解决方案

    调试Spring项目出现No bean class specified on bean definition异常 但是控制台并没有给出其他相关信息了 这个时候可以在AbstractBeanDefini ...

  5. __call()和__callStatic()方法

    __call() 当对象访问不存在的方法时,__call()方法会被自动调用__callStatic() 当对象访问不存在的静态方法时,__callStatic()方法会被自动调用 这两个方法在PHP ...

  6. 算法Sedgewick第四版-第1章基础-014一用stack把前置表达式转为后置表达式并计算值

    1. /************************************************************************* * Exercise 1.3.10 * * ...

  7. GNU Gettext

    一.简介 当前,无论是商业还是免费软件都是英文的,并用英文做为文档.直到现在,为使其它非英语语言用户也能够进行交互所做的工作仍然不足,所以这对非英语语言的国家很不利.然而,随着GNU gettext工 ...

  8. conda 添加bioconda源,创建/删除/重命名环境

    1.conda安装 在https://repo.continuum.io/miniconda/选择conda版本 wget "https://repo.continuum.io/archiv ...

  9. Python--面向对象编程--时钟实例开发

    在学习python面向对象编程的时候,心血来潮,决定写一个时钟模型来玩玩,所以就有了现在这个小玩意,不过python这个东西确实是挺好玩的 方法:运用python的tkinter库开发图形化时钟程序 ...

  10. fiddler----APP弱网测试

    转自:http://www.51testing.com/html/01/n-3727001.html APP弱网模拟测试 移动端测试区别于PC端测试的一点就是网络的多变性:不同的网络环境和网络制式的差 ...