Poj 2022 squares

Source: Internet
Author: User

Algorithm:

1. enumerate any two vertices and determine whether the coordinates of the other two points exist based on the formula.

2. Search Algorithm. You can use hash or binary search.

View code

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>using namespace std;struct Point{         int x,y;  bool operator < ( const Point &A) const  {    if( x != A.x )    return x < A.x;    return y < A.y;      }}tmp[1010];struct node{  Point p;  int next;}nt[201000];int head[201000],size;int find(Point A){   int key = abs(A.x+A.y);   for( int e = head[key]; e != -1; e = nt[e].next)   {      if( nt[e].p.x == A.x && nt[e].p.y == A.y )          return 1;        }   return 0;   }int main( ){  int N;  while( scanf("%d", &N), N)  {     memset(head, -1,sizeof(head));     size = 0;     for( int i = 1; i <= N; i++)     {        scanf("%d%d",&tmp[i].x, &tmp[i].y);        int key = abs(tmp[i].x+tmp[i].y);        nt[size].p = tmp[i];        nt[size].next = head[key];        head[key] = size++;     }     sort( tmp + 1, tmp + N + 1);     int num = 0;     for( int i = 1; i <= N; i++)     {        for( int j = i + 1; j <= N; j++)        {            int a1 = tmp[i].x;            int a2 = tmp[i].y;            int b1 = tmp[j].x;            int b2 = tmp[j].y;            Point A,B;            A.x = a1 + a2 - b2;            A.y = a2 + b1 - a1;            B.x = b1 + a2 - b2;            B.y = b2 + b1 - a1;            if( find(A) && find(B) )                num++;        }              }     printf("%d\n",num / 2);  }        }

 

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.