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(n4) 会超时。考虑换个思路:已知两个点,由正方形的几何特性
求出另外两个点的坐标,判断是否在点的集合里(哈希,二分,set。。。)都可我用的哈希。
具体先对所有的点按横坐标,纵坐标从小到大排序。统计个数tot,最后的答案即为tot/2(正方形的对称性)
那么问题来了,如何已知两个点,求另外两个点呢?
现在给出公式:记A(x1,y1) B(x2,y2) →AB=(x2−x1,y2−y1)
另外两个点C(x3,y3) D(x4,y4)
C: x3=y1−y2+x1 y3=x2−x1+y1
D: x4=y1−y2+x2 y4=x2−x1+y2
证明其实很简单记→X=(a,b)逆时针旋转β度得到→Y(x,y)
有:x=acosβ−bsinβ y=asinβ+bcosβ (由三角函数的几何意义易得)
那么→AC=(x3−x1,y3−y1)
x3−x1=(x2−x1)cosβ−(y2−y1)sinβ
y3−y1=(x2−x1)sinβ+(y2−y1)cosβ 其中β=900
所以x3=y1−y2+x1 y3=x2−x1+y1
→BD同理(注意向量的方向和旋转方向)
原谅我孱弱的语文水平写的太挫了凑合看吧/(ㄒoㄒ)/~~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #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; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 【译】我们最喜欢的2024年的 Visual Studio 新功能
· 如何打造一个高并发系统?