poj 2002 Squares

本文介绍了一种高效检测平面上由点集构成的正方形的方法。通过利用正方形的几何特性,算法可在给定点集中找到所有可能形成的正方形,避免了直接枚举带来的效率问题。文章详细解释了算法思路,包括如何通过已知两点推导出另两点坐标,以及使用哈希表进行快速查找。

题目连接

http://poj.org/problem?id=2002  

Squares

Description

A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property. 

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

The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.

Output

For each test case, print on a line the number of squares one can form from the given stars.

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

题意:给出一些点的集合求能组成正方形的个数。
思路:直接枚举的话$n = 1000\ \ \ O(n^4)$ 会超时。考虑换个思路:已知两个点,由正方形的几何特性
求出另外两个点的坐标,判断是否在点的集合里(哈希,二分,set。。。)都可我用的哈希。
具体先对所有的点按横坐标,纵坐标从小到大排序。统计个数$tot$,最后的答案即为${tot/2}$(正方形的对称性)
那么问题来了,如何已知两个点,求另外两个点呢?
现在给出公式:记$A(x_1,y_1)\ \ \ B(x_2,y_2) \ \ \overrightarrow {AB} =(x_2-x_1,y_2-y_1)$
另外两个点$C(x_3, y_3)\ \ \ D(x_4,y_4)\ \ \ $
$C:\ \ \ x_3 = y_1 - y_2 + x_1\ \ y_3 = x_2 - x_1 + y_1 $
$D:\ \ \ x_4= y_1 - y_2 + x_2\ \ y_4 = x_2 - x_1 + y_2 $
证明其实很简单记$\overrightarrow{X} = (a,b)$逆时针旋转$\beta$度得到$\overrightarrow{Y}(x,y)$
有:$x = acos\beta-bsin\beta\ \ y = asin\beta+bcos\beta$ (由三角函数的几何意义易得)
那么$\overrightarrow {AC} = (x_3-x_1,y_3-y_1)$
$x_3-x_1=(x_2-x_1)cos\beta - (y_2-y_1)sin\beta$
$y_3-y_1=(x_2-x_1)sin\beta + (y_2-y_1)cos\beta$ 其中$\beta = 90^0$
所以$x_3 = y_1-y_2+x_1\ \ y_3=x_2-x_1+y_1$
$\overrightarrow {BD}$同理(注意向量的方向和旋转方向)
原谅我孱弱的语文水平写的太挫了凑合看吧/(ㄒoㄒ)/~~

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::abs;
using std::sort;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 40007;
const int INF = 0x3f3f3f3f;
struct P {
    int x, y;
    P() {}
    P(int i , int j) :x(i), y(j) {}
    inline bool operator<(const P &k) const {
        return x == k.x ? y < k.y : x < k.x;
    }
}A[N];
struct Hash_Set {
    int tot, head[N];
    struct edge { int x, y, next; }G[N];
    inline void init() {
        tot = 0, cls(head, -1);
    }
    inline void insert(P &k) {
        int u = abs(k.x + k.y) % N;
        G[tot] = (edge){ k.x, k.y, head[u] }; head[u] = tot++;
    }
    inline bool find(P k) {
        int u = abs(k.x + k.y) % N;
        for(int i = head[u]; ~i; i = G[i].next) {
            edge &e = G[i];
            if(k.x == e.x && k.y == e.y) return true;
        }
        return false;
    }
}hash;
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w+", stdout);
#endif
    int n, x1, x2, x3, x4, y1, y2, y3, y4, ans;
    while(~scanf("%d", &n), n) {
        hash.init();
        rep(i, n) {
            scanf("%d %d", &A[i].x, &A[i].y);
            hash.insert(A[i]);
        }
        sort(A, A + n);
        ans = 0;
        for(int i = 0; i < n; i++) {
            for(int j = i + 1; j < n; j++) {
                x1 = A[i].x, y1 = A[i].y;
                x2 = A[j].x, y2 = A[j].y;
                x3 = y1 - y2 + x1, y3 = x2 - x1 + y1;
                x4 = y1 - y2 + x2, y4 = x2 - x1 + y2;
                if(hash.find(P(x3, y3)) && hash.find(P(x4, y4))) ans++;
            }
        }
        printf("%d\n", ans >> 1);
    }
    return 0;
}

转载于:https://www.cnblogs.com/GadyPu/p/4776659.html

先看效果: https://renmaiwang.cn/s/jkhfz Hue系列产品将具备高度的个性化定制能力,并且借助内置红、蓝、绿三原色LED的灯泡,能够混合生成1600万种不同色彩的灯光。 整个操作流程完全由安装于iPhone上的应用程序进行管理。 这一创新举措为智能照明控制领域带来了新的启示,国内相关领域的从业者也积极投身于相关研究。 鉴于Hue产品采用WiFi无线连接方式,而国内WiFi网络尚未全面覆盖,本研究选择应用更为普及的蓝牙技术,通过手机蓝牙与单片机进行数据交互,进而产生可调节占空比的PWM信号,以此来控制LED驱动电路,实现LED的调光功能以及DIY调色方案。 本文重点阐述了一种基于手机蓝牙通信的LED灯设计方案,该方案受到飞利浦Hue智能灯泡的启发,但考虑到国内WiFi网络的覆盖限制,故而选用更为通用的蓝牙技术。 以下为相关技术细节的详尽介绍:1. **智能照明控制系统**:智能照明控制系统允许用户借助手机应用程序实现远程控制照明设备,提供个性化的调光及色彩调整功能。 飞利浦Hue作为行业领先者,通过红、蓝、绿三原色LED的混合,能够呈现1600万种颜色,实现了全面的定制化体验。 2. **蓝牙通信技术**:蓝牙技术是一种低成本、短距离的无线传输方案,工作于2.4GHz ISM频段,具备即插即用和强抗干扰能力。 蓝牙协议栈由硬件层和软件层构成,提供通用访问Profile、服务发现应用Profile以及串口Profiles等丰富功能,确保不同设备间的良好互操作性。 3. **脉冲宽度调制调光**:脉冲宽度调制(PWM)是一种高效能的调光方式,通过调节脉冲宽度来控制LED的亮度。 当PWM频率超过200Hz时,人眼无法察觉明显的闪烁现象。 占空比指的...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值