POJ_2002Squares解题报告

Squares
Time Limit: 3500MS Memory Limit: 65536K
Total Submissions: 11181 Accepted: 4064

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
题目连接:http://poj.org/problem?id=2002
算法类型:Hash
解题思路:先把每个点所对应的Hash值储存在对应的容器里,在依次枚举两个点,算出其所对应的另外四个点,分别在其对应的容器中依次查找,如果两个点都找到,总数加一,最后把总数除以四。
算法实现:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<vector>
using namespace std;
#define MAX 9997
struct Squares
{
	int x;
	int y;
}squ[100002];
int Hash(Squares squ) //hash 函数;
{
	int s;
	s=squ.x*squ.x+squ.y*squ.y+squ.y;
	return s%MAX;
}
int  main()
{
	int N;
	while(scanf("%d",&N)!=EOF)
	{
		if(N==0)
		break;
		memset(squ,0,100002);
		vector<Squares> v[MAX];  //系统容器;
		memset(v,0,MAX);
		int sum=0;
		int i,J,JJ,P,PP;
		int hash,hash1,hash2,hash3,hash4;
		int X1,Y1,X2,Y2,X3,Y3,X4,Y4;
		for(i=0;i<N;i++)
		{
			scanf("%d %d",&squ[i].x,&squ[i].y);
			hash=Hash(squ[i]);
			v[hash].push_back(squ[i]);  //按照hash值储存在对应的容器中;
		}
		for(int q=0;q<N;q++)
		{
			for(int w=q+1;w<N;w++)
			{
				J=0;
				JJ=0;
				P=0;
				PP=0;
				X1=squ[q].x+squ[w].y-squ[q].y;  //  算出对应的点;
				Y1=squ[q].y-squ[w].x+squ[q].x;
				X2=squ[w].x+squ[w].y-squ[q].y;
				Y2=squ[w].y-squ[w].x+squ[q].x;
				X3=squ[q].x-squ[w].y+squ[q].y;
				Y3=squ[q].y+squ[w].x-squ[q].x;
				X4=squ[w].x-squ[w].y+squ[q].y;
				Y4=squ[w].y+squ[w].x-squ[q].x;
				hash1=X1*X1+Y1*Y1+Y1;
				hash1=hash1%MAX;
				hash2=X2*X2+Y2*Y2+Y2;
				hash2=hash2%MAX;
			    hash3=X3*X3+Y3*Y3+Y3;
				hash3=hash3%MAX;
				hash4=X4*X4+Y4*Y4+Y4;
				hash4=hash4%MAX;
				for(int e=0;e<v[hash1].size();e++)  //分别在其对应的hash值容器中查找;
				{
					if(v[hash1][e].x==X1 && v[hash1][e].y==Y1)
					{
							J=1;
					}
				}
				for(int r=0;r<v[hash2].size();r++)
				{
					if(v[hash2][r].x==X2 && v[hash2][r].y==Y2)
					{
						JJ=1;
					}
				}
		    if(J==1 && JJ==1)
				sum++;  //二个点都找到总数加一;
			for(int m=0;m<v[hash3].size();m++)
			{
				if(v[hash3][m].x==X3 && v[hash3][m].y==Y3)
					{
							P=1;
					}
			}
			for(int t=0;t<v[hash4].size();t++)
			{
				if(v[hash4][t].x==X4 && v[hash4][t].y==Y4)
					{
							PP=1;
					}
			}
			if(P==1 && PP==1)
				sum++;
			}
		}
		printf("%d\n",sum/4);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值