标签:style blog http io for ar 2014 amp log
给出n个点,问能组成多少个正方形?
题解:
先把每个点hash
然后枚举两点(即枚举正方形的一条边),然后通过三角形全等,可以推出正方形的另外两点,在hash表里查找这两点看是存在,存在则 Cnt +1。
最后 answer = Cnt/4 //因为同一正方形都统计了4次。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN = 1005; const int MOD = 10007; struct node { int x, y; node(int x=0, int y=0):x(x),y(y) {} }; node P[MAXN]; int n; int head[MOD], next[MAXN], v[MAXN], sz; void init() { memset(head, -1, sizeof head ); sz = 0; } void addedge(int a, int b) { next[sz] = head[a]; head[a] = sz; v[sz] = b; sz++; } int hash(node v) { return (v.x*v.x + v.y*v.y) % MOD; } bool find(int x, int y) { int h = hash(node(x,y)); for(int j=head[h]; ~j; j = next[j]) { int& t = v[j]; if(P[t].x==x && P[t].y==y) return true; } return false; } int main() { while(~scanf("%d", &n)&&n) { init(); for(int i=0; i<n; ++i) { scanf("%d%d", &P[i].x, &P[i].y); addedge(hash(P[i]), i); } int ans = 0; for(int i=0; i<n; ++i) for(int j=i+1; j<n; ++j) { int xx = P[j].x - P[i].x; int yy = P[j].y - P[i].y; int x3 = P[i].x + yy; int y3 = P[i].y - xx; int x4 = P[j].x + yy; int y4 = P[j].y - xx; if(find(x3,y3) && find(x4,y4)) ans++; x3 = P[i].x - yy; y3 = P[i].y + xx; x4 = P[j].x - yy; y4 = P[j].y + xx; if(find(x3,y3) && find(x4,y4)) ans++; } printf("%d\n", ans/4); } return 0; }
标签:style blog http io for ar 2014 amp log
原文地址:http://blog.csdn.net/yew1eb/article/details/38752695