#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);
}
#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用了拉链法解冲突, 数据也弱.