Description
A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates.
Input
The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are 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
4 1 0 0 1 1 1 0 0 9 0 0 1 0 2 0 0 2 1 2 2 2 0 1 1 1 2 1 4 -2 5 3 7 0 0 5 2 0
Sample Output
1 6 1
哈希表做法,程序略渣
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
#define Mod 1000007
using namespace std;
struct node
{
int x,y;
int next;
} ls[1010];
int top;
int Hash[Mod];
int x[1010],y[1010];
void Add(int x,int y)
{
int a = (x * x + y * y) % Mod;
ls[top].x = x;
ls[top].y = y;
ls[top].next = Hash[a];
Hash[a] = top++;
}
bool Search(int x,int y)
{
int a = (x * x + y * y) % Mod;
int next = Hash[a];
while(next != -1 )
{
if(ls[next].x == x && ls[next].y == y)
return true;
next = ls[next].next;
}
return false;
}
int main()
{
// freopen("in.txt","r",stdin);
int n;
while(cin>>n && n)
{
top = 0;
memset(Hash,-1,sizeof(Hash));
for(int i=0; i<n; i++)
{
cin>>x[i]>>y[i];
Add(x[i],y[i]);
}
LL ant = 0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
int xx = x[i] - x[j];
int yy = y[i] - y[j];
int x3 = x[i] + yy;
int y3 = y[i] - xx;
int x4 = x[j] + yy;
int y4 = y[j] - xx;
if(Search(x3,y3) && Search(x4,y4))
ant++;
x3 = x[i] - yy;
y3 = y[i] + xx;
x4 = x[j] - yy;
y4 = y[j] + xx;
if(Search(x3,y3) && Search(x4,y4))
ant++;
}
}
cout<<ant/4<<endl;
}
return 0;
}
//二分查找
<pre name="code" class="html">#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f
#define Mod 1000007
using namespace std;
struct node
{
int x,y;
} ls[1010];
bool cmp(node a,node b)
{
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
bool Search(int low,int high,int x,int y)
{
int i = low,j = high;
while(i <= j)
{
int mid = (i + j) / 2;
if(ls[mid].x == x && ls[mid].y == y)
return true;
if(ls[mid].x < x)
i = mid + 1;
else if(ls[mid].x > x)
j = mid - 1;
else if(ls[mid].x == x)
{
if(ls[mid].y < y)
i = mid + 1;
else if(ls[mid].y > y)
j = mid - 1;
}
}
return false;
}
int main()
{
std::ios::sync_with_stdio(false);
int n;
while(cin>>n && n)
{
for(int i=0; i<n; i++)
{
cin>>ls[i].x>>ls[i].y;
}
sort(ls,ls+n,cmp);
int ant = 0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
int xx = ls[i].x - ls[j].x;
int yy = ls[i].y - ls[j].y;
int x3 = ls[i].x - yy;
int y3 = ls[i].y + xx;
int x4 = ls[j].x - yy;
int y4 = ls[j].y + xx;
if(Search(0,n-1,x3,y3) && Search(0,n-1,x4,y4))
ant++;
x3 = ls[i].x + yy;
y3 = ls[i].y - xx;
x4 = ls[j].x + yy;
y4 = ls[j].y - xx;
if(Search(0,n-1,x3,y3) && Search(0,n-1,x4,y4))
ant++;
}
}
cout<<ant/4<<endl;
}
return 0;
}