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. pycharm的使用二

    一.pycharm设置参数 设置传入程序的参数:Alt+shift+F10 → Edit Configurations → 选中所需要进行设置参数的脚本 → Script parameters:输入设 ...

  2. ios tcpdump

    转载 前提条件:机器要破解,cydia能打开 需要工具1.openssh2.tcpdump 安装工具方法:1.连接网络,打开cydia2.确认Cydia设置为开发者模式(管理->设置->开 ...

  3. LDAP操作的两种方案

    最近由于项目需要研究了一下LDAP相关知识,感觉对没接触过的人来说还是有点坑的,所以记录下来给大家分享. 由于是第一次接触,就在网上搜了一些相关的文章,照着示例代码测试,却怎么也连不上LDAP服务器, ...

  4. Python全栈 MySQL 数据库(SQL命令大全、MySQL 、Python调用)

    为了梦想与了信仰    开局一张图   主要三个方面: 1.Linux终端命令 2.MySQL语句 3.Python调用   先删库 再跑路.....                         ...

  5. 系统编程--标准IO

    1.流和FILE对象 对于国际字符集,一个字符可以由一个以上的字节来表示.标准I/O文件流可以用来操作单字节和多字节(宽,wide)字符集.一个流的方向(orientation)决定了字符是以单字节还 ...

  6. HDU 3856 Palindrome ( Manacher + RMQ + 二分 ) WA!!!

    不知道错在哪了,求大神指教!!! 思路:用manacher求出每个以str[i]为中心轴的回文串的长度,RMQ预处理区间最大值,对于每个查询,二分最大回文串长,判定是否可行. #include < ...

  7. SPOJ 362 Ignore the Garbage 转7进制+简单大数除法

    显然正着倒着看仍然是数字的只有7个数:0,1,2,5,6,8,9 所以就是用这7个数组合成不同的数. 将n转换成7进制,对应位输出即可. #include <cstdio> #includ ...

  8. Scala 基础(1)—— 定义变量 & 定义函数

    1. 使用 val & var 定义变量 Scala 中的变量被分为2种:val 和 var.其含义于 Java 中的 final 关键字类似. val 等同于被 final 修饰过的变量, ...

  9. 【Luogu】P3239亚瑟王(概率DP)

    题目链接 请看luogu第一篇题解 #include<cstdio> #include<algorithm> #include<cstring> #include& ...

  10. DP石子合并问题

    转自:http://www.hnyzsz.net/Article/ShowArticle.asp?ArticleID=735 [石子合并]    在一个圆形操场的四周摆放着n 堆石子.现要将石子有次序 ...