17暑假多校联赛2.11 HDU 6055 Regular polygon

Regular polygon

Time Limit: 3000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

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个点,判断这些点能够构成多少个正多边形
形成正多边形需要每条边都相等,因为没点坐标都为整数,所以能构成的正多边形只有正方形,可以先将各个点排序可以按照先横坐标再纵坐标,也可以按照先纵坐标再横坐标,然后先选中两个点再往这两个点的上方或者右上方确定能组成正方形的点的坐标,然后判断这两个坐标是否在已给的坐标中

代码
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#define pf(a) a*a
using namespace std;
int N;
struct point
{
    int x,y;
} p[510];///存放给的点的坐标
bool cmp(struct point a,struct point b)
{
    if(a.y==b.y)///按照在坐标系中的位置先下再左的顺序排序
    {
        return a.x<b.x;
    }
    else return a.y<b.y;
}
bool findp(int x,int y)///查找点(x,y)是否在给定的点中
{
    for(int i=0; i<N; i++)
    {
        if(y==p[i].y&&x==p[i].x)
        {
            return true;
        }
    }
    return false;
}
int main()
{
    while(~scanf("%d",&N))
    {
        int sum=0;
        for(int i=0; i<N; i++)
        {
            scanf("%d %d",&p[i].x,&p[i].y);
        }
        sort(p,p+N,cmp);///将给的点排序
        for(int i=0; i<N-1; i++)
        {
            for(int j=i+1; j<N; j++)///让所有点都相互连接来作为正方形的一条边
            {
                if(p[i].y==p[j].y||(p[j].y>p[i].y&&p[j].x<p[i].x))///排序之后点p[j]的y值总是大于等于p[i]的y,因为要避免重复查点,所以只查询上方和右上方的可以构成正方形的点,所以p[j]的y大于p[i]的y时,它的x要小于p[i]的x,然后查找满足构成正方形的点
                {
                    if(p[i].y==p[j].y)
                    {
                        if((findp(p[i].x,(p[i].y+abs(p[i].x-p[j].x))))&&findp(p[j].x,(p[j].y+abs(p[i].x-p[j].x))))
                            sum++;
                    }
                    else
                    {
                        if((findp((p[i].x+abs(p[i].y-p[j].y)),(p[i].y+abs(p[i].x-p[j].x))))&&(findp((p[j].x+abs(p[i].y-p[j].y)),(p[j].y+abs(p[i].x-p[j].x)))))
                            sum++;
                    }
                }

            }
        }
        printf("%d\n",sum);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值