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. WebService之Axis2(5):会话(Session)管理

    WebService给人最直观的感觉就是由一个个方法组成,并在客户端通过SOAP协议调用这些方法.这些方法可能有返回值,也可能没有返回值.虽然这样可以完成一些工具,但这些被调用的方法是孤立的,当一个方 ...

  2. Angular.js+Bootstrap实现表格分页

    最近一直学习Angular.js,在学习过程中也练习了很多的Demo,这里先贴一下表格+分页. 先上图看看最终结果: 不得不说Angular.js代码风格很受人欢迎,几十行代码清晰简洁的实现了上面的功 ...

  3. SQL 数据库备、还,附、分,数据查询,聚合函数

    认识数据库备份和事务日志备份 数据库备份与日志备份是数据库维护的日常工作,备份的目的是在于当数据库出现故障或者遭到破坏时可以根据备份的数据库及事务日志文件还原到最近的时间点将损失降到最低点. 数据库备 ...

  4. FileInputStream and FileOutputStream

    Java FileOutputStream class Java FileOutputStream is an output stream for writing data to a file. If ...

  5. const对象

    const对象不能引用类的非const成员函数

  6. asp,asp.net 以表格输出excel,数据默认科学计数的解决办法

    关键字:  style="vnd.ms-excel.numberformat:@" 问题:在用table仿excel生成中经常遇到类似于身份证的长整数类型excel默认当成科学计数 ...

  7. CORBA的简单介绍及HelloWorld(zhuan)

    http://blog.csdn.net/drykilllogic/article/details/25971915

  8. Calculation控制台

    接口 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace s ...

  9. [Ubuntu] Install subversion1.8 on Ubuntu13.10

    Subversion1.8 is difference far away from subversion1.7, here is the steps to install subversion1.8. ...

  10. 向已写好的多行插入sql语句中添加字段和值

    #region 添加支款方式--向已写好的多行插入sql语句中添加字段和值 public int A_ZhifuFS(int diqu) { ; string strData = @"SEL ...