poj 2002 Squares

Squares
Time Limit: 3500MS Memory Limit: 65536K
Total Submissions: 15423 Accepted: 5845

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. 
正方形是4边形,边的长度相等且和邻边形成90度角。同样拥有绕着中心点旋转90°形状不变的特性。 他不是唯一有这个特性的多边形,一个正八边形也有这个特性。

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. 
所以,我们都知道正方形什么样子,不过我们能够找到夜空上的星星能够组成的所有正方形么?为了使问题变得更容易,我们将假定夜空在一个2维平面上,并且每个星星是由它的x和y坐标指定。

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.
输入有多个测试用例。每个测试用例第一行一个整数n(1<= N <= 1000),表示点的数目。接下来的n行,指定每个点的x和y坐标(2个整数)。你可以假设点是不同的,坐标的大小均小于20000,当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

这道题是跟着上道题写的,由于上道题参考了别人的hash写法,于是这道题也写的很像,那人写的十分全面。。。具体数学计算方法也在里面。
交了一次wa是因为没有在每一次读取n后给hash表清零。
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define MAX_NUM_OF_Point 1111
#define PRIME 1999


struct Point{
	int x;
	int y;
};

struct Hash{
	int x;
	int y;
	struct Hash* next;
};

struct Hash* hash_t[PRIME + 1];

int calculate(struct Point p){
	return (p.x * p.x + p.y * p.y) % PRIME;
}

void insert_p(struct Point p){
	int key = calculate(p);

	if (!hash_t[key]){
		struct Hash* temp;
		if ((temp = (struct Hash*)malloc(sizeof(struct Hash))) == NULL){
			return ;
		}
		temp->x = p.x;temp->y = p.y;
		//printf("temp->x = %d, temp->y = %d\n", temp->x, temp->y);
		temp->next = NULL;
		hash_t[key] = temp;
	}else{
		struct Hash* temp = hash_t[key];

		while(temp->next != NULL){
			temp = temp->next;
		}
		if ((temp->next = (struct Hash*)malloc(sizeof(struct Hash))) == NULL){
			return ;
		}
		temp->next->x = p.x;temp->next->y = p.y;
		//sprintf("temp->next->x = %d, temp->next->y = %d\n", temp->next->x, temp->next->y);
		temp->next->next = NULL;
	}
}
int find_p(struct Point p){
	int key = calculate(p);

	if (!hash_t[key]){
		return 0;
	}else{
		struct Hash* temp = hash_t[key];
		while(temp != NULL){
			if (temp->x == p.x && temp->y == p.y){
				return 1;
			}
			temp = temp -> next;
		}
	}
	return 0;
}

int main()
{
	int i, j, n, num_square;
	struct Point point[MAX_NUM_OF_Point];
	struct Point point1, point2;


	freopen("in.txt",  "r", stdin);
	//freopen("out.txt", "w", stdout);

	while(scanf("%d", &n) && (n != 0)){
        memset(hash_t, 0, sizeof(hash_t));
		for (i = 0;i < n;i ++){
			scanf("%d%d", &point[i].x, &point[i].y);
			//printf("%d %d\n", point[i].x, point[i].y);
			insert_p(point[i]);
		}
		num_square = 0;
		for (i = 0;i < n - 1;i ++){
			for (j = i+1;j < n;j ++){
				int a = point[j].x - point[i].x;
				int b = point[j].y - point[i].y;

				point1.x = point[i].x + b;
				point1.y = point[i].y - a;
				point2.x = point[j].x + b;
				point2.y = point[j].y - a;

				if(find_p(point1) && find_p(point2)){
					num_square ++;
				}

				point1.x = point[i].x - b;
				point1.y = point[i].y + a;
				point2.x = point[j].x - b;
				point2.y = point[j].y + a;

				if(find_p(point1) && find_p(point2)){
					num_square ++;
				}
			}
		}
		printf("%d\n", num_square/4);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值