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

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

题意:平面上有n个点,问n个点能够组成多少个正方形.
初看很简单,就是枚举每两个点去找另外两个点..然后我马上遇到了难题,我准备用bool去存点,结果试了一下 40000*40000直接爆掉了..然后正方形的其余两个点不会求..因为这题都是整形,用斜率求肯定
会出问题(平时做几何体,最好不要用斜率,因为会有正无穷)。然后竟然有公式!!用全等三角形可以证明。。。我等膜拜。。然后就是不能用bool只能用hash了。。hash又是一个好的借鉴。。链式前向星
存的。。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int maxn = ;
const int H = ;
struct Point
{
int x,y;
} p[maxn],C,D;
///************************hash模板 int Hash[H],cur;
void initHash(){
memset(Hash,-,sizeof(Hash));
cur =;
}
struct Node
{
int x,y;
int next;
} node[maxn];
void InsertHash(int x,int y)
{
int h=(x*x + y*y) % H;
node[cur].x=x;
node[cur].y=y;
node[cur].next=Hash[h];
Hash[h]=cur++;
}
bool SearchHash(int x,int y)
{
int h=(x*x + y*y) % H;
int next=Hash[h];
while(next != -)
{
if(node[next].x == x && node[next].y == y) return true;
next = node[next].next;
}
return false;
}
/*******************************/
///已知正方形 a,b 两点求解另外两点的 c,d的坐标(要分两种情况)
void solve1(Point a,Point b,Point& c,Point& d) ///情况1
{
c.x = a.x + (a.y-b.y);
c.y = a.y - (a.x-b.x);
d.x = b.x + (a.y-b.y);
d.y = b.y - (a.x-b.x);
}
void solve2(Point a,Point b,Point& c,Point& d) ///情况2(反过来)
{
c.x = a.x - (a.y-b.y);
c.y = a.y + (a.x-b.x);
d.x = b.x - (a.y-b.y);
d.y = b.y + (a.x-b.x);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n)
{
initHash();
for(int i=; i<n; i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
InsertHash(p[i].x,p[i].y);
}
int ans = ;
for(int i=; i<n; i++)
{
for(int j=i+; j<n; j++)
{
solve1(p[i],p[j],C,D);
if(SearchHash(C.x,C.y)&&SearchHash(D.x,D.y)) ans++;
solve2(p[i],p[j],C,D);
if(SearchHash(C.x,C.y)&&SearchHash(D.x,D.y)) ans++;
}
}
printf("%d\n",ans/);
}
return ;
}

poj 2002(好题 链式hash+已知正方形两点求另外两点)的更多相关文章

  1. 2020牛客暑期多校训练营 第二场 B Boundary 计算几何 圆 已知三点求圆心

    LINK:Boundary 计算几何确实是弱项 因为好多东西都不太会求 没有到很精通的地步. 做法很多,先说官方题解 其实就是枚举一个点 P 然后可以发现 再枚举一个点 然后再判断有多少个点在圆上显然 ...

  2. POJ 1511 Invitation Cards 链式前向星+spfa+反向建边

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 27200   Accepted: 902 ...

  3. [YY]已知逆序列求原序列(二分,树状数组)

    在看组合数学,看到逆序列这个概念.于是YY了一道题:已知逆序列,求出原序列. 例子: 元素个数 n = 8 逆序列 a={5,3,4,0,2,1,1,0} 则有原序列 p={4,8,6,2,5,1,3 ...

  4. 已知段地址,求CPU寻址范围

    已知段地址为0001H,仅通过变化偏移地址寻址,则CPU的寻址范围是? 物理地址 = 段地址×16 + 偏移地址 所以物理地址的范围是[16×1H+0H, 16×1H+FFFFH] 也就是[10H×1 ...

  5. 【NX二次开发】三点画圆,三角形外心,已知三点求圆心

    已知P1.P2.P3,求点O 算法:三点不在一条直线上时,通过连接任意两点,作中垂线.任意两条中垂线的交点是圆心.

  6. poj 2242(已知三点求外接圆周长)

    The Circumference of the Circle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8310   ...

  7. poj 1329(已知三点求外接圆方程.)

    Circle Through Three Points Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3766   Acce ...

  8. Codeforce 459A - Pashmak and Garden (已知两点求另外两点构成正方形)

    Pashmak has fallen in love with an attractive girl called Parmida since one year ago... Today, Pashm ...

  9. 已知空间三点组成的面求该面上某点的Z值

    已知空间三点,那么可以就可以确定空间三点组成的平面.此时可以根据某一点的X值和Y值,来求取该点在平面上的Z值.这个过程对于求三角面片上某点的高程或者权值特别有用,其本身也可以看作一种线性插值. 其算法 ...

随机推荐

  1. SpringMVC---四大注解

    SpringMVC四大注解 Component 通用标注,在不清楚使用哪个注解的时候,可以使用Component通用注解 Controller 标注web请求控制器 Service 标注Service ...

  2. css3心形 perspective transform

    CSS3挺有趣的,能实现不少动画,以下为娱乐内容 <!DOCTYPE html> <html> <head> <meta charset="UTF- ...

  3. https refused 解决方法

    今天调试Android程序,所有的手机都ok,后来,我一个手机一直说,refused. 其实这就说明代码是没有问题的,你应该可以根据这个把代码的原因排除.然后剩下的,网络请求还能有什么,网路白. 果然 ...

  4. Android学习笔记(四)之碎片化Fragment实现仿人人客户端的侧边栏

    其实一种好的UI布局,可以使用户感到更加的亲切与方便.最近非常流行的莫过于侧边栏了,其实我也做过很多侧边栏的应用,但是那些侧边栏的使用我 都不是很满意,现在重新整理,重新写了一个相对来说我比较满意的侧 ...

  5. 《Cracking the Coding Interview》——第16章:线程与锁——题目2

    2014-04-27 19:14 题目:如何测量上下文切换的时间? 解法:首先,上下文切换是什么,一搜就知道.对于这么一个极短的时间,要测量的话,可以通过放大N倍的方法.比如:有A和B两件事,并且经常 ...

  6. 程序员必需知道的Windows Shell命令

    Windows系统本来就很人性化的操作系统,操作很方便,但是对于开发人员来说,有些时候改变一些电脑配置或者执行某些任务来说,使用GUI操作反而事倍功半,因此建议使用Shell命令来提高一下工作效率. ...

  7. 每天一个Linux命令(3):ls命令

    ls命令用来显示目标列表,在Linux中是使用率较高的命令.ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件. 语法 ls(选项)(参数) 选项 -a:显示所有档案及目录(ls内定将档案名 ...

  8. jwt手动生成access_token

    from rest_framework_jwt.settings import api_settings # 手动为用户生成tokenjwt_payload_handler = api_setting ...

  9. python基础——字典dict

    1.概念: (1)字典dict,是一系列的键—值对.每个键key都和一个值value相映射.(字典是python中唯一的映射类型.) (2)每一项item,是一个键值对key—value对. (3)键 ...

  10. hadoop2.6.4【windows7】构建maven项目 系列2

    准备windows版本的hadoop2.6.4 下载windows版本的hadoop2.6.4解压在本地 新建maven项目构建hadoop依赖 <?xml version="1.0& ...