Squares

Time Limit: 3500MS Memory Limit: 65536K

Total Submissions: 17462 Accepted: 6634

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

Source

Rocky Mountain 2004

在推正方形顶点时,多亏了金巨巨,金巨巨就是给力

#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout) const int MAX = 1010; struct node
{
int x;
int y ;
}Point[MAX]; bool cmp(node b,node c)
{
if(b.x<c.x||(b.x==c.x&&b.y<c.y))
{
return true;
}
return false;
} bool Look(int low,int high,int x,int y)//二分查找
{
int i=low,j=high;
while(i<=j)
{
int mid=(i+j)/2;
if(Point[mid].x==x&&Point[mid].y==y)
{
return true;
}
if(Point[mid].x<x)
{
i=mid+1;
}
else if(Point[mid].x>x)
{
j=mid-1;
}
else if(Point[mid].x==x)
{
if(Point[mid].y<y)
{
i=mid+1;
}
else if(Point[mid].y>y)
{
j=mid-1;
}
}
}
return false;
}
int main()
{ int n;
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++)
{
scanf("%d %d",&Point[i].x,&Point[i].y);
}
sort(Point,Point+n,cmp);
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int ans=(Point[i].x+Point[j].x-Point[j].y+Point[i].y);//计算正方形的其余的两个顶点
int ant=(Point[i].y+Point[j].y+Point[j].x-Point[i].x);
bool flag=false;
if(ans%2==0&&ant%2==0)
{
flag=Look(0,n-1,ans/2,ant/2);
} if(!flag)
{
continue;
}
flag=false;
ans=(Point[i].x+Point[j].x+Point[j].y-Point[i].y);
ant=(Point[i].y+Point[j].y-Point[j].x+Point[i].x);
if(ans%2==0&&ant%2==0)
{
flag=Look(0,n-1,ans/2,ant/2);
}
if(flag)
{
sum++;
}
}
}
printf("%d\n",sum/2);//对于每个正方形都会查找到两次,所以除二
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Squares 分类: POJ 2015-08-04 11:46 3人阅读 评论(0) 收藏的更多相关文章

  1. PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏

    Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...

  2. 百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET 2015-01-12 11:18 346人阅读 评论(0) 收藏

    在百度编辑器示例代码基础上进行了修改,封装成类库,只需简单配置即可使用. 完整demo下载 版权声明:本文为博主原创文章,未经博主允许不得转载.

  3. Least Common Ancestors 分类: ACM TYPE 2014-10-19 11:24 84人阅读 评论(0) 收藏

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...

  5. 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏

    #include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...

  6. Segment Tree with Lazy 分类: ACM TYPE 2014-08-29 11:28 134人阅读 评论(0) 收藏

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; stru ...

  7. 8大排序算法图文讲解 分类: Brush Mode 2014-08-18 11:49 78人阅读 评论(0) 收藏

    排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...

  8. C语言之void类型及void指针 分类: C/C++ 2015-07-13 11:24 8人阅读 评论(0) 收藏

    原文网址:http://www.cnblogs.com/pengyingh/articles/2407267.html 1.概述 许多初学者对C/C 语言中的void及void指针类型不甚理解,因此在 ...

  9. 指向函数的指针 分类: C/C++ 2015-07-13 11:03 14人阅读 评论(0) 收藏

    原文网址:http://www.cnblogs.com/zxl2431/archive/2011/03/25/1995285.html 讲的很清楚,备份记录. (一) 用函数指针变量调用函数 可以用指 ...

随机推荐

  1. 从零开始攻略PHP(8)——面向对象(下)

    8.编写代码类 每个分离的函数可以执行一个明确的任务.任务越简单,编写与测试这个函数就越简单,当然也不要将这个函数分得太小——若将程序分成太多的小个体,读起来就会很困难. 使用继承可以重载操作.我们可 ...

  2. 转:Selenium之CSS Selector定位详解

    CSS selector定位 CSS(Cascading Style Sheets)是一种语言,它被用来描述 HTML 和 XML 文档的样式.  百度输入框: <input name=&quo ...

  3. 工龄居然这么有用![Reprint]

    工龄有多重要?你恐怕未必知道,别以为没啥大不了,事实上,工龄会从各方面影响你的生活 .为了自己的权益,关于工龄的这些事儿,你必须要了解! 1.影响带薪年休假 (1)职工本单位累计工作满1年不满10年的 ...

  4. SQL封装、多态与重载

    面向对象1.类:众多对象抽象出来的2.对象:类实例化出来的 3.类的定义关键字 class 4.类里面包含成员变量成员属性 成员方法 5.面向对象三大特性(1)封装目的:保护类,让类更加安全.做法:让 ...

  5. Java基础(6):foreach 方法遍历数组

    foreach 并不是 Java 中的关键字,是 for 语句的特殊简化版本,在遍历数组.集合时, foreach 更简单便捷.从英文字面意思理解 foreach 也就是“ for 每一个”的意思,那 ...

  6. cookie和session区别

    cookie和session区 session是在服务器端保存用户信息,cookie是在客户端保存用户信息 session保存的是对象,cookie保存的是字符串 session会随回话结束而关闭,c ...

  7. jQuery中处理事件冒泡的方法和取消后续内容的方法

    一:事件冒泡的意思是:一个大的容器已经设置了事件,如果这个容器里还包容着一个小的容器也设置了自己的事件,那么因为小容器是在大容器里面的,触发小容器的事件同时也等于触发了大容器的事件,有时这并不是我们想 ...

  8. jquery 数组求差集,并集

    var alpha = [1, 2, 3, 4, 5, 6], beta = [4, 5, 6, 7, 8, 9]; $.arrayIntersect = function(a, b){ return ...

  9. :nth-child()和:nth-of-type(n)区别

    ele:nth-child(n) 父元素下第n个元素且这个元素为ele ele:nth-of-type(n) 指父元素下第n个ele元素

  10. Oracle11.2新特性之listagg函数 (行列转换)

    SELECT regexp_substr('公司1,贵公司2', '[^,]+', 1, LEVEL, 'i') FROM dualCONNECT BY LEVEL <= length('公司1 ...