poj2002

已经正方形的两个点,那么其余两个点的可能坐标是:

已知: (x1,y1)  (x2,y2)

则:   x3=x1+(y1-y2)   y3= y1-(x1-x2)

x4=x2+(y1-y2)   y4= y2-(x1-x2)

x3=x1-(y1-y2)   y3= y1+(x1-x2)

x4=x2-(y1-y2)   y4= y2+(x1-x2)

将所有点散列到一个hash表里面,然后查找其余两个点是否存在。

采用的hash函数为平方散列:x1 * x1 + x2 * x2

AC代码:

/**
 *FILENAME: poj2002.c
 *AUTHOR: XIANG CHANG SHENG
 *CREATE ON: 2013-2-4
 */
#include <stdio.h>
#include <string.h>


/**
 *DESCRIPTION:
 Hash to sovle the problem
 */

#define MAX_NUMBER 1005
#define PRIME 1999
struct Star {
    int x;
    int y;
};

int head[PRIME];
int next[MAX_NUMBER];
struct Star star[MAX_NUMBER];

int hashCode(int order) {
    int hash_code = 0;
    hash_code = star[order].x * star[order].x + star[order].y * star[order].y;
    return hash_code % PRIME;
}

int find(struct Star point) {
    int hash_code = point.x * point.x + point.y * point.y;
    int j;
    hash_code = hash_code % PRIME;
    for (j = head[hash_code]; j != -1; j = next[j]) {
        if (point.x == star[j].x && point.y == star[j].y) {
            return 1;
        }
    }
    return 0;
}
int main() {
    freopen("input.txt", "r", stdin);
    int star_number, i, j, ans, hash_code;
    struct Star next_point_1, next_point_2;
    while (scanf("%d", &star_number) != EOF) {
        if (!star_number) {
            break;
        }
        memset(head, -1, sizeof(head));
        memset(next, -1, sizeof(next));
        ans = 0;
        for (i = 0; i < star_number; i++) {
            scanf("%d%d", &star[i].x, &star[i].y);
            hash_code = hashCode(i);
            next[i] = head[hash_code];
            head[hash_code] = i;
        }

        for (i = 0; i < star_number; i++) {
            for (j = i + 1; j < star_number; j++) {
                next_point_1.x = star[i].x + (star[i].y - star[j].y);
                next_point_1.y = star[i].y - (star[i].x - star[j].x);
                next_point_2.x = star[j].x + (star[i].y - star[j].y);
                next_point_2.y = star[j].y - (star[i].x - star[j].x);
                if (find(next_point_1) && find(next_point_2)) {
                    ans++;
                }
                next_point_1.x = star[i].x - (star[i].y - star[j].y);
                next_point_1.y = star[i].y + (star[i].x - star[j].x);
                next_point_2.x = star[j].x - (star[i].y - star[j].y);
                next_point_2.y = star[j].y + (star[i].x - star[j].x);
                if (find(next_point_1) && find(next_point_2)) {
                    ans++;
                }
            }
        }
        printf("%d\n", ans / 4);
    }
}

 


 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值