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 ...
随机推荐
- python 中的 int() 与 round
int(x):向下取整 round(x):超过 .5 则向上取整,否则向下取整
- 九、Appium-python-UI自动化之通过text定位
1.通过xpath定位text xpath路径为://android.widget.EditText[@text='请输入包含街道的完整地址'] 2.通过AndroidUIAutomator # 这个 ...
- Docker 安装 Logstash
使用同版本镜像 7.4.1 1.下载Logstash镜像 docker pull logstash: #查看镜像 docker images 2.编辑logstash.yml配置文件logstash. ...
- OpenThreads库学习
在看STM32资料中看到STM32WB系列“双核无线微控制器配备双核无线微控制器(MCU)配备Bluetooth® 5.OpenThread和ZigBee®3.0连接技术,同时兼备超低功耗性能“不知道 ...
- 使用maven搭建web项目
在pom.xml中添加java ee相关的三个依赖包:<scope> jar的有效范围 provided 表示编译期生效,不会打包发布到 tomcat 中 <properties&g ...
- JavaScript - Compiling Vs Transpiling
参考 https://blog.csdn.net/napolunyishi/article/details/20473799 https://www.stevefenton.co.uk/2012/11 ...
- Keras的TimeDistributed层
Keras的TimeDistributed层主要用途是在时间维度上进行全连接. 比如Faster RCNN,1张图生成了16个ROI,需要对每一个ROI进行分类和回归,ROI的维度是7×7×512,长 ...
- 一个linuxk开发板的开发笔记
arm-fsl-linux-gnueabi开发笔记 //开发主机系统信息 $ lsb_release -a No LSB modules are available. Distributor ID:U ...
- [蓝桥杯2015决赛]穿越雷区(BFS求最短路)
题目描述 X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废.某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?已 ...
- C 语言入门第十二章---C语言文件操作
C语言具有操作文件的能力,比如打开文件.读取和追加数据.插入和删除数据.关闭文件.删除文件等. 在操作系统中,为了同意对各种硬件的操作,简化接口,不同的硬件设备也都被看成一个文件.对这些文件的操作,等 ...
