Squares 分类: POJ 2015-08-04 11:46 3人阅读 评论(0) 收藏
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) 收藏的更多相关文章
- 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 ...
- 百度编辑器UEditor ASP.NET示例Demo 分类: ASP.NET 2015-01-12 11:18 346人阅读 评论(0) 收藏
在百度编辑器示例代码基础上进行了修改,封装成类库,只需简单配置即可使用. 完整demo下载 版权声明:本文为博主原创文章,未经博主允许不得转载.
- Least Common Ancestors 分类: ACM TYPE 2014-10-19 11:24 84人阅读 评论(0) 收藏
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏
#include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...
- 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏
#include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...
- 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 ...
- 8大排序算法图文讲解 分类: Brush Mode 2014-08-18 11:49 78人阅读 评论(0) 收藏
排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...
- 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指针类型不甚理解,因此在 ...
- 指向函数的指针 分类: C/C++ 2015-07-13 11:03 14人阅读 评论(0) 收藏
原文网址:http://www.cnblogs.com/zxl2431/archive/2011/03/25/1995285.html 讲的很清楚,备份记录. (一) 用函数指针变量调用函数 可以用指 ...
随机推荐
- MyCat:在.NET Core中使用MyCat
http://www.cnblogs.com/yuangang/p/5830716.html
- UITableView——点击某一行移动到指定位置
选中某一行后想要tableView自动滚动使得选中行始终处于table的top.middle或者bottom,使用以下方法中的一个就可以实现: [tableView scrollToRowAtInde ...
- WCF和Web Service的 区(guan)别(xi)
参考文献:http://social.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice 之前 ...
- logstash的性能测试
logstash有简单的批量生成插件.generator.详情见官网:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-g ...
- 夺命雷公狗ThinkPHP项目之----企业网站29之网站前台左侧导航的高亮显示
其实这里就是加多一段jq代码即可: 首先加一个id : 然后在下面加多一段jq代码即可: <script type="text/javascript"> $(docum ...
- MyEclipse启动失败
日志的一部分: !SESSION 2014-09-24 11:47:03.156 -----------------------------------------------eclipse.buil ...
- 为centos添加额外的源
使用这个命令: yum install epel-release
- 在Debian下安装ibus输入法
sudo apt-get install ibus ibus-pinyin im-switch sudo apt-get install ibus-googlepinyin im-switch -s ...
- 导出excel部分代码
public static function header_file($doc,$file,$title,$type='Excel5'){ if(!empty($doc)){ $objWriter = ...
- Windows应用层网络模块扫盲
说到Windows应用层网络通信不得不提winsock,winsock是工作在TCP/IP层的应用层(TCP/IP层分为主机到网络层[比特].网络互联层[数据帧].传输层[数据包].应用 ...