Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 15489   Accepted: 5864

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

二维平面给定一堆点,求能够组成正方形的个数,点数为1000。因此不能枚举四个点推断。

比較优化的方法是将全部点hash。然后枚举两个点,计算出另外两个点的坐标,然后在hash表里查找,最后结果除以4,由于每一条边被统计了4次。

代码:

/* ***********************************************
Author :_rabbit
Created Time :2014/5/11 8:26:00
File Name :20.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100009;
class HASH{
public:
struct Node{
int next,to;
Node(int _next=0,int _to=0){
next=_next;to=_to;
}
}edge[10010];
int tol,head[maxn+10];
void clear(){
memset(head,-1,sizeof(head));tol=0;
}
void add(int x,int y){
if(find(x,y))return;
int t=(x+maxn)%maxn;
edge[tol]=Node(head[t],y);
head[t]=tol++;
}
int find(int x,int y){
int t=(x+maxn)%maxn;
for(int i=head[t];i!=-1;i=edge[i].next)
if(edge[i].to==y)
return 1;
return 0;
}
}mi;
int x[1010],y[1010];
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int n;
while(~scanf("%d",&n)&&n){
mi.clear();
for(int i=0;i<n;i++){
scanf("%d%d",&x[i],&y[i]);
mi.add(x[i],y[i]);
// cout<<"hhh "<<endl;
}
ll ans=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++){
// cout<<"ddd"<<endl;
ll x3,y3,x4,y4;
x3=x[i]+(y[j]-y[i]);y3=y[i]-(x[j]-x[i]);
x4=x[j]+(y[j]-y[i]);y4=y[j]-(x[j]-x[i]);
if(mi.find(x3,y3)&&mi.find(x4,y4))ans++;
x3=x[i]-(y[j]-y[i]);y3=y[i]+(x[j]-x[i]);
x4=x[j]-(y[j]-y[i]);y4=y[j]+(x[j]-x[i]);
if(mi.find(x3,y3)&&mi.find(x4,y4))ans++;
// cout<<"ddd"<<endl;
}
ans/=4;
cout<<ans<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ 2002 点hash的更多相关文章

  1. POJ 2002 Squares [hash]

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

  2. POJ 2002 几何+hash

    题目大意: 给定1000个点,寻找有多少组四点对能组成正方形 这里的题目跟上一道做的找平行四边形类似但想法却又不相同的方法 这里找任意2个点形成的一条边,那么可以根据这两个点,找到能和他们组成正方形剩 ...

  3. POJ 2002 统计正方形 HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...

  4. POJ 2002 Squares 数学 + 必须hash

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

  5. Squares - poj 2002(hash)

    枚举两个点作为一条边,求出正方形的另外两个点,利用hash查找另外两个点. #include<stdio.h> #include<string.h> #include<s ...

  6. poj 2002(好题 链式hash+已知正方形两点求另外两点)

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 18493   Accepted: 7124 Descript ...

  7. POJ 2549 二分+HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定一个n个数字组成的序列,然后求出4个数使得a+b+c=d,求d的最大值.其中a,b,c,d要求是给定序列的数,并且不能 ...

  8. POJ 2002 Squares 哈希

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

  9. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

随机推荐

  1. Qt录音程序

    源地址:http://www.oschina.net/code/snippet_1243295_48623 [代码] [C/C++]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  2. css盒模型和块级、行内元素深入理解

    盒模型是CSS的核心知识点之一,它指定元素如何显示以及如何相互交互.页面上的每个元素都被看成一个矩形框,这个框由元素的内容.内边距.边框和外边距组成,需要了解的朋友可以深入参考下 一.CSS盒模型 盒 ...

  3. Go语言 关于go error处理风格的一些讨论和个人观点(上)

    原创文章.转载请注明出处:server非业余研究-sunface 近期看谷歌go group里面有非常多讨论go error处理风格的问题,颇有启示.如今跟大家分享一下.首先请看一个提问: Hi fo ...

  4. Windows 8 动手实验系列教程 实验7:磁贴和通知

    动手实验 实验7:磁贴和通知 2012年9月 简介 磁贴是Windows应用商店应用用户体验的重要元素.当应用程序被安装后,它的磁贴将在Windows 8开始屏幕被创建.该磁贴(称为主磁贴)作为启动应 ...

  5. JavaScript快速入门(四)——JavaScript函数

    函数声明 之前说的三种函数声明中(参见JavaScript快速入门(二)——JavaScript变量),使用Function构造函数的声明方法比较少见,我们暂时不提.function func() { ...

  6. hdu 4704 同余定理+普通快速幂

    此题往后推几步就可找到规律,从1开始,答案分别是1,2,4,8,16.... 这样就可以知道,题目的目的是求2^n%Mod的结果.....此时想,应该会想到快速幂...然后接着会发现,由于n的值过大, ...

  7. reduce个数究竟和哪些因素有关

    reduce的数目究竟和哪些因素有关 1.我们知道map的数量和文件数.文件大小.块大小.以及split大小有关,而reduce的数量跟哪些因素有关呢?  设置mapred.tasktracker.r ...

  8. Linux Shell常用技巧(二) grep

    七. grep家族:       1.  grep退出状态:    0: 表示成功:    1: 表示在所提供的文件无法找到匹配的pattern:    2: 表示参数中提供的文件不存在.    见如 ...

  9. 9月mob(ShareSDK)活动预告,这个秋天非常热

    9月秋天来临,广州的天气依旧非常热,广州的活动氛围更热~ 先有GMGC B2B对接会在广州创新谷,再有上方网TFC全球移动游戏开发人员大会来袭,游戏圈的火越烧越旺,成都GMGDC全球移动游戏开发人员大 ...

  10. Functional Jobs // Hire Functional Programmers In Less Time

    Functional Jobs // Hire Functional Programmers In Less Time Hire Functional Programmers Quick Save T ...