POJ-2002 Squares---绕点旋转+Hash
题目链接:
https://vjudge.net/problem/POJ-2002
题目大意:
有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少。
相同的四个点,不同顺序构成的正方形视为同一正方形。
解题思路:
直接四个点四个点地枚举肯定超时的,不可取。
普遍的做法是:先枚举两个点(这两个点是正方形的一条边),通过数学公式得到另外2个点,使得这四个点能够成正方形。然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形。
但这种做法会使同一个正方形按照不同的顺序被枚举了四次,因此最后的结果要除以4.
已知点(x1, y1),(x2, y2),可求出下面两种可能


求出另外两个点之后直接在不在hash表中(之前用二分一直超时)
关于点(x1, y1)绕点(x0, y0)逆时针旋转β度得到(x2, y2)的公式:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#define lowbot(i) (i&(-i))
//#define Rotate(a, b) node(a.x + a.y - b.y, a.y + b.x - a.x)
using namespace std;
typedef long long ll;
const int maxn = + ;
const int mod = ;
struct node
{
int x, y;
node(){}
node(int x, int y):x(x), y(y){}
};
struct hashtable
{
int x, y;
hashtable * next;
hashtable()
{
next = ;
}
};
hashtable * Hash[mod];
void Hash_Insert(node a)
{
int key = (a.x * a.x + a.y * a.y) % mod;
if(!Hash[key])
{
hashtable * p = new hashtable;
p->x = a.x;
p->y = a.y;
Hash[key] = p;
}
else
{
hashtable *p = Hash[key];
while(p->next)p=p->next;
hashtable* temp = new hashtable;
temp->x = a.x;
temp->y = a.y;
p->next = temp;
}
}
bool Find(node a)
{
int key = (a.x * a.x + a.y * a.y) % mod;
if(!Hash[key])return false;
else
{
hashtable * temp = Hash[key];
while(temp)
{
if(temp->x == a.x && temp->y == a.y)
return true;
temp = temp->next;
}
}
return false;
}
node a[maxn]; node Rotate(node a, node b)//点b绕着点a逆时针旋转90度的坐标
{
int x = (b.x - a.x) * - (b.y - a.y) * + a.x;
int y = (b.x - a.x) * + (b.y - a.y) * + a.y;
return node(x, y);
} int main()
{
int n;
while(scanf("%d", &n) != EOF && n)
{
memset(Hash, , sizeof(Hash));
for(int i = ; i <= n; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
Hash_Insert(a[i]);
}
int ans = ;
node x, y;
for(int i = ; i <= n; i++)
{
for(int j = i + ; j <= n; j++)
{
x = Rotate(a[i], a[j]);
y = Rotate(x, a[i]);
if(Find(x) && Find(y))ans++;
//cout<<i<<" "<<j<<" "<<x.x<<" "<<x.y<<" "<<y.x<<" "<<y.y<<endl;
x = Rotate(a[j], a[i]);
y = Rotate(x, a[j]);
if(Find(x) && Find(y))ans++;
}
}
printf("%d\n", ans / );
}
return ;
}
POJ-2002 Squares---绕点旋转+Hash的更多相关文章
- POJ 2002 Squares【值得摸索的一道二分+点旋转】
id=2002">Squares 很好的一道二分,事实上本来我是没有思路的,看了基神的题解之后才似乎明确了点. 题意:给出最多有1000个点,问这些点能够组成多少个正方形 分析:先想想 ...
- POJ 2002 Squares 数学 + 必须hash
http://poj.org/problem?id=2002 只能说hash比二分快很多.随便一个hash函数都可以完爆二分. 判断是否存在正方形思路如下: 1.枚举任意两个点,作为正方形的一条边,那 ...
- POJ 2002 Squares [hash]
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 16631 Accepted: 6328 Descript ...
- POJ 2002 Squares 哈希
题目链接: http://poj.org/problem?id=2002 #include <stdio.h> #include <string.h> ; struct Has ...
- poj 2002 Squares 几何二分 || 哈希
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 5749 Descript ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- POJ 2002 Squares 几何, 水题 难度: 0
题目 http://poj.org/problem?id=2002 题意 已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形. 思路 如图,将坐标按照升序排列后 ...
- poj 2002(好题 链式hash+已知正方形两点求另外两点)
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 18493 Accepted: 7124 Descript ...
- POJ 2002 Squares
二分.... Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 14530 Accepted: 5488 Descr ...
- No.5 - 纯 CSS 制作绕中轴旋转的立方体
body{ background-color: #000; margin:; padding:; } main{ perspective: 800px; } .cube{ transform-styl ...
随机推荐
- Go语言基础之10--面向对象编程2之方法
一.方法的定义 之前我们学习了结构体(struct),其仅仅是对数据的封装,并没有行为方法,还不是一个完全的面向对象的思路,所以现在我们来学习在结构体的基础上如何去定义一个方法.结构体(类)+方法=完 ...
- 缓存算法及Redis、Memcached、Guava、Ehcache中的算法
https://my.oschina.net/ffy/blog/501003 https://yq.aliyun.com/articles/622757 https://blog.csdn.net/s ...
- Linux内核模块简单示例
1. Linux 内核的整体结构非常庞大,其包含的组件也非常多,使用这些组件的方法有两种: ① 直接编译进内核文件,即zImage或者bzImage(问题:占用内存过多) ② 动态添加 * 模块本身并 ...
- 一个简单的基于MINI2440开发板的启动代码
1. S3C2440大概的启动流程(NAND启动): ①设置CPU为SVC模式 ②关闭看门狗 ③屏蔽中断 ④关闭MMU ⑤初始化时钟 ⑥初始化内存(SDRAM) ⑦初始化栈指针(SP, R13) ⑧初 ...
- 02Data
1.数据从何而来 2.数据对象和属性类型 数据集合的类型 结构数据的重要特征 数据对象 属性 属性类型 数据属性的类型 离散 vs.连续属性 3.数据的(基本)统计描述 分布度量 代数度量 整体度量 ...
- 傻瓜式Spring教学第一课
首先,把Spring需要的五个包导入项目: commons-logging-1.2.jar spring-beans-4.3.4.RELEASE.jar spring-context-4.3.4.RE ...
- SQL server数据库端口访问法
最近数据库连接,也是无意中发现了这个问题,数据库可根据端口来连接 我用的是sql2014测试的,在安装其他程序是默认安装了sql(sql的tcp/ip端口为xxx),服务也不相同,但是由于比较不全,我 ...
- my30_表碎片整理
确认表的类型与存储引擎,是否全部是innodb select TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE,ENGINE,VERSION,ROW_FORMAT,TABLE_RO ...
- JavaScript操作符(=?,)优先级
JavaScript操作符优先级: 关于最后3个运算符的优先级比较,下面通过一个实例来具体说明: var a,b,c; a = 3,4,5; b = a--,--a,a; c = a ? b++ : ...
- ThinkPHP find大坑 不要随便用
举例: M("User")->find(3); $m=M("User"); $m->userName="aaa"; $m-> ...