2017 杭电多校联赛第二场 1011 Regular polygon(多个点求正方形个数)POJ 2002

Regular polygon

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make.
 

Input
The input file consists of several test cases. Each case the first line is a numbers N (N <= 500). The next N lines ,each line contain two number Xi and Yi(-100 <= xi,yi <= 100), means the points’ position.(the data assures no two points share the same position.)
 

Output
For each case, output a number means how many different regular polygon these points can make.
 

Sample Input
  
  
4 0 0 0 1 1 0 1 1 6 0 0 0 1 1 0 1 1 2 0 2 1
 

Sample Output
  
  
1 2
跟POJ 2002一模一样。。。

则p1 p2 p3 p4组成一个正方形,设p1 = (x1,y1), p2 = (x2, y2),根据向量的旋转公式可以求出p3, p4的坐标为

p3 = (y1 - y2 + x1, x2 - x1 + y1)

p4 = (y1 - y2 + x2, x2 - x1 + y2)

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 1010;
const int H = 10007;
int ptx[N], pty[N];

struct Node
{
    int x;
    int y;
    int next;
};
Node node[N];
int cur;
int n;
long ans;
int hashTable[H];

void initHash()
{
    for (int i = 0; i < H; ++i) hashTable[i] = -1;
    cur = 0;
    ans = 0;
}

void insertHash(int x, int y)
{
    int h = (x * x + y * y) % H;
    node[cur].x = x;
    node[cur].y = y;
    node[cur].next = hashTable[h];
    hashTable[h] = cur;
    ++cur;
}

bool searchHash(int x, int y)
{
    int h = (x * x + y * y) % H;
    int next;
    next = hashTable[h];
    while (next != -1)
    {
        if (x == node[next].x && y == node[next].y) return true;
        next = node[next].next;
    }
    return false;
}

int main()
{
    while (scanf("%d", &n) != EOF && n)
    {
        initHash();
        for (int i = 0; i < n; ++i) 
        {
            scanf("%d%d", &ptx[i], &pty[i]);
            insertHash(ptx[i], pty[i]);
        }
        for (int i = 0; i < n; ++i)
        {
            for (int j = i + 1; j < n; ++j)
            {
                int x1 = ptx[i] - (pty[i] - pty[j]);
                int y1 = pty[i] + (ptx[i] - ptx[j]);
                int x2 = ptx[j] - (pty[i] - pty[j]);
                int y2 = pty[j] + (ptx[i] - ptx[j]);
                if (searchHash(x1, y1) && searchHash(x2, y2)) ++ans;
            }
        }
        for (int i = 0; i < n; ++i)
        {
            for (int j = i + 1; j < n; ++j)
            {
                int x1 = ptx[i] + (pty[i] - pty[j]);
                int y1 = pty[i] - (ptx[i] - ptx[j]);
                int x2 = ptx[j] + (pty[i] - pty[j]);
                int y2 = pty[j] - (ptx[i] - ptx[j]);
                if (searchHash(x1, y1) && searchHash(x2, y2)) ++ans;
            }
        }
        ans >>= 2;
        printf("%ld\n", ans);
    }
    return 0;
}


 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值