您的位置:首页 > 其它

poj 2002 Squares

2016-08-11 21:04 423 查看
Squares

Time Limit: 3500MS Memory Limit: 65536K
Total Submissions: 18973 Accepted: 7320
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. 

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

Source

Rocky Mountain 2004

/*

大致题意:

有一堆平面散点集,任取四个点,求能组成正方形的不同组合方式有多少。

相同的四个点,不同顺序构成的正方形视为同一正方形。

 

解题思路:

做本题数学功底要很强= =

 

直接四个点四个点地枚举肯定超时的,不可取。

普遍的做法是:先枚举两个点,通过数学公式得到另外2个点,使得这四个点能够成正方形。然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形。

但这种做法会使同一个正方形按照不同的顺序被枚举了四次,因此最后的结果要除以4.

 

已知: (x1,y1)  (x2,y2)

则:   x3=x1+(y1-y2)   y3= y1-(x1-x2)

x4=x2+(y1-y2)   y4= y2-(x1-x2)



x3=x1-(y1-y2)   y3= y1+(x1-x2)

x4=x2-(y1-y2)   y4= y2+(x1-x2)
*/
根据以上的提示就可以做出来

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <vector>
#define MAX 14997
using namespace std;

int n, m;
struct node
{
int c;
int k;
};
int ax[10002], ay[10002];
vector<node> v[15000];

void init ()
{
int i;
for ( i = 0;i < 15000; i++ )
{
v[i].clear();
}
}

int abs ( int x )
{
return x >= 0? x: -x;
}

int hash ( int x, int y )
{
int sum = x*(1 << 5)+y;
return abs(sum%MAX); //这里千万不要忘记取正数否则会越界,,,
}

int cunzai( int x, int y )
{
int i;
int pos = hash ( x, y );
for ( i = 0;i < v[pos].size(); i++ )
{
if ( v[pos][i].c == x&&v[pos][i].k == y)
return 1;
}
return 0;
}

int main()
{
int i, j;
int x1, x2, y1, y2;
int x3, x4, y3, y4;
while ( ~scanf ( "%d", &n ) && n != 0 )
{
init();
for ( i = 0;i < n ; i++ )
{
scanf ( "%d %d", &ax[i], &ay[i] );
int pos = hash ( ax[i], ay[i] );
node temp;
temp.c = ax[i];
temp.k = ay[i];
v[pos].push_back(temp);
}

int sum = 0;
for ( i = 0;i < n-1; i++ )
{
for ( j = i+1;j < n; j++ )
{
x1 = ax[i] + (ay[i]-ay[j]);
y1 = ay[i] - (ax[i]-ax[j]);
x2 = ax[j] + (ay[i]-ay[j]);
y2 = ay[j] - (ax[i]-ax[j]);
x3 = ax[i] - (ay[i]-ay[j]);
y3 = ay[i] + (ax[i]-ax[j]);
x4 = ax[j] - (ay[i]-ay[j]);
y4 = ay[j] + (ax[i]-ax[j]);
//x3=x1+(y1-y2) y3= y1-(x1-x2)
//x4=x2+(y1-y2) y4= y2-(x1-x2)
//或
//x3=x1-(y1-y2) y3= y1+(x1-x2)
//x4=x2-(y1-y2) y4= y2+(x1-x2)
if ( exist ( x1, y1 ) && exist ( x2, y2 ) )
{
sum++;
}
if ( exist ( x3, y3 ) && exist ( x4, y4 ) )
{
sum++;
}
}
}
printf ( "%d\n", sum/4 );
}
}


代码菜鸟,如有错误,请多包涵!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: