Regular polygon
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2279 Accepted Submission(s): 912
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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6055
题意:输入n个点的坐标,每个点的坐标在-100到100之间。
要求:输出由输入的点构成正多边形的个数。
解题思路:由整数点构成的多边形只能是正方形,这一点很关键,知道这个的话题目也就容易了很多,构造一个存储输入点坐标的结构体,输入的点数最多为500个点,点的范围在-100到100,定义一个int型的二维数组,下标代表的是下标-100的点的坐标,初始值为0,每当输入一个点就把对应的下标值改为1。将每个点按照x和y的升序排序,然后将每个点遍历一遍,再确定一个点之后根据公式判断剩下两点是否存在就好了就好了。
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Point{
int x;
int y;
};
int n;
Point point[500+10];//存数输入点的信息
int exist[201][201];//判断点是否存在
int cmp(Point p1,Point p2)//将输入的点按照x的升序和y的升序排序
{
if(p1.x!=p2.x) return p1.x<p2.x;
else return p1.y<p2.y;
}
int count()
{
int i,j;
int x1,y1,x2,y2;
int dx,dy;
int cnt;
cnt=0;
for(i=0;i<n;i++)//遍历输入的点
{
x1=point[i].x;
y1=point[i].y;
for(j=i+1;j<n;j++)//找到改点右边存在的一个点
{
dx=point[j].x-x1;
dy=point[j].y-y1;
if(dy<=0) continue;
x2=x1+dx+dy;
y2=y1+dy-dx;
if(x2<201 && y2>=0 && y2<201)//根据公式判断剩下的两个点是否存在
if(exist[x2][y2]==1)
{
x2=x1+dy;
y2=y1-dx;
if(x2<201 && y2>=0)
if(exist[x2][y2]==1)
cnt++;
}
}
}
return cnt;
}
int main(void)
{
int i;
int x,y;
while(scanf("%d",&n)==1)
{
memset(exist,0,sizeof(exist));//初始化
for(i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
point[i].x=x+100;
point[i].y=y+100;
exist[x+100][y+100]=1;
}
sort(point,point+n,cmp);
printf("%d\n",count());
}
return 0;
}