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 ...
随机推荐
- django终端打印Sql语句
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console':{ 'level':'DEBU ...
- Jenkins自动化CI CD流水线之6--构建邮件状态通知
一. 前提 前提: 服务器开启邮箱服务: 二. 基础配置 需要安装一个插件: 插件: Email Extension Plugin 进行配置: 系统管理->系统设置-> 相关配置如下图: ...
- oracle中所有表的字段和注释
select t1.owner ,t1.table_name ,t1.column_id ,t1.column_name ,t1.data_type ,t2.comments from all_tab ...
- mc03_IntelliJ IDEA配置github
配置本地git仓库 首先配置一个本地的git仓库,熟悉一下git上传文件到github的过程,具体操作参考 mc02_配置本地git仓库并上传到github IntelliJ IDEA与github的 ...
- scp —— 服务器之间互传文件
scp 可以在 2个 linux 主机间复制文件: 从 本地 复制到 远程 * 复制文件: 举例子: scp /home/space/music/.mp3 root@192.168.0.1 ...
- 转Linux 下用alias 设置命令别名快速切换常用命令
https://blog.csdn.net/u012830148/article/details/80618616 在linux下开发,经常需要切换目录,如果目录很长则切换起来非常的麻烦,针对一些常用 ...
- nodejs的异步非阻塞IO
简单表述一下:发启向系统IO操作请求,系统使用线程池IO操作,执行完放到事件队列里,node主线程轮询事件队列,读取结果与调用回调.所以说node并非真的单线程,还是使用了线程池的多线程. 上个图看看 ...
- F. Cooking Time 贪心
http://codeforces.com/gym/101498/problem/F 对于知道使用情况的置换算法,最优解是找一个最后需要使用的物品替换掉 也就是,如果一个物品后面已经不需要用到,就要拿 ...
- 纯CSS控制背景图片100%自适应填充布局
https://blog.csdn.net/wd4java/article/details/50537562 解决: html,body{height: 100%;width: 100%;marg ...
- 报表XtraReport创建是实现
1.创建XtraReport报表程序 一般设计这个程序是分着的,为了方便我就把他们合到一起 首先创建一个Winform Application 在form1中放一个button,右键程序,添加新项 如 ...