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
题意:给出n个坐标点(皆在格点上),求问这些点可以构成几个正多边形?
题解:
1.所有点皆在格点上只有一个情况,那便是正四边形
2.任意枚举两个点,求出另两个点的坐标,来观察是否在给出的点中,若皆存在,cnt++。
求另两个坐标的方法:设已知两个点为a,b(a.x<b.x),另两个点为c,d,设c是直接与b相连的,d是直接与a相连的
如图方法可以求出c-b,d-a的x,y变化分别为 disx=abs(a.y-b.y),disy=abs(a.x-b.x),然后即可以通过一条边计算它左右两个正方形。具体见代码。
3.最后去重,因为一个四边形,它的四条边都计算过它一次,因此将最后的结果/4。
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<string.h> 6 using namespace std; 7 8 bool vis[205][205]; 9 //把所有值都加100 10 11 struct node 12 { 13 int x,y; 14 }point[505]; 15 16 bool cmp(node a,node b) 17 { 18 return a.x<b.x; 19 } 20 21 bool judge(node c) 22 { 23 if(c.x>=0&&c.x<=200&&c.y>=0&&c.y<=200) 24 if(vis[c.x][c.y]) 25 return true; 26 return false; 27 } 28 29 30 int main() 31 { 32 int n,cnt; 33 while(~scanf("%d",&n)) 34 { 35 memset(vis,false,sizeof(vis)); 36 for(int i=0;i<n;i++) 37 { 38 scanf("%d%d",&point[i].x,&point[i].y); 39 point[i].x+=100; 40 point[i].y+=100; 41 vis[point[i].x][point[i].y]=true; 42 } 43 sort(point,point+n,cmp); 44 cnt=0; 45 node a,b,c,d; 46 int disx,disy; 47 for(int i=0;i<n;i++) 48 { 49 for(int j=i+1;j<n;j++) 50 { 51 a=point[i]; 52 b=point[j]; 53 disx=abs(a.y-b.y); 54 disy=abs(a.x-b.x); 55 if(b.y<=a.y) 56 { 57 //右上 58 c.x=b.x+disx; 59 c.y=b.y+disy; 60 d.x=a.x+disx; 61 d.y=a.y+disy; 62 if(judge(c)&&judge(d)) 63 cnt++; 64 //左下 65 c.x=b.x-disx; 66 c.y=b.y-disy; 67 d.x=a.x-disx; 68 d.y=a.y-disy; 69 if(judge(c)&&judge(d)) 70 cnt++; 71 } 72 else 73 { 74 //右下 75 c.x=b.x+disx; 76 c.y=b.y-disy; 77 d.x=a.x+disx; 78 d.y=a.y-disy; 79 if(judge(c)&&judge(d)) 80 cnt++; 81 //左上 82 c.x=b.x-disx; 83 c.y=b.y+disy; 84 d.x=a.x-disx; 85 d.y=a.y+disy; 86 if(judge(c)&&judge(d)) 87 cnt++; 88 } 89 } 90 } 91 printf("%d ",cnt/4); 92 } 93 return 0; 94 }