PKU 2002 - Squares (计算几何 + 二分)

题意

找出给出的点里有多少正方形。

思路

枚举两个点,计算剩下两个点二分。

代码

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1000 + 10;
const int MOD = 9901;
const int dir[][2] = { {-1, 0}, {0, -1}, { 1, 0 }, { 0, 1 } };
int cases = 0;
typedef pair<int, int> pii;
 
pii arr[MAXN];
set<pii> mp;
 
int main()
{
    //ROP;
    int n;
    while (scanf("%d", &n), n)
    {
        mp.clear();
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d", &arr[i].X, &arr[i].Y);
            mp.insert(arr[i]);
        }
        sort(arr, arr+n);
        LL cnt = 0;
        for (int i = 0; i < n; i++)
            for (int j = i+1; j < n; j++)
            {
                int t1 = arr[i].X + arr[i].Y - arr[j].Y;
                int k1 = arr[i].Y - (arr[i].X - arr[j].X);
                int t2 = arr[j].X + arr[i].Y - arr[j].Y;
                int k2 = arr[j].Y - (arr[i].X - arr[j].X);
                if (mp.count(MP(t1, k1)) && mp.count(MP(t2, k2))) cnt++;
            }
        printf("%lld\n", (cnt>>1));
    }
    return 0;
}

Powered by Jekyll and Theme by solid