Regular polygon HDU - 6055

本文介绍了一种算法,该算法通过输入一系列整数坐标点来找出这些点能构成的不同正方形的数量。通过枚举点对并检查是否存在对应的对角点对来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


一道简单的几何题, 由于输入的是整数点,那么在整数点上只能找到正四边形, 其余的都是无法构造的。所以枚举输入的任意两点,以这两点连成的线段为对角线,看另外两个点是否存在就可以得到答案了。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <math.h>
using namespace std;
int n;
struct point{
    int x, y;
};
point v[510];
int map1[500][500];
bool check(int x)
{
    if (-100 <= x && x <= 100)
        return true;
    else return false;
}
int main()
{
    while(scanf("%d", &n) != EOF) {
        memset(map1, 0, sizeof(map1));
        for (int i = 1; i <= n; i++) {
            scanf("%d%d", &v[i].x, &v[i].y);
            map1[v[i].x+100][v[i].y+100] = 1;
        }
        long long int sum = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) if (i != j){
                int x1 = v[j].x + v[i].x;
                int y1 = v[j].y + v[i].y;
                int x2 = (v[j].y - v[i].y + x1) ;
                int y2 = (v[i].x - v[j].x + y1) ;
                if (x2 % 2 != 0 || y2 % 2 != 0) continue;
                    x2 /= 2;
                    y2 /= 2;
                if (check(x2) && check(y2) && map1[x2+100][y2+100]) {
                    int x3 = (v[i].y - v[j].y + x1);
                    int y3 = (v[j].x - v[i].x + y1);
                    if (x3 % 2 != 0 || y3 % 2 != 0) continue;
                    x3 /= 2;
                    y3 /= 2;
                    if (check(x3) && check(y3) && map1[x3+100][y3+100])
                        sum++;
                }
            }
        }
        cout <<sum/4<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值