题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6055
题意:
有n个整数点,问这些点能组成多少个正多边形。加以推导,我们可以知道,只有是正方形时,才能使得所有的端点为整数点。
思路:
枚举正方形的一条边,另一条边可以根据这条边推导求得。
已知a(x1, y1), b(x2, y2), 则
c(x1+(y2-y1), y1-(x2-x1))
d(x2+(y2-y1), y2-(x2-x1))
求出线段ab对应的其中一条边cd(还有另一条边)
代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 600;
struct Point
{
int x, y;
Point(){}
Point(int x, int y):x(x), y(y){}
bool operator<(const Point &rhs)const{
if(this->x<rhs.x){
return true;
}
else if(this->x==rhs.x){
return this->y > rhs.y;
}
else{
return false;
}
}
}A[maxn];
int main(){
int n;
while(cin>>n){
for(int i=0; i<n; ++i){
cin>>A[i].x>>A[i].y;
}
sort(A, A+n);
int cnt = 0;
for(int i=0; i<n-1; ++i){
for(int j=i+1; j<n; ++j){
int x1 = A[i].x+(A[j].y-A[i].y); int y1 = A[i].y-(A[j].x-A[i].x);
int x2 = A[j].x+(A[j].y-A[i].y); int y2 = A[j].y-(A[j].x-A[i].x);
Point a(x1, y1), b(x2, y2);
int pos1 = lower_bound(A, A+n, a) - A;
int pos2 = lower_bound(A, A+n, b) - A;
if(A[pos1].x==a.x&&A[pos1].y==a.y && A[pos2].x==b.x && A[pos2].y==b.y){
++cnt;
}
}
}
cout<<cnt/2<<endl;
}
return 0;
}