POJ 2002 Squares (two minutes)

Source: Internet
Author: User

Squares
Time Limit: 3500MS Memory Limit: 65536K
Total Submissions: 17423 Accepted: 6614

Description

A square is a 4-sided polygon whose sides has equal length and adjacent sides form 90-degree angles. It is also a polygon such the degrees gives the same polygon of It centre by. It isn't the only polygon with the latter property, however, as a regular octagon also have this property.

So we are know what's a square looks like, but can we find all possible squares that can is formed from a set of stars in a Night sky? To make the problem easier, we'll assume that the night sky was a 2-dimensional plane, and each star was specified by its X and Y coordinates.

Input

The input consists of a number of test cases. Each test case is starts with the integer n (1 <= n <=) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (both integers) of each point. Assume that the points is distinct and the magnitudes of the coordinates is less than 20000. The input is terminated when n = 0.

Output

For each test case, print on a line the number of squares one can form from the given stars.

Sample Input

41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20

Sample Output

161


Give a flat scatter set to determine how many squares can be formed. Although there are 3.5 seconds, the four-layer cycle of violence will definitely be timed out. So there is a way of thinking: First point sorting, double-loop enumeration before (N-2) points, in order to prevent repeated judgment, the second loop in the J to start from the i+1, two points after the search (N-J) point in the presence of a can and s[i],s[j] form the point of the square, so the second layer loop end condition is j< =n-2, the remaining 2 points are used to find the range of binary lookups is [j+1,n].

Known 2 points, write the coordinates that can form a square with these 2 points, such as (do not use double when calculating coordinates, otherwise it is easy to tle or WA)




#include <cstdio> #include <iostream> #include <algorithm>using namespace std;const int maxn=1e5+20;    int N;long long ans;struct star{int x, y;        Star () {} star (int x,int y) {this->x=x;    this->y=y;        } bool operator< (const star& N) Const {if (this->x==n.x) return this->y<n.y;    Return this->x<n.x;        }}s[maxn];int searchh (int l,int R,star N) {while (l<=r) {int mid= (L+R)/2;        if (S[MID].X==N.X&AMP;&AMP;S[MID].Y==N.Y) return 1;        if (s[mid]<n) l=mid+1;    else r=mid-1; } return 0;} int main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge while (scanf ("%d", &n)!=eof&amp        ; &n) {ans=0;        for (int i=1;i<=n;i++) scanf ("%d%d", &s[i].x,&s[i].y);        Sort (s+1,s+n+1);    for (int i=1;i<=n-3;i++) {for (int j=i+1;j<=n-2;j++) {            int xxx=s[j].x-s[i].x;                int yyy=s[j].y-s[i].y;                int sx1=s[i].x+yyy;                int sy1=s[i].y-xxx;                int sx2=s[j].x+yyy;                int sy2=s[j].y-xxx;                if (Searchh (J+1,n,star (sx1,sy1)) &&searchh (J+1,n,star (sx2,sy2))) ans++;                int sx3=s[i].x-yyy;                int sy3=s[i].y+xxx;                int sx4=s[j].x-yyy;                int sy4=s[j].y+xxx;            if (Searchh (J+1,n,star (sx3,sy3)) &&searchh (J+1,n,star (SX4,SY4))) ans++;    }} printf ("%lld\n", ans); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2002 Squares (two minutes)

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.