HDU-6055 Regular polygon
去年多校赛的一道题目,写的时候一直WA,今年回过头来看的时候才发现是自己加了太多没有必要的判断所导致的。。
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
题目的意思大致是发给你好多好多个坐标点,让你判断这些点一共可以几个正方形。
我们可以画一个处在坐标系中的正方形把它的坐标表示为a(x1,y1),b(x2,y2),c(x3,y3),d(x4,y4),图型添加了到坐标系的辅助线之后通过几何知识,发现我们在已知正方形下面两点的坐标的情况下可以表示出上面的两点。如:
x3 = y1 - y2 + x1
y3 = x2 - x1 + y1
x4 = y1 - y2 + x2
y4 = x2 - x1 + y2
当点a位于点b的左上方时等式成立。
所以我们就遍历每一条边,首先判断两点是不是满足ab条件,然后判断该两点的c,d两点是不是存在于输入之中。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int pf[210][210]; //判断点是否存在
int zx[510],zy[510]; //zx储存点的x坐标,zy储存点的y坐标
bool right_down(int r,int l) //用于判断点a是否在点b的左上方
{
if(zx[r]<zx[l]&&zy[r]>=zy[l])return true; //或者两点一样高也是成立的
else return false;
}
int main()
{
int n;
while(cin>>n)
{
int ans=0;
memset(zx,0,sizeof(zx));
memset(zy,0,sizeof(zy));
memset(pf,0,sizeof(pf));
for(int i=1;i<=n;i++)
{
int x,y;
cin>>x>>y;
x+=100; //为了让坐标都是正整数所以+100
y+=100;
pf[x][y]=i; //表示该点坐标存在
zx[i]=x;
zy[i]=y;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(!right_down(i,j)) continue;
int x1,y1,x2,y2;
x1=zy[i]-zy[j]+zx[i];
y1=zx[j]-zx[i]+zy[i];
x2=zy[i]-zy[j]+zx[j];
if(x1<0||x1>200||y1<0||y1>200)continue;
if(x2<0||x2>200||y2<0||y2>200)continue;
if(pf[x1][y1]==0||pf[x2][y2]==0) continue;
ans++;
}
}
cout<<ans<<endl;
}
}