POJ2002 squares (two-dimensional point hash)

Source: Internet
Author: User

Topic Links:

http://poj.org/problem?id=2002


Test instructions

A given n points to determine how many squares this n points can make.


Analysis:

Violence enumerates the two endpoints of an edge, and then, according to the congruent triangles, can find the endpoints of the other two sides of the square, and then judge that the two two exist.

So the first thing to do is to hash all the dots, then the brute force enumeration, so that the four sides are counted once, so the last one is to divide by 4.


The code is as follows:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < cmath>using namespace Std;const int mod = 10007;const int maxn = 1010;struct nod{int x, y;};    inline int read ()//input plug-in {char ch=getchar ();    int x=0,f=1; while (ch> ' 9 ' | |        ch< ' 0 ') {if (ch== '-') f=-1;    Ch=getchar ();        } while (ch<= ' 9 ' &&ch>= ' 0 ') {x=x*10+ch-' 0 ';    Ch=getchar (); } return x*f;}    struct hashmap{nod A[MAXN];    int head[mod],next[maxn],cnt;        void Init () {cnt=0;        memset (head,-1,sizeof (head));    memset (Next,0,sizeof (next));        } bool Find (nod val) {int tmp = (val.x*val.x+val.y*val.y)%mod;        for (int i=head[tmp]; i!=-1; i=next[i]) if (A[I].X==VAL.X&AMP;&AMP;A[I].Y==VAL.Y) return true;    return false;        } void Add (nod val) {int tmp = (val.x*val.x+val.y*val.y)%mod;          for (int i=head[tmp]; i!=-1; I=next[i])  if (A[I].X==VAL.X&AMP;&AMP;A[I].Y==VAL.Y) return;        A[cnt]=val;        NEXT[CNT]=HEAD[TMP];    head[tmp]=cnt++;    }} H;nod P[maxn];int main () {int n;        while (~SCANF ("%d", &n)} {if (n==0) break;        H.init ();            for (int i=0; i<n; i++) {p[i].x=read ();            P[i].y=read ();        H.add (P[i]);        } int ans = 0;                for (int i=0, i<n; i++) {for (int j=i+1; j<n; J + +) {nod tmp1,tmp2;                tmp1.x = p[i].x-(P[I].Y-P[J].Y);                TMP1.Y = P[i].y + (p[i].x-p[j].x);                tmp2.x = p[j].x-(P[I].Y-P[J].Y);                TMP2.Y = P[j].y + (p[i].x-p[j].x);            if (H.find (TMP1) &&h.find (TMP2)) ans++; }} for (int i=0, i<n; i++) {for (int j=i+1; j<n; J + +) {No                D TMP1,TMP2;         tmp1.x = p[i].x + (P[I].Y-P[J].Y);       TMP1.Y = P[i].y-(p[i].x-p[j].x);                tmp2.x = p[j].x + (P[I].Y-P[J].Y);                TMP2.Y = P[j].y-(p[i].x-p[j].x);            if (H.find (TMP1) &&h.find (TMP2)) ans++;        }} ans >>= 2;    printf ("%d\n", ans); } return 0;}



POJ2002 squares (two-dimensional point hash)

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.