hdu6055--Regular polygon

来源:互联网 发布:淘宝正品代购店 编辑:程序博客网 时间:2024/06/14 08:20

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

40 00 11 01 160 00 11 01 12 02 1

Sample Output

12

题解:

二维平面内有n个点,计算这n个点能组成多少个正多边形
即能组成多少个正方形

分析

假设a(x1, y2)和b(x2, y2)是这n个点中的两个点
x = x1 - x2, y = y1 - y2
那么x代表横坐标之差,y代表纵坐标之差
那么只需要判断,以a b为边的上下两个正方形,其他两顶点是否存在即可

其余两顶点分别为(x1+y, y1-x)(x2+y, y2-x)
或者(x1-y, y1+x)(x2-y, y2+x)

因为题目中已经把负数化作正数放进数组(结构体)中,所以要判断该点是否为正
并且,正方形有四条边
计算的时候会按照四条边的来计算
所以最终结果要再除以4


代码:

#include<cstdio>#include<cstring>#include<iostream>using namespace std;int vis[500][500];struct node{    int x,y;}p[505];int judge(node a,node b)//判断两个点是否能组成正方形{    int ans=0;    int x=a.x-b.x;    int y=a.y-b.y;    if(a.x+y>=0&&a.y-x>=0&&b.x+y>=0&&b.y-x>=0&&vis[a.x+y][a.y-x]&&vis[b.x+y][b.y-x])        ans++;    if(a.x-y>=0&&a.y+x>=0&&b.x-y>=0&&b.y+x>=0&&vis[a.x-y][a.y+x]&&vis[b.x-y][b.y+x])        ans++;    return ans;}int main(){    int n;    while(~scanf("%d",&n))    {        int ans=0;        memset(vis,0,sizeof(vis));        for(int i=0;i<n;i++)        {            int x,y;            scanf("%d%d",&x,&y);            x+=200;            y+=200;            p[i].x=x;            p[i].y=y;            vis[x][y]=1;        }        for(int i=0;i<n-1;i++)        {            for(int j=i+1;j<n;j++)            {                ans+=judge(p[i],p[j]);            }        }        cout<<ans/4<<endl;    }    return 0;}

当然,也可以根据对角线来算,通过二分查找的方式查找顶点存在情况
那么要计算两个点的中点,可以唯一确定四边形位置
结果只需要除以2就可以

代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include<iostream>using namespace std ;#define eps 1e-7struct node{    double x,y;} p[510];int n;bool cmp(node a,node b){    if(a.x==b.x)        return a.y<b.y;    return a.x<b.x;}bool judge(double x,double y)       // 二分查找{    int low=0,high=n-1;    while(low<=high)    {        int mid=(low+high)/2;        if(fabs(p[mid].x-x)<eps&&fabs(p[mid].y-y)<eps)            return true;        else if(p[mid].x-x>eps||(fabs(p[mid].x-x)<eps&&p[mid].y-y>eps))            high=mid-1;        else low=mid+1;    }    return false;}int main(){    while(cin>>n)    {        int ans=0;        for(int i=0; i<n; i++)            cin>>p[i].x>>p[i].y;        sort(p,p+n,cmp);        for(int i=0; i<n; i++)        {            for(int j=i+1; j<n; j++)            {                if(i==j)continue;                double xa=(p[i].x+p[j].x)/2.0;                double ya=(p[i].y+p[j].y)/2.0;                double xb=p[i].x-xa;                double yb=p[i].y-ya;                if(judge(xa+yb,ya-xb)&&judge(xa-yb,ya+xb))                    ans++;            }        }        cout<<ans/2<<endl;    }    return 0;}



链接:

http://acm.hdu.edu.cn/showproblem.php?pid=6055

原创粉丝点击