总时间限制:
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
示例代码
#include <iostream>
#include <map>
#include <cmath>
using namespace std;
int main() {
int n;
while (cin >> n && n != 0) {
map<pair<int, int>, int> points;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
points[{x, y}] = 1;
}
int count = 0;
for (auto it1 = points.begin(); it1 != points.end(); it1++) {
for (auto it2 = next(it1); it2 != points.end(); it2++) {
int dx = it2->first.first - it1->first.first;
int dy = it2->first.second - it1->first.second;
if (points.count({it1->first.first + dy, it1->first.second - dx})
&& points.count({it2->first.first + dy, it2->first.second - dx})) {
count++;
}
}
}
cout << count / 2 << endl;
}
return 0;
}