HDOJ 6055-Regular polygon

来源:互联网 发布:nemo软件好用吗 编辑:程序博客网 时间:2024/06/08 13:14

Regular polygon

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

Total Submission(s): 537    Accepted Submission(s): 207


题目链接:点击打开链接



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个整数点,问能构成多少个不同的正多边形。

题解:容易得知只有正四边形可以使得所有的顶点为整数点。(具体证明可参考杨景钦在2017的国家队论文)所以正解即求出所有的正四边形个数。

枚举2个点,然后暴力判断另外2个点的位置是否存在。

复杂度 N*N*logN。

分析:已知一个正方形中任意两点 (x1,y1)和(x2,y2) 
则:x3=x1+(y1-y2) y3= y1-(x1-x2) 
       x4=x2+(y1-y2) y4= y2-(x1-x2) 
或 
       x3=x1-(y1-y2) y3= y1+(x1-x2) 
       x4=x2-(y1-y2) y4= y2+(x1-x2)

下面给出两个代码,代码一因为只用了一般的公式,所以最后结果/2,第二个代码因为用了全部的公式,最后结果要/4.

最后有些小伙伴可能不知道由两点 推其余两点的坐标怎么得到的,在这里稍微补一点数学知识,好了,不多说,看图:


代码一:

#include <cstdio>#include <iostream>#include <algorithm>#include <map>using namespace std;struct point{    double x,y;    bool operator < (const point &a)const    {        if(x == a.x)            return y<a.y;        else            return x<a.x;    }};point p[505];int main(){    int n;    while(~scanf("%d",&n))    {        map<point,int> m;        for(int i=0;i<n;i++)        {            scanf("%lf %lf",&p[i].x,&p[i].y);            m[p[i]]++;        }        int sum = 0;        for(int i=0;i<n;i++)        {            for(int j=i+1;j<n;j++)            {                point t,t1;                t.x = (p[i].x+p[j].x)/2 + (p[i].y - (p[i].y+p[j].y)/2);                t.y = (p[i].y+p[j].y)/2-(p[i].x - (p[i].x+p[j].x)/2);                if(m[t]==0) continue;                t1.x = (p[i].x+p[j].x)/2 - (p[i].y - (p[i].y+p[j].y)/2);                t1.y = (p[i].y+p[j].y)/2 + (p[i].x - (p[i].x+p[j].x)/2);                if(m[t1]==0) continue;                sum++;            }        }        printf("%d\n",sum/2);    }    return 0;}


代码 二:
#include <iostream>#include<cstdio>#include<cstring>using namespace std; int x[507],y[507]; int visit[207][207]; int main() {     int n,ans;     while(~scanf("%d",&n))    {         ans=0;         memset(visit,0,sizeof(visit));         for(int i=0;i<n;i++)        {             scanf("%d %d",&x[i],&y[i]);             x[i]+=100,y[i]+=100;             visit[x[i]][y[i]]=1;         }         for(int i=0;i<n;i++)        {             for(int j=i+1;j<n;j++)             {                 int dx=x[i]-x[j];                 int dy=y[i]-y[j];                 //判断(x3, y3) , (x4, y4)是否满足条件                if(x[i]+dy>=0&&x[i]+dy<=200&&y[i]-dx>=0&&y[i]-dx<=200&&                    x[j]+dy>=0&&x[j]+dy<=200&& y[j]-dx>=0&&y[j]-dx<=200&&                    visit[x[i]+dy][y[i]-dx]&&visit[x[j]+dy][y[j]-dx])                         ans++;                 if(x[i]-dy>=0&&x[i]-dy<=200&&y[i]+dx>=0&&y[i]+dx<=200&&x[j]-dy>=0&&                    x[j]-dy<=200&&y[j]+dx>=0&&y[j]+dx<=200&&                    visit[x[i]-dy][y[i]+dx]&&visit[x[j]-dy][y[j]+dx])                         ans++;             }         }         printf("%d\n",ans/4); } return 0; }



 
原创粉丝点击