Day3-I-Squares POJ2002
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
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 思路:一个正方形的四个顶点可由2个来算出,枚举这两个点,然后搜索剩下的点,O(N^2logN),如图:
此时枚举x1,x2,x3 = x2+-(y1-y2), y3 = y2+-(x1-x2), x4,y4同理,代码如下:
const int maxm = ;
struct Node {
int x, y;
bool operator<(const Node &a)const {
return x < a.x || (x == a.x && y < a.y);
}
} buf[maxm];
int n;
int main() {
while(scanf("%d",&n) != EOF && n) {
int ans = ;
Node t1, t2;
for (int i = ; i < n; ++i) {
scanf("%d%d", &buf[i].x, &buf[i].y);
}
sort(buf, buf + n);
for (int i = ; i < n - ; i++) {
for (int j = i + ; j < n; ++j) {
t1.x = buf[j].x + (buf[i].y - buf[j].y);
t1.y = buf[j].y - (buf[i].x - buf[j].x);
t2.x = buf[i].x + (buf[i].y - buf[j].y);
t2.y = buf[i].y - (buf[i].x - buf[j].x);
if(binary_search(buf,buf+n,t1) && binary_search(buf,buf+n,t2))
++ans;
t1.x = buf[j].x - (buf[i].y - buf[j].y);
t1.y = buf[j].y + (buf[i].x - buf[j].x);
t2.x = buf[i].x - (buf[i].y - buf[j].y);
t2.y = buf[i].y + (buf[i].x - buf[j].x);
if(binary_search(buf,buf+n,t1) && binary_search(buf,buf+n,t2))
++ans;
}
}
printf("%d\n", ans / );
}
return ;
}
Day3-I-Squares POJ2002的更多相关文章
- [poj2002]Squares_hash
Squares poj-2002 题目大意:在笛卡尔坐标系中给出n个点,求这些点可以构成多少个正方形. 注释:$1\le n\le 10^3$,$-2\cdot 10^3\le x , y\le 2\ ...
- poj2002 Squares(hash+折半枚举)
Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-d ...
- poj2002 hash+邻接表优化Squares
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 17487 Accepted: 6643 Descript ...
- POJ-2002 Squares,哈希模板+数学公式!
Squares 题意:二维坐标轴给出n个点求有多少个正方形. 要是平时做比赛的话毫无疑问会 ...
- [POJ2002]Squares(计算几何,二分)
题目链接:http://poj.org/problem?id=2002 给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化. 有公 ...
- Poj2002 Squares
题意描述:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少.相同的四个点,不同顺序构成的正方形视为同一正方形. 思路变迁: 1.最简单的方法,直接暴力搜索,即依次取四个顶点,根据其坐标 ...
- POJ2002 Squares(枚举)
题目链接. 分析: 普遍的做法是:先枚举两个点,通过数学公式得到另外2个点,使得这四个点能够成正方形.然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形. 但这种做法会使同一个正方形 ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- 卡通图像变形算法(Moving Least Squares)附源码
本文介绍一种利用移动最小二乘法来实现图像变形的方法,该方法由用户指定图像中的控制点,并通过拖拽控制点来驱动图像变形.假设p为原图像中控制点的位置,q为拖拽后控制点的位置,我们利用移动最小二乘法来为原图 ...
- Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
随机推荐
- 【渗透测试】Squirrelmail远程代码执行漏洞+修复方案
最近网上有点不太平,爆出各种漏洞,等下会把近期的漏洞复现一下,发出来.安全圈的前辈总是默默的奉献,在这里晚辈们只能站在巨人的肩膀上,跟紧前辈们的步伐,走下去. -------------------- ...
- GO ERR
o 语言通过内置的错误接口提供了非常简单的错误处理机制. error类型是一个接口类型,这是它的定义: type error interface { Error() string } 我们可以在编码中 ...
- 引入C/C++动态库
[DllImport("SocketAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = t ...
- node vue 项目git 管理
push 上传到云的时候,依赖包及相关文件是不上传上去的, 所以每次克隆到本地后,node 项目运行前须要 npm install 安装对应依赖 vue 项目编译前也须要 npm install,安 ...
- 使用类进行面向对象编程 Class 实例化 和 ES5实例化 对比,继承
ES5 写法 function Book(title, pages, isbn) { this.title = title; this.pages = pages; this.isbn = isbn; ...
- iview table表格内容为数组或者对象的子元素时问题讨论
正常情况下,iview框架table表格内容只需配置好 key 就OK, 稍微复杂点就是用一个reder函数进行操作(params.row 为本行数据) . 以上问题都很好解决,无需太动脑筋. 开发中 ...
- 「NOI2009」植物大战僵尸
「NOI2009」植物大战僵尸 传送门 这是一道经典的最大权闭合子图问题,可以用最小割解决(不会的可以先自学一下) 具体来说,对于这道题,我们对于两个位置的植物 \(i\) 和 \(j\) ,如果 \ ...
- maven设置jdk版本
方法一:在maven文件夹下的settings.xml中添加 <profile> <id>jdk-1.8</id> <activation> <a ...
- ajax相同url和参数,将不会重复发起请求
IE浏览器下使用GET发送请求时,如果两次请求的地址和参数相同,在不刷新页面的情况下,浏览器会缓存第一次请求的内容,服务端更新后浏览器仍然显示第一次的内容. 解决办法: 一. GET请求URL后加随机 ...
- python中 with 的作用
with是 Python2.5 引入的一个新语法,它是一种上下文管理协议,目的在于从流程中吧 try...except 和 finally 关键字和资源释放相关代码统统去掉,简化 try...exce ...
