Problem Description
On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make.
Input
The input file consists of several test cases. Each case the first line is a numbers N (N <= 500). The next N lines ,each line contain two number Xi and Yi(-100 <= xi,yi <= 100), means the points’ position.(the data assures no two points share the same position.)
Output
For each case, output a number means how many different regular polygon these points can make.
Sample Input
4
0 0
0 1
1 0
1 1
6
0 0
0 1
1 0
1 1
2 0
2 1
Sample Output
1
2
判断有多少个正方形,几何题目。
假设正方形的四个点 左下(x1,y1),右下(x2,y2),右上(x3,y3),左上(x4,y4),其中(x1,y1)和(x3,y3)是一对对角点。一定有y3-y1 = x3-x1。
(nx+dy)&1 和(ny+dx)&1 不成立时,说明这两个点一定不能组成一个正方形的对角点,以(x1,y1),(x3,y3)当例子,(nx+dy) = x1+x3+y3-y1 = x1+x3+x3-x1 = 2*x3,
ny+dx = y1+y3+y3-y1 = 2*y3,一定不是奇数。其中((nx+dy)/2,(ny+sg*dx)/2)是求(x3,y1)即(x2,y2),((nx-dy)/2,(ny-sg*dx)/2)是求(x1,y3)即(x4,y4)。
由于一个正方形重复计算了两遍,结果除2就是答案了。
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <set> 5 using namespace std; 6 int x[550], y[550]; 7 int main() { 8 int n; 9 while(scanf("%d",&n)!=EOF) { 10 set<pair<int,int> > st; 11 for(int i = 0; i < n; i ++) { 12 scanf("%d%d",&x[i],&y[i]); 13 st.insert(make_pair(x[i],y[i])); 14 } 15 int ans = 0; 16 for(int i = 0; i < n; i ++) { 17 for(int j = i+1; j < n; j ++) { 18 int nx = x[i]+x[j], dx = max(x[i],x[j])-min(x[i],x[j]); 19 int ny = y[i]+y[j], dy = max(y[i],y[j])-min(y[i],y[j]); 20 if((nx+dy)&1 || (ny+dx)&1) continue; 21 int sg = ((x[i]-x[j])*(y[i]-y[j]) < 0)?1:-1; 22 if(st.count(make_pair((nx+dy)/2,(ny+sg*dx)/2)) && st.count(make_pair((nx-dy)/2,(ny-sg*dx)/2))) 23 ans++; 24 } 25 } 26 printf("%d ",ans/2); 27 } 28 return 0; 29 }