poj 2002 Squares

Squares
Time Limit: 3500MS Memory Limit: 65536K
Total Submissions: 18822 Accepted: 7240

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.

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

提示

题意:

天空中有很多星星,那么我们假设天空是一个二维平面,并给出星星的坐标,问我们在这些星星上连线最多可以得到几个正方形。(由4个相同的点组成的是同一个正方形)

思路:

这题如果不知道有这样的公式,恐怕不好做吧:

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

已经知道公式就可以做了,先存点(当然用哈希表啊),之后用两个点去枚举,看哈希表中有没有,最后的答案要记得÷4,因为同一个正方形被枚举了4次。

从四个点枚举,降到两个点枚举这就比较快了。

示例程序

Source Code

Problem: 2002		Code Length: 1277B
Memory: 12144K		Time: 1454MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
struct
{
    int x,y,bug;
}hash[10000][100];
struct
{
    int x,y;
}a[1000];
void insert(int x,int y)
{
    int key,i=0;
    key=(x*x+y*y)%10000;			//我以(x*x+y*y)modP作为地址,这样可以保证key的值为正
    while(hash[key][i].bug==1)
    {
        i++;
    }
    hash[key][i].bug=1;
    hash[key][i].x=x;
    hash[key][i].y=y;
}
int judge(int x,int y)
{
    int key,i;
    key=(x*x+y*y)%10000;
    for(i=0;hash[key][i].bug==1;i++)
    {
        if(hash[key][i].x==x&&hash[key][i].y==y)
        {
            return 1;
        }
    }
    return 0;
}
int main()
{
    int n,i,i1,num,x3,y3,x4,y4;
    scanf("%d",&n);
    while(n!=0)
    {
        num=0;
        memset(hash,0,sizeof(hash));
        for(i=0;n>i;i++)
        {
            scanf("%d %d",&a[i].x,&a[i].y);
            insert(a[i].x,a[i].y);		//打表
        }
        for(i=0;n>i;i++)
        {
            for(i1=0;n>i1;i1++)
            {
                x3=a[i].x+(a[i].y-a[i1].y);			//套公式
                y3=a[i].y-(a[i].x-a[i1].x);
                x4=a[i1].x+(a[i].y-a[i1].y);
                y4=a[i1].y-(a[i].x-a[i1].x);
                if(i!=i1&&judge(x3,y3)==1&&judge(x4,y4)==1)		//保证两个点不重复且另外两个点都要存在
                {
                    num++;
                }
            }
        }
        printf("%d\n",num/4);				//答案要÷4
        scanf("%d",&n);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值