Poj2002squares (number of squares composed of point sets)

Source: Internet
Author: User

Link

Two vertices can be enumerated, because the two outer vertices of a square can be obtained by known results. It is said that the following formula can be obtained based on the equi of a triangle. The mathematical scum will not prove it...

Known: (x1, Y1) (X2, Y2)

Then: X3 = X1 + (y1-y2) Y3 = Y1-(x1-x2)

X4 = x2 + (y1-y2) Y4 = Y2-(x1-x2)

Or

X3 = x1-(y1-y2) Y3 = Y1 + (x1-x2)

X4 = x2-(y1-y2) Y4 = y2 + (x1-x2)

Then we can do the hash or binary. Here we only use the hash to do it.

It should be regarded as a simple hash application for resolving conflicts and put it in an adjacent table.

Two vertices need to be enumerated twice to ensure that the squares at both locations are enumerated.

The final result must be divided by 4 because the enumeration is repeated.

 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set>10 using namespace std;11 #define N 101012 #define mod 9999113 #define LL long long14 #define INF 0xfffffff15 const double eps = 1e-8;16 const double pi = acos(-1.0);17 const double inf = ~0u>>2;18 struct point19 {20     int x,y;21     point(int x=0,int y=0):x(x),y(y){}22 }p[N],o[N];23 int next[N],head[mod],t;24 void insert(int i)25 {26     int key = (p[i].x*p[i].x+p[i].y*p[i].y)%mod;27     next[t] = head[key];28     o[t].x = p[i].x;29     o[t].y = p[i].y;30     head[key] = t++;31 }32 int find(point a)33 {34     int  key = (a.x*a.x+a.y*a.y)%mod;35     int i;36     for(i = head[key] ; i!= -1 ; i = next[i])37     {38         if(o[i].x==a.x&&o[i].y == a.y) return 1;39     }40     return 0;41 }42 bool cmp(point a,point b)43 {44     if(a.x==b.x)45     return a.y<b.y;46     return a.x<b.x;47 }48 int main()49 {50     int n,i,j;51     while(scanf("%d",&n)&&n)52     {53         memset(head,-1,sizeof(head));54         t = 0;55         for(i = 1; i <= n; i++)56         {57             scanf("%d%d",&p[i].x,&p[i].y);58             insert(i);59         }60         //sort(p+1,p+n+1,cmp);61         int ans = 0;62         for(i = 1; i <= n ;i++)63             for(j = 1 ; j <= n; j++)64             {65                 if(i==j) continue;66                 point p1,p2;67                 p1.x = p[i].x+(p[i].y-p[j].y);68                 p1.y = p[i].y-(p[i].x-p[j].x);69                 p2.x = p[j].x+(p[i].y-p[j].y);70                 p2.y = p[j].y-(p[i].x-p[j].x);71                 if(!find(p1)) continue;72                 if(!find(p2)) continue;73                 ans++;74             }75         printf("%d\n",ans/4);76     }77     return 0;
View code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.