hdu6055--Regular polygon

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个点,计算这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-7
struct 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值