题意:给定二维平面上不超过1000个点(点坐标的绝对值<=20000),问这些点能够构成多少个正方形。
思路:枚举两个顶点,可以确定另外两个顶点的位置,判断另外两个顶点是否存在于点集中。先用hash把点存储一下,然后判断。此处用了stl 的set。
因为多组,所以一开始要清空set,忘了这一步导致wa了多次。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
int n;
struct node{
int x,y;
};
node p[1005];
set<int> h;
int id(int x,int y){
return x*40001+y;
}
int main(){
while(scanf("%d",&n) && n){
int res=0;
h.clear();
for(int i = 0;i<n;i++){
scanf("%d %d",&p[i].x,&p[i].y);
h.insert(id(p[i].x,p[i].y));
}
for(int i = 0;i<n;i++){
//for(int j = i+1;j<n;j++){//这么枚举会出错,看了discuss知道的
for(int j = 0;j<n;j++){
if(i==j)
continue;
int x1 = p[i].x+(p[i].y-p[j].y);
int y1 = p[i].y+(p[j].x-p[i].x);
int x2 = p[j].x+(p[i].y-p[j].y);
int y2 = p[j].y+(p[j].x-p[i].x);
if(h.find(id(x1,y1))!=h.end() && h.find(id(x2,y2))!=h.end())
res++;
}
}
printf("%d\n",res>>2);
}
return 0;
}