poj-3432

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

struct pointInfo {
    int x;
    int y;
    int havl;
    struct pointInfo * next;
};

struct point {
    int x;
    int y;
};

typedef struct pointInfo pointInfo;
typedef struct point point;

#define esp 1e-10

const int BIG_PRIME = 999999;
const int MAX_ADD = 10000;

point pointArray[2001];
pointInfo *pointHashArray[999999];

void fillInHash(int x, int y) {
    int x1 = x + MAX_ADD;
    int y1 = y + MAX_ADD;
    int havl = (x1*x1 + y1*y1) % BIG_PRIME;
    int fillPos = havl;
    pointInfo * prev = pointHashArray[fillPos];
    pointHashArray[fillPos] = (pointInfo *)malloc(sizeof(pointInfo));
    pointHashArray[fillPos]->x = x;
    pointHashArray[fillPos]->y = y;
    pointHashArray[fillPos]->havl = havl;
    pointHashArray[fillPos]->next = prev;
}

int IfPointExist(int x, int y) {   // -1 no exist, else the pos in hashArray.
    int x1 = x + MAX_ADD;
    int y1 = y + MAX_ADD;
    int havl = (x1*x1 + y1*y1) % BIG_PRIME;
    int Pos = havl;
    pointInfo *search = pointHashArray[Pos];
    while(search) {
        if (search->x == x &&
            search->y == y) {
            return 1;
        }
        search = search->next;
    }
    return -1;
}

void getSquareNum(int pointNum) {
    int squareNum = 0;
    for (int i = 0; i < pointNum-1; i++) {
        for (int j = i+1; j < pointNum; j++) {
            double x1 = pointArray[i].x;
            double x2 = pointArray[j].x;
            double y1 = pointArray[i].y;
            double y2 = pointArray[j].y;

            double x3 = (x1+x2+y2-y1)/2;
            double y3 = (y1+y2+x1-x2)/2;

            double x4 = (x1+x2-y2+y1)/2;
            double y4 = (y1+y2-x1+x2)/2;

            double len1=((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1))*2;
            double len2=((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
            if ((int)x3 != x3 ||
                (int)x4 != x4 ||
                (int)y3 != y3 ||
                (int)y4 != y4) {
                continue;
            }

            if(abs(len1-len2) > esp)
                continue;
            int Pos3 = IfPointExist(x3, y3);
            int Pos4 = IfPointExist(x4, y4);
            if (Pos3 != -1 && Pos4 != -1) {
                // printf("%f %f %f %f\n", x1, y1, x2, y2);
                // printf("%f %f %f %f\n", x3, y3, x4, y4);
                squareNum++;
            }
        }
    }
    printf("%d\n", squareNum/2);
    // printf("%d %d\n", squareNum, squareNum/2);
}

int main() {
    int pointNum = 0;
    while(scanf("%d", &pointNum) > 0) {
        if (pointNum == 0) {
            return 0;
        }
        memset(pointArray, 0 , sizeof(pointArray));
        memset(pointHashArray, 0 , sizeof(pointHashArray));
        for (int i = 0; i < pointNum; i++) {
            scanf("%d %d", &(pointArray[i].x), &(pointArray[i].y));
            fillInHash(pointArray[i].x, pointArray[i].y);
        }
        getSquareNum(pointNum);
    }

}

c++ 4144K 188MS。

和2002一样,只不过这次hash用了拉链法解冲突, 数据也弱.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值