Squares(POJ--2002

Description

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. 

Input

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.

Output

For each test case, print on a line the number of squares one can form from the given stars.
题意:多组输入,每组输入n表示有n个点,求这n个点总共能构成几个正方形。
思路:根据数学知识可知,只要知道任意两点就可以根据公式求出其他两点,
            公式:已知: (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。


Sample Input

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

Sample Output

1
6
1

Source

<span style="font-size:18px;">#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 2000
#define H 1999
using namespace std;
//int x[N],y[N];
typedef struct node
{
    int x,y,next;              //next是为了防止存储发生冲突,当发生冲突时就另外再开辟新的空间去存储新的一点的信息,next是来记录新空间的位置的
} Node;
Node node[N],nade[N];
int n,cnt,hashtable[H];
long sum;
bool cmp(Node a,Node b) 
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
void init()                  //初始化,让没有出现过的点全部都等于-1
{
    for(int i=0; i<H; i++)
        hashtable[i]=-1;
    cnt=0;
    sum=0;
}
void insertt(int x,int y)
{
    int h=(x*x+y*y)%H;            //为了减少冲突,用平方取余去存储
    node[cnt].x=x;
    node[cnt].y=y;
    node[cnt].next=hashtable[h];         //相当于逆序建表,如果发生冲突了,就在原来出现的位置插入一个新空间
    hashtable[h]=cnt;
    cnt++;
}
int serch(int x,int y)
{
    int h=(x*x+y*y)%H;
    int next;
    next=hashtable[h];
    while(next!=-1)
    {
        if(node[next].x==x&&node[next].y==y)         //如果已经找到的话就返回真值
            return 1;
        next=node[next].next;                                      //如果当前不是要找的点就接着往下找
    }
    return 0;
}
int main()
{
    //freopen("oo.text","r",stdin);
    int n,x3,x4,y3,y4,i,j;
    while(~scanf("%d",&n)&&n)
    {
        init();
        for(i=0; i<n; i++)
        {
            scanf("%d %d",&nade[i].x,&nade[i].y);
            insertt(nade[i].x,nade[i].y);
        }
        sort(nade,nade+n,cmp);             //要记得给所有的点排序,如果不排序那么只枚举一遍最后结果除以2是不对的,可能会漏情况,如果不排序,就要用两个公式枚举两遍,然后结果除以4
        for(i=0; i<n; i++)
        {
            for( j=i+1; j<n; j++)
            {
                x3=nade[i].x+(nade[j].y-nade[i].y);
                y3=nade[i].y+(nade[i].x-nade[j].x);
                x4=nade[j].x+(nade[j].y-nade[i].y);
                y4=nade[j].y+(nade[i].x-nade[j].x);
                if(serch(x3,y3)&&serch(x4,y4))
                    sum++;
            }
        }
        sum>>=1;
        printf("%ld\n",sum);
    }
    return 0;
}</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值