Squares(枚举+set 查找)
http://poj.org/problem?id=2002
题意:给出n组坐标,判断这些坐标能组成的正方形的个数。
思路:参考某大神的想法,先枚举两个点,然后利用公式表示出另外两个点,判断这两个点是否在这n组坐标中,其中查找另两个坐标用的set容器。
已知 (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);
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <set> 5 const int N=100000; 6 const int maxn=1010; 7 8 using namespace std; 9 struct node 10 { 11 long long x; 12 long long y; 13 } p[maxn]; 14 int main() 15 { 16 int n; 17 while(~scanf("%d",&n)&&n) 18 { 19 int ans = 0; 20 set<long long>tt; 21 for (int i = 0; i < n; i ++) 22 { 23 scanf("%lld %lld",&p[i].x,&p[i].y); 24 tt.insert(p[i].x*N+p[i].y);//将坐标转化成一个数,坐标不同,则这个数就不同 25 } 26 for (int i = 0; i < n; i ++) 27 { 28 int x1 = p[i].x; 29 int y1 = p[i].y; 30 for (int j = i+1; j < n; j ++) 31 { 32 int x2 = p[j].x; 33 int y2 = p[j].y; 34 int x3 = x1+(y1-y2); 35 int y3 = y1-(x1-x2); 36 int x4 = x2+(y1-y2); 37 int y4 = y2-(x1-x2); 38 if (tt.count(x3*N+y3) && (tt.count(x4*N+y4)))// 查找坐标是否存在 39 ++ans; 40 x3 = x1-(y1-y2); 41 y3 = y1+(x1-x2); 42 x4 = x2-(y1-y2); 43 y4 = y2+(x1-x2); 44 if (tt.count(x3*N+y3) && (tt.count(x4*N+y4))) 45 ++ans; 46 } 47 } 48 printf("%d\n",ans/4); 49 } 50 return 0; 51 }