首页 > 其他 > 详细

poj 2002 Squares 几何二分

时间:2014-02-25 10:38:52      阅读:379      评论:0      收藏:0      [点我收藏+]
Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 15137   Accepted: 5749

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

Source

 
bubuko.com,布布扣
 1 /*
 2 题意:给你1000个点的坐标(x,y),让你找出能
 3             构成正方形的个数。
 4 思路:由于是1000,则枚举两个点,求出相应的另外
 5 两个点的坐标。然后用二分判断是否两个点都存在。
 6 
 7 
 8 就个人而言,关键在  "求出相应的另外两个点的坐标"
 9 设两个点a1,a2;
10 由a1为中心,逆时针旋转求出
11 a3.x=a1.y-a2.y+a1.x;
12 a3.y=a2.x-a1.x+a1.y;
13 由a2为中心,顺时针旋转求出
14 a4.x=a1.y-a2.y+a2.x;
15 a4.y=a2.x-a1.x+a2.y;
16 由于被计算两次,所以除2
17 */
18 #include<iostream>
19 #include<stdio.h>
20 #include<cstring>
21 #include<cstdlib>
22 #include<algorithm>
23 using namespace std;
24 
25 typedef struct
26 {
27     int x,y;
28 }node;
29 node a[1003];
30 bool cmp(node n1,node n2)
31 {
32     if( n1.x!=n2.x )
33         return n1.x<n2.x;
34     else return n1.y<n2.y;
35 }
36 bool query(int l,int r,node cur)
37 {
38     int mid;
39     while(l<=r)
40     {
41         mid=(l+r)/2;
42         if( a[mid].x<cur.x || (a[mid].x==cur.x&&a[mid].y<cur.y))
43             l=mid+1;
44         else if( a[mid].x>cur.x || ( a[mid].x==cur.x&&a[mid].y>cur.y))
45             r=mid-1;
46         if( a[mid].x==cur.x && a[mid].y==cur.y) return true;
47     }
48     return false;
49 }
50 int main()
51 {
52     int n,i,j,num;
53     node a1,a2,a3,a4;
54     while(scanf("%d",&n)>0)
55     {
56         if(n==0)break;
57         for(i=1;i<=n;i++)
58             scanf("%d%d",&a[i].x,&a[i].y);
59         sort(a+1,a+1+n,cmp);
60 
61         for(i=1,num=0;i<n;i++)
62         {
63             a1=a[i];
64             for(j=i+1;j<=n;j++)
65             {
66                 a2=a[j];
67                 a3.x=a1.y-a2.y+a1.x;
68                 a3.y=a2.x-a1.x+a1.y;
69                 if( !query(1,n,a3)) continue;
70                 a4.x=a1.y-a2.y+a2.x;
71                 a4.y=a2.x-a1.x+a2.y;
72                 if( query(1,n,a4)) num++;
73 
74             }
75         }
76         printf("%d\n",num/2);
77     }
78     return 0;
79 }
bubuko.com,布布扣

 

poj 2002 Squares 几何二分

原文:http://www.cnblogs.com/tom987690183/p/3564375.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!