Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 15137   Accepted: 5749

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

 
 /*
题意:给你1000个点的坐标(x,y),让你找出能
构成正方形的个数。
思路:由于是1000,则枚举两个点,求出相应的另外
两个点的坐标。然后用二分判断是否两个点都存在。 就个人而言,关键在 "求出相应的另外两个点的坐标"
设两个点a1,a2;
由a1为中心,逆时针旋转求出
a3.x=a1.y-a2.y+a1.x;
a3.y=a2.x-a1.x+a1.y;
由a2为中心,顺时针旋转求出
a4.x=a1.y-a2.y+a2.x;
a4.y=a2.x-a1.x+a2.y;
由于被计算两次,所以除2
*/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; typedef struct
{
int x,y;
}node;
node a[];
bool cmp(node n1,node n2)
{
if( n1.x!=n2.x )
return n1.x<n2.x;
else return n1.y<n2.y;
}
bool query(int l,int r,node cur)
{
int mid;
while(l<=r)
{
mid=(l+r)/;
if( a[mid].x<cur.x || (a[mid].x==cur.x&&a[mid].y<cur.y))
l=mid+;
else if( a[mid].x>cur.x || ( a[mid].x==cur.x&&a[mid].y>cur.y))
r=mid-;
if( a[mid].x==cur.x && a[mid].y==cur.y) return true;
}
return false;
}
int main()
{
int n,i,j,num;
node a1,a2,a3,a4;
while(scanf("%d",&n)>)
{
if(n==)break;
for(i=;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+,a++n,cmp); for(i=,num=;i<n;i++)
{
a1=a[i];
for(j=i+;j<=n;j++)
{
a2=a[j];
a3.x=a1.y-a2.y+a1.x;
a3.y=a2.x-a1.x+a1.y;
if( !query(,n,a3)) continue;
a4.x=a1.y-a2.y+a2.x;
a4.y=a2.x-a1.x+a2.y;
if( query(,n,a4)) num++; }
}
printf("%d\n",num/);
}
return ;
}

哈希做法:

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; const int INF = ;
typedef struct
{
int x,y;
}node;
struct hash
{
int x;
int y;
struct hash *next;
};
struct hash hash_table[];
node a[INF+]; bool cmp(node n1,node n2)
{
if( n1.x!=n2.x )
return n1.x<n2.x;
else return n1.y<n2.y;
}
void Insert(int x,int y)
{
unsigned k=(x*x+y*y)%INF;
struct hash *new_hash;
new_hash=(struct hash *)malloc(sizeof(struct hash));
new_hash->x=x;
new_hash->y=y;//build new_hash->next=hash_table[k].next;
hash_table[k].next=new_hash;
}
bool found(int x,int y)
{
unsigned k=(x*x+y*y)%INF;
struct hash *new_hash;
new_hash=hash_table[k].next;
while(new_hash!=NULL)
{
if(new_hash->x==x && new_hash->y==y)break;
else new_hash=new_hash->next;
}
if(new_hash==NULL)return false;
else return true;
} int main()
{
int n,i,j,num;
node a1,a2,a3,a4;
while(scanf("%d",&n)>)
{
if(n==)break;
memset(hash_table,,sizeof(hash_table));
for(i=;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
Insert(a[i].x,a[i].y);
}
sort(a+,a++n,cmp); for(i=,num=;i<n;i++)
{
a1=a[i];
for(j=i+;j<=n;j++)
{
a2=a[j];
a3.x=a1.y-a2.y+a1.x;
a3.y=a2.x-a1.x+a1.y;
if(!found(a3.x,a3.y))continue;
a4.x=a1.y-a2.y+a2.x;
a4.y=a2.x-a1.x+a2.y;
if(found(a4.x,a4.y)==true)
num++;
}
}
printf("%d\n",num/);
}
return ;
}

poj 2002 Squares 几何二分 || 哈希的更多相关文章

  1. POJ 2002 Squares 几何, 水题 难度: 0

    题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...

  2. POJ 2002 Squares【值得摸索的一道二分+点旋转】

    id=2002">Squares 很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点. 题意:给出最多有1000个点,问这些点能够组成多少个正方形 分析:先想想 ...

  3. POJ 2774 后缀数组 || 二分+哈希

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 35607   Accepted: 14 ...

  4. POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)

    经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...

  5. POJ 2002 Squares 哈希

    题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...

  6. POJ 2002 Squares 数学 + 必须hash

    http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...

  7. POJ 2002 Squares

    二分.... Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5488 Descr ...

  8. POJ 2002 Squares [hash]

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 16631   Accepted: 6328 Descript ...

  9. 2016vijos 1-1 兔子的字符串(后缀数组 + 二分 + 哈希)

    题意: 给出一个字符串,至多将其划分为n部分,每一部分取出字典序最大的子串ci,最小化 最大的ci 先看一个简化版的问题: 给一个串s,再给一个s的子串t,问能否通过将串划分为k个部分,使t成为划分后 ...

随机推荐

  1. aspx代码审计-1

    今天和大家分享一下aspx网站的代码审计,漏洞类型就是SQL注入和cookie欺骗. 本文作者:i春秋签约作家——非主流 今天看的cms名字叫做:XX星员工请假系统 我们首先看一下网站的目录结构: 其 ...

  2. RN 中 Native 模块的注入过程

    找到所有的模块 一般来说,只要在模块中声明 RCT_EXPORT_MODULE 即可.这是个宏,展开后是声明了一个函数,定义了两个函数,如下所示. #define RCT_EXPORT_MODULE( ...

  3. 初识Flask框架,以及Flask中的模板语言jinjia2和Flask内置的Session

    一.web框架的对比 首先我们先来看下比较火的web框架 1.Django: 优点:大而全,所有组件都是组织内部开发高度定制化,教科书级别的框架 缺点:大到浪费资源,请求的时候需要的资源较高 2.Fl ...

  4. HBase 使用外部的 zookeeper

    HBase 启动时,默认会根据hbase-site.xml文件中的如下设置端口上启动一个zookeeper服务: <property> <name>hbase.zookeepe ...

  5. testng XMl 参数化

    方法一: 方法二: 方法三: (1)如果测试的数据较多的情况下,很显然这种方式不适合,那么可以通过@DataProvider生成测试数据,通过@Test(dataProvider = "&q ...

  6. MySQL error2003错误原因以及解决方案

    转自:http://hi.baidu.com/tianxia339/item/8e8849111461ea7e7a5f2540 出现ERROR 2003 (HY000): Can't connect ...

  7. 警告: Hessian/Burlap: 'com.github.pagehelper.Page' is an unknown class in WebappClassLoader

    项目中使用mybatis的分页插件pagehelper出现下面的警告 出现上面的警告,并不影响程序的运行.但是毕竟看着比较闹心. 使用debug进行代码根据发现,执行的过程中使用到了pagehelpe ...

  8. Windows和Ubuntu双系统时间相差8个小时的问题

    由于要学编程,在windows 10上安装了Ubuntu16.04双系统.但是却造成windows时间老是比实际时间慢八个小时,Ubuntu会与网络同步时间,但是在程序中调用主板时间时仍然是UTC时间 ...

  9. Mac 安装tensorflow

    一. 安装 TensorFlow谷歌的官网和开源项目都有介绍各个系统的安装和使用(官网:https://www.tensorflow.org/install git: https://github.c ...

  10. WPF简单MVVM实现

    1. MVVM介绍: MVVM就是: Model -- 模型(现实中对象的抽象) View -- UI(用户界面) ViewModel -- UI界面的抽象(给View提供数据,并响应View的操作) ...