POJ - 2002:Squares

POJ - 2002:Squares

来源:

标签:

参考资料:

相似题目:

题目

A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates.

输入

The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.

输出

For each test case, print on a line the number of squares one can form from the given stars.

输入样例

4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0

输出样例

1
6
1

解题思路

参考代码

#include<stdio.h>
#include<algorithm>
#define MAXN 1000+5
using namespace std;
struct info{
    int x;
    int y;
};
bool cmp(info A, info B){
    if(A.x<B.x || A.x==B.x && A.y<B.y)
        return true;
    return false;
}
info pot[MAXN];
int n;

int cmp1(info A, info B){
    if(A.x<B.x || A.x==B.x && A.y<B.y)
        return -1;
    else if(A.x==B.x && A.y==B.y){
        return 0;
    }
    return 1;
}

int binSearch(info des){
    int start=0, end=n-1;
    int mid;
    while(start<=end){
        mid=(start+end)/2;
        if(cmp1(pot[mid],des)==-1){
            start=mid+1;
        }else if(cmp1(pot[mid],des)==1){
            end=mid-1;
        }else{
            break;
        }
    }
    if(pot[mid].x==des.x && pot[mid].y==des.y)
        return 1;
    return 0;
}
int main(){
    while(scanf("%d",&n)==1 && n){
        int i,j;
        for(i=0;i<n;i++){
            scanf("%d%d",&pot[i].x,&pot[i].y);
        }
        sort(pot,pot+n,cmp);
        double pos1x,pos1y,pos2x,pos2y;
        double midx,midy;
        int flag,cnt=0;
        for(i=0;i<n;i++){
            for(j=i+1;j<n;j++){
                flag=0;
                midx=(pot[i].x+pot[j].x)/2.0;
                midy=(pot[i].y+pot[j].y)/2.0;
                pos1x=midx+midy-pot[j].y;
                pos1y=midy-midx+pot[j].x;
                pos2x=midx+midy-pot[i].y;
                pos2y=midy-midx+pot[i].x;

                if(pos1x==int(pos1x) && pos1y==int(pos1y) && pos2x==int(pos2x) && pos2y==int(pos2y)){
                    info pot1,pot2;
                    pot1.x=pos1x;
                    pot1.y=pos1y;
                    pot2.x=pos2x;
                    pot2.y=pos2y;
                    flag+=binSearch(pot1)+binSearch(pot2);
                    if(flag==2) cnt++;
                }
            }
        }
        printf("%d\n",cnt/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wingrez/article/details/80532059
POJ