poj_2002 Squares(哈希)

Squares
Time Limit: 3500MS   Memory Limit: 65536K
Total Submissions: 19231   Accepted: 7429

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
 
 
题目要求找直角坐标系中所给点一共能组成多少个正方形。
在纸上模拟一下,比较容易发现已知两点(x1,y1),(x2,y2)能求出正方形中的另外两点(x3,y3),(x4,y4)
即 x3 = x1 - (y2 - y1),y3 = y1 + (x2 - x1),x4 = x2 - (y2 - y1),y4 = y2 + (x2 - x1)。
或 x3 = x1 + (y2 - y1),y3 = y1 - (x2 - x1),x4 = x2 + (y2 - y1),y4 = y2 - (x2 - x1)。
然后就可以用哈希表保存所有的点,然后枚举两点去找另外两点。
 
 
 
 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

struct Node
{
    int x, y;
    int next;
}node[maxn];

int n;
int cot;
int hashTable[maxn];

void initHash()
{
    cot = 0;
    memset(hashTable, -1, sizeof(hashTable));
}

int getHash(int x, int y)
{
    return ((x * x) % mod + (y * y) % mod) % mod;
}

void insertHash(int x, int y)
{
    int key = getHash(x, y);
    node[cot].x = x, node[cot].y = y;
    node[cot].next = hashTable[key];
    hashTable[key] = cot++;
}

bool searchHash(int x, int y)
{
    int key = getHash(x, y);
    int next = hashTable[key];
    while(next != -1)
    {
        if(x == node[next].x && y == node[next].y) return true;
        next = node[next].next;
    }
    return false;
}

struct Point
{
    int x, y;
}p[1010];

int main()
{
    while(scanf("%d", &n) && n)
    {
        int ans = 0;
        initHash();
        for(int i = 1; i <= n; i++) scanf("%d%d", &p[i].x, &p[i].y), insertHash(p[i].x, p[i].y);
        for(int i = 1; i <= n; i++)
        {
            for(int j = i+1; j <= n; j++)
            {
                int x1 = p[i].x + (p[j].y - p[i].y), y1 = p[i].y - (p[j].x - p[i].x);
                int x2 = p[j].x + (p[j].y - p[i].y), y2 = p[j].y - (p[j].x - p[i].x);
                int x3 = p[i].x - (p[j].y - p[i].y), y3 = p[i].y + (p[j].x - p[i].x);
                int x4 = p[j].x - (p[j].y - p[i].y), y4 = p[j].y + (p[j].x - p[i].x);

                if((searchHash(x1, y1) && searchHash(x2, y2))) ans++;
                if((searchHash(x3, y3) && searchHash(x4, y4))) ans++;
            }
        }
        printf("%d\n", ans / 4);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/christry_stool/article/details/53010610