枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点。
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
int x,y;
} point[]; struct hash{
int pos;
hash* next;
} hashtable[];
int find(int x,int y){ hash* t;
int tmp=(x*x+y*y)%;
t=&hashtable[tmp];
if(t->pos==){
return ;
}else{
int pos=t->pos;
if(point[pos].x==x&&point[pos].y==y)
return ;
}
while(t->next){
t=t->next;
int pos=t->pos;
if(point[pos].x==x&&point[pos].y==y)
return ;
}
return ;
}
int N;
int main() {
scanf("%d",&N);
int i,j;
while(N){
memset(hashtable,,sizeof(hashtable));
memset(point,,sizeof(point));
for(i=;i<=N;i++){
int x,y;
scanf("%d %d",&x,&y);
point[i].x=x;
point[i].y=y;
int t=(x*x+y*y)%;
if(hashtable[t].pos==){
hashtable[t].pos=i;
}else{
hash* tmp=(hash*)malloc(sizeof(hash));
tmp->next=;
tmp->pos=i;
tmp->next=hashtable[t].next;
hashtable[t].next=tmp;
}
}
int result=;
for(i=;i<=N;i++){
for(j=i+;j<=N;j++){
int x1=point[i].x+(point[j].y-point[i].y);
int y1=point[i].y-(point[j].x-point[i].x);
int x2=point[j].x-(point[i].y-point[j].y);
int y2=point[j].y+(point[i].x-point[j].x);
if(find(x1,y1)&&find(x2,y2))
result++;
}
}
for(i=;i<=N;i++){
for(j=i+;j<=N;j++){
int x1=point[i].x-(point[j].y-point[i].y);
int y1=point[i].y+(point[j].x-point[i].x);
int x2=point[j].x+(point[i].y-point[j].y);
int y2=point[j].y-(point[i].x-point[j].x);
if(find(x1,y1)&&find(x2,y2))
result++;
}
}
printf("%d\n",result/);
scanf("%d",&N);
} return ;
}
 
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 17553   Accepted: 6677

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

Squares - poj 2002(hash)的更多相关文章

  1. POJ 2002 Squares 数学 + 必须hash

    http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...

  2. POJ 2002 Squares [hash]

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 16631   Accepted: 6328 Descript ...

  3. POJ 2002 统计正方形 HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...

  4. POJ 2002 Squares 哈希

    题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...

  5. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  6. POJ 2002 点hash

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15489   Accepted: 5864 Descript ...

  7. POJ 2002 Squares 几何, 水题 难度: 0

    题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...

  8. poj 2002 Squares 几何二分 || 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 5749 Descript ...

  9. poj 2002(好题 链式hash+已知正方形两点求另外两点)

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 18493   Accepted: 7124 Descript ...

随机推荐

  1. Node.js mimimn图片批量下载爬虫 1.00

    这个爬虫在Referer设置上和其它爬虫相比有特殊性.代码: //====================================================== // mimimn图片批 ...

  2. FTP服务器配置

    一.FTP服务器: FTP服务使用FTP协议来进行文件的上传和下载,可以非常方便的进行远距离的文件传输,并可以实现相应的安全控制. FTP和NFS.Samba :三大文件服务器 主动模式:消息端口21 ...

  3. iOS12适配指南

    1.重复文件报错 build Phases-> 删除多余的info.plist 2.library not found for -lstdc++ 苹果在XCode10和iOS12中移除了libs ...

  4. NET:Checkboxlist,Dropdownlist 加入ToolTip说明

    ToolTip属性: ToolTip 类 (System.Windows.Controls)‎ 表示创建弹出项的控件.该弹出项可显示界面中元素的相关信息.命名空间: System.Windows.Co ...

  5. 機器學習基石(Machine Learning Foundations) 机器学习基石 作业三 课后习题解答

    今天和大家分享coursera-NTU-機器學習基石(Machine Learning Foundations)-作业三的习题解答.笔者在做这些题目时遇到非常多困难,当我在网上寻找答案时却找不到,而林 ...

  6. hp-ux 集群,内存 小记

    -----查看hp 集群状态信息 # cmviewcl -v CLUSTER        STATUS       dbsvr          up               NODE      ...

  7. LeetCode-1:Two Sum

    [Problem:1-Two Sum] Given an array of integers, return indices of the two numbers such that they add ...

  8. 《转》oracle—flashback

    FLASHBACK介绍 在介绍flashback之前先介绍下undo_retention相关参数 undo_retention:表示undo数据的过期时间.系统默认这个时间设置为900即15分钟.但要 ...

  9. Rabbitmq消息队列(三) 工作队列

    1.简介 默认来说,RabbitMQ会按顺序得把消息发送给每个消费者(consumer).平均每个消费者都会收到同等数量得消息.这种发送消息得方式叫做——轮询(round-robin). 工作队列(又 ...

  10. GSM/GPRS/EDGE简介

    GSM 全球移动通信系统(Global System of Mobile communication),是全球应用最广泛的移动电话标准.GSM被认为是第二代移动通信标准(2G),同时它是一个开放的标准 ...