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 思路:一个正方形的四个顶点可由2个来算出,枚举这两个点,然后搜索剩下的点,O(N^2logN),如图:

 此时枚举x1,x2,x3 = x2+-(y1-y2), y3 = y2+-(x1-x2), x4,y4同理,代码如下:

const int maxm = ;

struct Node {
int x, y; bool operator<(const Node &a)const {
return x < a.x || (x == a.x && y < a.y);
}
} buf[maxm]; int n; int main() {
while(scanf("%d",&n) != EOF && n) {
int ans = ;
Node t1, t2;
for (int i = ; i < n; ++i) {
scanf("%d%d", &buf[i].x, &buf[i].y);
}
sort(buf, buf + n);
for (int i = ; i < n - ; i++) {
for (int j = i + ; j < n; ++j) {
t1.x = buf[j].x + (buf[i].y - buf[j].y);
t1.y = buf[j].y - (buf[i].x - buf[j].x);
t2.x = buf[i].x + (buf[i].y - buf[j].y);
t2.y = buf[i].y - (buf[i].x - buf[j].x);
if(binary_search(buf,buf+n,t1) && binary_search(buf,buf+n,t2))
++ans;
t1.x = buf[j].x - (buf[i].y - buf[j].y);
t1.y = buf[j].y + (buf[i].x - buf[j].x);
t2.x = buf[i].x - (buf[i].y - buf[j].y);
t2.y = buf[i].y + (buf[i].x - buf[j].x);
if(binary_search(buf,buf+n,t1) && binary_search(buf,buf+n,t2))
++ans;
}
}
printf("%d\n", ans / );
}
return ;
}

Day3-I-Squares POJ2002的更多相关文章

  1. [poj2002]Squares_hash

    Squares poj-2002 题目大意:在笛卡尔坐标系中给出n个点,求这些点可以构成多少个正方形. 注释:$1\le n\le 10^3$,$-2\cdot 10^3\le x , y\le 2\ ...

  2. poj2002 Squares(hash+折半枚举)

    Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-d ...

  3. poj2002 hash+邻接表优化Squares

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 17487   Accepted: 6643 Descript ...

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

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

  5. [POJ2002]Squares(计算几何,二分)

    题目链接:http://poj.org/problem?id=2002 给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化. 有公 ...

  6. Poj2002 Squares

    题意描述:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少.相同的四个点,不同顺序构成的正方形视为同一正方形. 思路变迁: 1.最简单的方法,直接暴力搜索,即依次取四个顶点,根据其坐标 ...

  7. POJ2002 Squares(枚举)

    题目链接. 分析: 普遍的做法是:先枚举两个点,通过数学公式得到另外2个点,使得这四个点能够成正方形.然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形. 但这种做法会使同一个正方形 ...

  8. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  9. 卡通图像变形算法(Moving Least Squares)附源码

    本文介绍一种利用移动最小二乘法来实现图像变形的方法,该方法由用户指定图像中的控制点,并通过拖拽控制点来驱动图像变形.假设p为原图像中控制点的位置,q为拖拽后控制点的位置,我们利用移动最小二乘法来为原图 ...

  10. Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

随机推荐

  1. Bugku-CTF加密篇之这不是摩斯密码(下载看看吧)

    这不是摩斯密码 下载看看吧  

  2. 后端——框架——持久层框架——Mybatis——补充——pageHelper(分页)插件

    Pagehelper插件的知识点大致可以分为三个部分 搭建环境,引入jar包,配置. 使用方式,只需要记住一种即可.类似于在写SQL语句中,可以left join,也可以right join,它们实现 ...

  3. 输入流输出流IO的理解

    之前对IO理解总是有点模糊, 输入输出,其实针对 数据处理主体 A ,这个主体我们通常是指服务器程序本身,   交互目的源B ,一般是本地磁盘,由本地磁盘IO传输, 或者 远程客户端,由网络IO传输. ...

  4. jemter-plugins-maven dependency -WIiki用法配置介绍

    1.先介绍下jmeter 的maven中央仓库地址,有兴趣自己看下 https://mvnrepository.com/artifact/org.apache.jmeter 2.Wiki github ...

  5. 大数据篇:MapReduce

    MapReduce MapReduce是什么? MapReduce源自于Google发表于2004年12月的MapReduce论文,是面向大数据并行处理的计算模型.框架和平台,而Hadoop MapR ...

  6. C++关键字总结【新手必学】

    const 关键字——常量const 与definedefine是预编译器的编译指令,它从C语言兼容下来,工作方式与文本编辑器中的全局搜索和替换相似.define定义的常量的意义在它开始的地方持续到文 ...

  7. faster-RCNN 加入新的Ground Truth

    出于project的需要,不仅要detect和classify对象,还希望建立不同class的对象之间的关系,进行所谓的pair-matching. 需要完成以下几步: 1. dataset中labe ...

  8. spark实验(一)--spark安装(1)

    一.实验目的 (1)掌握 Linux 虚拟机的安装方法.Spark 和 Hadoop 等大数据软件在 Linux 操作系统 上运行可以发挥最佳性能,因此,本教程中,Spark 都是在 Linux 系统 ...

  9. 读取npz,并显示图像

    import numpy as npimport osimport matplotlib.pyplot as pltimport sysimport cv2 a = np.load('/home/wg ...

  10. 【转载】 NVIDIA Tesla/Quadro和GeForce GPU比较

    原文地址: https://blog.csdn.net/m0_37462765/article/details/74394932 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议 ...