POJ2002 Squares(枚举)
题目链接。
分析:
普遍的做法是:先枚举两个点,通过数学公式得到另外2个点,使得这四个点能够成正方形。然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形。
但这种做法会使同一个正方形按照不同的顺序被枚举了四次,因此最后的结果要除以4.
已知: (x1,y1) (x2,y2)
则: x3=x1+(y1-y2) y3= y1-(x1-x2)
x4=x2+(y1-y2) y4= y2-(x1-x2)
或
x3=x1-(y1-y2) y3= y1+(x1-x2)
x4=x2-(y1-y2) y4= y2+(x1-x2)
直接hash太麻烦,使用set简单些.
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <set> using namespace std; const int maxn = + ;
const int VAL = ; typedef long long LL; set<LL> st; struct Pos {
int x, y;
}pos[maxn]; int main() {
int n, x3, y3, x4, y4; while(scanf("%d", &n) == && n) {
st.clear();
for(int i=; i<n; i++) {
scanf("%d %d", &pos[i].x, &pos[i].y);
st.insert((LL)pos[i].x*VAL+pos[i].y);
} int ans = ; for(int i=; i<n; i++) {
int x1 = pos[i].x, y1 = pos[i].y;
for(int j=i+; j<n; j++) {
int x2 = pos[j].x, y2 = pos[j].y;
x3 = x1+(y1-y2); y3 = y1-(x1-x2);
x4 = x2+(y1-y2); y4 = y2-(x1-x2); if( st.count((LL)x3*VAL+y3) && st.count((LL)x4*VAL+y4)) {
ans++;
} x3 = x1-(y1-y2); y3 = y1+(x1-x2);
x4 = x2-(y1-y2); y4 = y2+(x1-x2); if( st.count((LL)x3*VAL+y3) && st.count((LL)x4*VAL+y4)) {
ans++;
}
}
} printf("%d\n", ans/);
} return ;
}
POJ2002 Squares(枚举)的更多相关文章
- poj2002 Squares(hash+折半枚举)
Description A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-d ...
- POJ-2002 Squares,哈希模板+数学公式!
Squares 题意:二维坐标轴给出n个点求有多少个正方形. 要是平时做比赛的话毫无疑问会 ...
- [POJ2002]Squares(计算几何,二分)
题目链接:http://poj.org/problem?id=2002 给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化. 有公 ...
- Poj2002 Squares
题意描述:有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少.相同的四个点,不同顺序构成的正方形视为同一正方形. 思路变迁: 1.最简单的方法,直接暴力搜索,即依次取四个顶点,根据其坐标 ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares 数学题枚举
D. Spongebob and Squares Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- UVA12113-Overlapping Squares(二进制枚举)
Problem UVA12113-Overlapping Squares Accept:116 Submit:596 Time Limit: 3000 mSec Problem Descripti ...
- poj2002 hash+邻接表优化Squares
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 17487 Accepted: 6643 Descript ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
- Squares(枚举+set 查找)
http://poj.org/problem?id=2002 题意:给出n组坐标,判断这些坐标能组成的正方形的个数. 思路:参考某大神的想法,先枚举两个点,然后利用公式表示出另外两个点,判断这两个点是 ...
随机推荐
- ipad itunes 恢复
http://jingyan.baidu.com/article/a3aad71aa58efbb1fa00967c.html http://act.feng.com/wetools/index.php ...
- git 指令汇总
学习git过程中整理的笔记: git add 添加文件到暂存区: git commit -m "更改说明" 提交文件更改: git status 查看当前文件状态: git dif ...
- HDU 4294 Multiple(搜索+数学)
题意: 给定一个n,让求一个M,它是n个倍数并且在k进制之下 M的不同的数字最少. 思路: 这里用到一个结论就是任意两个数可以组成任何数的倍数.知道这个之后就可以用搜索来做了.还有一个问题就是最多找n ...
- n进制转为十进制
主程序代码 - #include <stdio.h> #include <string.h> main() { long t1; int i, n, t, t3; ]; pri ...
- jQuery注册验证
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PHP 通过随机数获得ASCII 值返回字符。
for($i=0;$i<20;$i++){ $Matrix .= chr(65 + rand(0,26)); }
- jquery实现很简单的DIV拖动
今天用jquery实现了一个很简单的拖动...实现思路很简单 如下: 在thickbox 弹出层内实现拖拽DIV,那么得进行一下相对宽高的运算:必须加上相对于可见窗口宽高和弹出层宽高之间的差: ...
- 怎样在thinkphp里面执行原生的sql语句
$Model = new Model(); $sql = "select * from `order`"; $voList = $Model->query($sql); 只是 ...
- ios9 http请求失败的问题
最近做项目的时候 将电脑版本升级到10.11.3 xcode'升级到 7.2 但是在模拟器上边进行数据请求的时候告诉我说网路哦有问题 截图如下 通过网络终于找到了解决的办法 原来是ios9 采用 ...
- SGU 106.Index of super-prime
时间限制:0.25s 空间限制:4M 题目大意: 在从下标1开始素数表里,下标为素数的素数,称为超级素数(Super-prime),给出一个n(n<=10000) ...