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. vue 事件总线(bus)

    1.全局引入bus Vue.prototype.$bus = new.Vue() 2.组件间传值使用(在发送事件时接收组件会实时接收到, 可以用做兄弟组件间相互传值, 但页面跳转组件间有问题 通过$e ...

  2. springmvc中整合mongodb副本集配置文件

    配置文件jdbc.properties: mongo.hostport=192.168.100.100:28007,192.168.100.110:28008,192.168.100.120:2800 ...

  3. Spring Boot 整合MaBatis如何在控制台打印执行的SQL语句

    yml文件:logging: level: com.XXX.Mapper: debug (红色部分为Dao层的包名,注意不是XML文件的包名) properties文件: logging.level. ...

  4. 吴裕雄--天生自然Numpy库学习笔记:NumPy 排序、条件刷选函数

    numpy.sort() 函数返回输入数组的排序副本.函数格式如下: numpy.sort(a, axis, kind, order) 参数说明: a: 要排序的数组 axis: 沿着它排序数组的轴, ...

  5. Docker安装部署ELK教程(Elasticsearch+Kibana+Logstash+Filebeat)

    Elasticsearch 是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logstash 是一个完全开 ...

  6. 【转】使用普通用户执行docker

    原文:https://www.cnblogs.com/klvchen/p/9098745.html CentOS 版本 7.4,Docker 版本 docker-1.13 及以下 ll /var/ru ...

  7. 【原】Mysql最大连接数

    MySQL最大连接数的默认值是100, 这个数值对于并发连接很多的数据库的应用是远不够用的,当连接请求大于默认连接数后,就会出现无法连接数据库的错误,因此我们需要把它适当调大一些. 在使用MySQL数 ...

  8. 第四周之Hadoop学习(四)

    上周已经成功完成了Hadoop的学习,这周则是搭建好Hadoop的安卓编程环境 今天的学习根据这篇博客:https://blog.csdn.net/HcJsJqJSSM/article/details ...

  9. JavaScript高级特征之面向对象笔记

    Javascript面向对象 函数 * Arguments对象: * Arguments对象是数组对象 * Arguments对象的length属性可以获取参数的个数 * 利用Arguments对象模 ...

  10. 利用DOCKER实现云桌面的开发环境初步设想

    想法阶段,持续更新中 一.准备一台开发专用服务器 二.建立企业私有镜像仓库 三.建立开发环境镜像并提交到私有镜像仓库 开发镜像的要求: 1.安装vnc服务,ssh服务 vnc密码的设定 2.安装开发环 ...