亲测不hash会MLE= =
Squares
Time Limit: 3500MS | | Memory Limit: 65536K |
Total Submissions: 14856 | | Accepted: 5632 |
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
Source
[Submit] [Go Back] [Status] [Discuss]
正方形(中文版)
总时间限制:3500ms内存限制:65536kB描述
给定直角坐标系中的若干整点,请寻找可以由这些点组成的正方形,并统计它们的个数。
输入包括多组数据,每组数据的第一行是整点的个数n(1<=n<=1000),其后n行每行由两个整数组成,表示一个点的x、y坐标。输入保证一组数据中不会出现相同的点,且坐标的绝对值小于等于20000。输入以一组n=0的数据结尾。输出对于每组输入数据,输出一个数,表示这组数据中的点可以组成的正方形的数量。样例输入
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
样例输出
1
6
1
我的解答
/*=============================================================================
# FileName: square.cpp
# Desc: poj 2002
# Author: zhuting
# Email: cnjs.zhuting@gmail.com
# HomePage: my.oschina.net/locusxt
# Version: 0.0.1
# CreatTime: 2013-12-04 18:27:36
# LastChange: 2013-12-04 19:35:39
# History:
=============================================================================*/
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <vector>
#define maxn 1005
#define maxl 40005
#define offset 20000/*偏移避免负数*/
using namespace std;
/*不hash会爆内存...*/
class point
{
public:
int x;
int y;
point()
{
x = 0;
y = 0;
}
};
point p[maxn];
class hash/*拉链法解决冲突的问题*/
{
public:
vector <int> vn;
};
hash h[maxl];
bool find(int x, int y)/*寻找是否有点(x,y)*/
{
int len = h[x].vn.size();
if (len == 0) return 0;
for (int i = 0; i < len; ++i)
{
if (h[x].vn[i] == y)
return 1;
}
return 0;
}
void add(int x, int y)/*加入点*/
{
h[x].vn.push_back(y);
return;
}
int main()
{
int n = 0;
int xtmp = 0, ytmp = 0;
while(scanf("%d", &n) && n)
{
memset (p, 0, sizeof(p));
memset (h, 0, sizeof(h));
for (int i = 0; i < n; ++i)
{
scanf("%d%d", &xtmp, &ytmp);
xtmp += offset;
ytmp += offset;
add(xtmp, ytmp);
p[i].x = xtmp;
p[i].y = ytmp;
}
/*没有必要排序*/
//sort(p, p + n, cmpx);
int square_num = 0;
for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n; ++j)
{
int pix = p[i].x;
int piy = p[i].y;
int pjx = p[j].x;
int pjy = p[j].y;
int dy = pjy - piy;
int dx = pjx - pix;
/*分直线上下找*/
if (find(pix - dy, piy + dx) && find(pjx - dy, pjy + dx))
{
++square_num;
}
if (find(pix + dy, piy - dx) && find(pjx + dy, pjy - dx))
{
++square_num;
}
}
printf("%d\n", square_num / 4);/*存在重复*/
}
return 0;
}