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. linux开机启动项问题。6版本与7版本不同之处 。

    参考 网址:https://blog.csdn.net/weixin_41909810/article/details/82775247

  2. 洛谷 P3808 【模板】AC自动机(简单版) (AC自动机优化板子)

    题中有一个坑点,就是模式串可以相同,并且全部计数. #include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; ...

  3. Attributes for Slot

    关于AP Config中的一些参数的意义: Radio Type................................... RADIO_TYPE_80211ac-5 Radio Subba ...

  4. 《Interest Rate Risk Modeling》阅读笔记——第八章:基于 LIBOR 模型用互换和利率期权进行对冲

    目录 第八章:基于 LIBOR 模型用互换和利率期权进行对冲 思维导图 推导浮息债在重置日(reset date)的价格 第八章:基于 LIBOR 模型用互换和利率期权进行对冲 思维导图 推导浮息债在 ...

  5. 修改html内联样式的方法

    问题:如下图弹出页面操作不了 分析:审查元素,发现是内联元素样式z-index:19891015导致的,修改内联元素样式z-index:0发现可以操作了 解决方法:内联样式优先级高,再引入css覆盖样 ...

  6. Mysql基本用法-存储引擎-03

    看到存储引擎这个地方感到很多细节比较陌生,所以总结小记一些 为了适应各种不同的运行环境,MYSQL提供了多种不同的存储引擎(Storage Engine ),在应用程序开发这个层面上,开发者可以根据不 ...

  7. Centos7虚拟环境virtualenv与virtualenvwrapper的安装及基本使用

    一.使用虚拟环境的原因 在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题:亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难.此时,我们需 ...

  8. 软件工程 - 防御式编程EAFP vs LBYL

    概念 EAFP:easier to ask forgiveness than permission LBYL:look before you leap 代码 # LBYL def getUserInf ...

  9. 操作系统OS - Zip Bomb

    42.zip - A 42 kb zip file that contains 4.5 Petabytes of uncompressed data.

  10. Elasticsearch 如何使用RESTful API

    所有其他语言可以使用 RESTful API 通过端口 9200 和 Elasticsearch 进行通信,你可以用你最喜爱的 web 客户端访问 Elasticsearch .事实上,正如你所看到的 ...