Count Squares
题目意思很简单,给一群不同的点,求这些点最多组成了几个正方形。
每此枚举2个点,作为正方行的一条边,然后顺时针/逆时针旋转90度(利用相对坐标来计算),得到另外2个点的坐标,查询HASH看这2个点如果都存在那么cnt++;
最后cnt/4,因为一个正方形被算过4次
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 1331 | Accepted: 552 |
Description
Given a set of points with integer coordinates xi, yi, i = 1...N, your program must find all the squares having each of four vertices in one of these points.
Input
Input file contains integer N followed by N pairs of integers xi yi.
Constraints
-104 ≤ xi, yi ≤ 104, 1 ≤ N ≤ 2000. All points in the input are different.
Output
Output file must contain a single integer — number of squares found.
Sample Input
Sample input 1
4 0 0 4 3 -3 4 1 7
Sample input 2
9
1 1 1 2 1 3
2 1 2 2 2 3
3 1 3 2 3 3
Sample Output
Sample output 1
1
Sample output 2
6
Hint
Bold texts appearing in the sample sections are informative and do not form part of the actual data.Source
Northeastern Europe 2005, Far-Eastern Subregion题目意思很简单,给一群不同的点,求这些点最多组成了几个正方形。
每此枚举2个点,作为正方行的一条边,然后顺时针/逆时针旋转90度(利用相对坐标来计算),得到另外2个点的坐标,查询HASH看这2个点如果都存在那么cnt++;
最后cnt/4,因为一个正方形被算过4次
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- {
- if(i==j)continue;
- //下面是重点-坐标变换
- TMP.x=P[j].y-P[i].y+P[j].x;
- TMP.y=P[i].x-P[j].x+P[j].y;
- if(isin(TMP))//isin查询HASH表中点是否存在
- {
- //坐标变换,得到另一个点
- TMP.x+=(P[i].x-P[j].x);
- TMP.y+=(P[i].y-P[j].y);
- if(isin(TMP))
- cnt++;
- }
- }
- printf("%d/n",cnt/4);