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值.这个过程对于求三角面片上某点的高程或者权值特别有用,其本身也可以看作一种线性插值. 其算法 ...
随机推荐
- c语言printf()输出格式大全(转载)
1.转换说明符 %a(%A) 浮点数.十六进制数字和p-(P-)记数法(C99) %c 字符 %d 有符号十进制整 ...
- SpringMVC---springMVC配置文件(springweb.xml)简介
再web.xml中设置HTTP请求的中央调度处理器DispatcherServlet时,会指定SpringMVC配置文件,这里取名springweb.xml是因设置DispatcherServlet时 ...
- Robocopy.exe使用详例
Robocopy.exe使用详例 Robocopy.exe 是 微软在Windows server 2003 Resource Kit Tools 里面提供的程序来做备份的.Vis ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目4
2014-03-21 21:28 题目:给定一个20GB大小的文本文件,每一行都是一个字符串.请设计方法将这个文件里的字符串排序. 解法:请看下面的注释. 代码: // 11.4 Given a fi ...
- 【Remove Duplicates from Sorted List 】cpp
题目: 第一次刷的时候漏掉了这道题. Given a sorted linked list, delete all duplicates such that each element appear o ...
- Python编码、流程控制、格式化输出
Python编码 初始编码: 电脑的传输,还有储存,实际上都是010101010 ASCII码: (American Standard Code for Information Interchange ...
- 查看Linux系统用户登录日志
日志: /var/log/wtmp说明: 此文件是二进制文件,查看方法如下:该日志文件永久记录每个用户登录.注销及系统的启动.停机的事件.因此随着系统正常运行时间的增加,该文件的大小也会越来越大,增加 ...
- java程序员笑不死的经历ส้้้้้้้้้
ส้้้้้้้้้้ส้้้้้้้้้้ส้้้้้้้้้ 1.程序猿最烦两件事,第一件事是别人要求他给自己的代码写文档,第二件呢?是别人的程序没有留下文档. 2.宪法顶个球!中国的法律都是.t ...
- ThreadLocal 学习
JDK 1.2版本就已经提供了java.lang.ThreadLocal.其为多线程程序的并发问题提供了一种新的思路.使用该工具类可以简洁地编写出优美的多线程程序. 当使用ThreadLocal维护变 ...
- 2 26requests.py
""" requests """ # import requests # reponse = requests.get("http ...