HDU 6055 Regular polygon

来源:互联网 发布:维多利亚2 mac 中文 编辑:程序博客网 时间:2024/05/16 09:23

【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=6055

题目意思

给定n个点问你可以形成多少个正多边形。

解题思路

因为给定点为整数,所以只能形成正4边形,因此从上往下,从左往右。任意两个点两两判断剩余其余两点是否在给定点中,最后答案除2,因为ab和ba重复计算

代码部分

#include <bits/stdc++.h>using namespace std;const int maxn=510;struct Node   ///存储点{    int x,y;    bool operator<(const Node &rhs)const  ///排序从上往下从左往右    {        if(x == rhs.x)            return y < rhs.y;        else            return x < rhs.x;    }} nodes[maxn];int main(){    int n;    while(~scanf("%d",&n))    {        int ans=0;///正方形个数        for(int i=0; i<n; ++i)            scanf("%d%d",&nodes[i].x,&nodes[i].y);        sort(nodes,nodes+n);        for(int i=0; i<n; ++i)            for(int j=i+1; j<n; ++j)            {                Node tmp;  ///tmp作为正方形的第3或4个点                ///特殊方法来判断第三个和第四个点的位置(自己画画图就懂了)                tmp.x=nodes[i].x+nodes[i].y-nodes[j].y;                tmp.y=nodes[i].y+nodes[j].x-nodes[i].x;                if(!binary_search(nodes,nodes+n,tmp)) continue;   ///二分查找是否存在这个点                tmp.x=nodes[j].x+nodes[i].y-nodes[j].y;                tmp.y=nodes[j].y+nodes[j].x-nodes[i].x;                if(!binary_search(nodes,nodes+n,tmp)) continue;                ++ans;            }        ///在两个点为一个边的时候判断了两次,所以结果应该除二        printf("%d\n",ans/2);    }    return 0;}
原创粉丝点击