HDU 2017多校联赛-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

因为给的点坐标全是整数,那么就是计算正四边形的个数,只有正方形才可以使所有的点为整数点。那么由两个点便可以得到可能满足正方形情况的两个点,判断这两个点是否存在就好了,注意在计算的过程中每个正方形有四条边所以会被统计四遍,最终结果应是 ans/4。
PS:给的数据点的范围是[-100,100],因此对点的坐标+100进行处理。

已知: (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)

一般的在横坐标 或 纵坐标都相同的就不考虑了,(x1-x2)或(y1-y2)为0,一定满足
我们只考虑在如下情况的正方形

图一 当(x1, y1) (x2,y2)在如下位置时 他能找到的(x3,y3)(x4,y4) 有实线和虚线 围成的两种方案

HDU 2017多校联赛-Regular polygon_第1张图片

同理,还有另外一种2种排法

HDU 2017多校联赛-Regular polygon_第2张图片

因此,确定2个点(一条边) 都能找到与之对应的 2个四边形,那么4条边 就有8个 但4个是重复的 所以 /4

代码:

#include 
#include
#include
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;iscanf("%d %d",&x[i],&y[i]);
            x[i]+=100,y[i]+=100;
            visit[x[i]][y[i]]=1;
        }
        for(int i=0;ifor(int j=i+1;jint dx=x[j]-x[i];
                int dy=y[j]-y[i];
                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;
}

你可能感兴趣的:(ACM_数学,ACM_计算几何)