poj 2002(好题 链式hash+已知正方形两点求另外两点)
| Time Limit: 3500MS | Memory Limit: 65536K | |
| Total Submissions: 18493 | Accepted: 7124 |
Description
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
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
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+已知正方形两点求另外两点)的更多相关文章
- 2020牛客暑期多校训练营 第二场 B Boundary 计算几何 圆 已知三点求圆心
LINK:Boundary 计算几何确实是弱项 因为好多东西都不太会求 没有到很精通的地步. 做法很多,先说官方题解 其实就是枚举一个点 P 然后可以发现 再枚举一个点 然后再判断有多少个点在圆上显然 ...
- POJ 1511 Invitation Cards 链式前向星+spfa+反向建边
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 27200 Accepted: 902 ...
- [YY]已知逆序列求原序列(二分,树状数组)
在看组合数学,看到逆序列这个概念.于是YY了一道题:已知逆序列,求出原序列. 例子: 元素个数 n = 8 逆序列 a={5,3,4,0,2,1,1,0} 则有原序列 p={4,8,6,2,5,1,3 ...
- 已知段地址,求CPU寻址范围
已知段地址为0001H,仅通过变化偏移地址寻址,则CPU的寻址范围是? 物理地址 = 段地址×16 + 偏移地址 所以物理地址的范围是[16×1H+0H, 16×1H+FFFFH] 也就是[10H×1 ...
- 【NX二次开发】三点画圆,三角形外心,已知三点求圆心
已知P1.P2.P3,求点O 算法:三点不在一条直线上时,通过连接任意两点,作中垂线.任意两条中垂线的交点是圆心.
- poj 2242(已知三点求外接圆周长)
The Circumference of the Circle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8310 ...
- poj 1329(已知三点求外接圆方程.)
Circle Through Three Points Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3766 Acce ...
- Codeforce 459A - Pashmak and Garden (已知两点求另外两点构成正方形)
Pashmak has fallen in love with an attractive girl called Parmida since one year ago... Today, Pashm ...
- 已知空间三点组成的面求该面上某点的Z值
已知空间三点,那么可以就可以确定空间三点组成的平面.此时可以根据某一点的X值和Y值,来求取该点在平面上的Z值.这个过程对于求三角面片上某点的高程或者权值特别有用,其本身也可以看作一种线性插值. 其算法 ...
随机推荐
- python的列表生成式和生成器
1.列表生成式是Python受欢迎的语法之一,通过一句简洁的语法就可以对一组元素进行过滤,还可以对得到的元素进行转换处理,语法格式为: [exp for val in collection if co ...
- 网易云深度剖析Kubernetes优化与实践
欢迎访问网易云社区,了解更多网易技术产品运营经验. 10 月 15 日,聚焦 Kubernetes 中国行业应用与技术落地的首届中国 Kubernetes 用户大会(KEUC)在杭州成功举办.本次大会 ...
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目5
2014-03-18 01:40 题目:对字符串进行类似游程编码的压缩,如果压缩完了长度更长,则返回不压缩的结果.比如:aabcccccaaa->a2b1c5a3,abc->abc. 解法 ...
- Unit Test Generator使用
一.环境 1.单元测试(个人理解):是通过编写测试程序检查和验证其他程序的正确性.测试单元粒度越较越易于发现问题. 2.在VS2010在编辑器右键便出现 创建“单元测试”几个字样,可是到了vs2013 ...
- 立体匹配之Census Transform
1.立体匹配算法主要可分为两大类:基于局部约束和基于全局约束的立体匹配算法. (一)基于全局约束的立体匹配算法:在本质上属于优化算法,它是将立体匹配问题转化为寻找全局能量函数的最优化问题,其代表算法主 ...
- 在jsp页面中使用jstl标签
第一步:引入标签库 <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%> 第 ...
- js 回车触发点击事件
$(document).keyup(function(event){ if(event.keyCode ==13){ $("#submit").trigger("clic ...
- StringBuilder_学习笔记
参考:https://www.jianshu.com/p/160c9be0b132 连接符号 "+" 本质 字符串变量(非final修饰)通过 "+" 进行拼接 ...
- HDU 1007 Quoit Design | 平面分治
暂鸽 #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #d ...
- BZOJ4552 [Tjoi2016&Heoi2016]排序 【二分 + 线段树】
题目链接 BZOJ4552 题解 之前去雅礼培训做过一道题,\(O(nlogn)\)维护区间排序并能在线查询 可惜我至今不能get 但这道题有着\(O(nlog^2n)\)的离线算法 我们看到询问只有 ...