百练2805:正方形

Posted 逢山开路 遇水架桥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了百练2805:正方形相关的知识,希望对你有一定的参考价值。

传送门:http://bailian.openjudge.cn/practice/2805/

【题解】

n不大,枚举两个端点,推算出剩下两个,拿map或哈希看看有多少即可。

技术分享
# include <map>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n;
struct point {
    int x, y;
    point() {}
    point(int x, int y) : x(x), y(y) {}
    friend bool operator == (point a, point b) {
        return a.x == b.x && a.y == b.y;
    }
    friend bool operator != (point a, point b) {
        return !(a==b);
    }
    friend bool operator < (point a, point b) {
        return a.x < b.x || (a.x == b.x && a.y < b.y);
    }
    friend bool operator > (point a, point b) {
        return a.x > b.x || (a.x == b.x && a.y > b.y);
    }
}p[M];

map < point, int > mp;

inline void getpoint(point a, point b, point &c, point &d) {
    // a->b, right a->d
    int dy = b.y - a.y, dx = a.x - b.x;
    d = point(a.x + dy, a.y + dx);
    c = point(b.x + dy, b.y + dx);
}

int main() {
    while(cin >> n) {
        if(!n) break;
        ll ans = 0;
        mp.clear();
        for (int i=1; i<=n; ++i) {
            scanf("%d%d", &p[i].x, &p[i].y);
            mp[p[i]] = mp[p[i]] + 1;
        }
        for (int i=1; i<=n; ++i)
            for (int j=1; j<=n; ++j) {
                if(i == j || p[i] == p[j]) continue;
                point a, b;
                getpoint(p[i], p[j], a, b);
                if(a != p[i] && a != p[j] && b != p[i] && b != p[j]) {
                    if(mp.find(a) != mp.end() && mp.find(b) != mp.end()) {
                        ans += 1ll * mp[a] * mp[b];
//                        printf("(%d,%d), (%d,%d), (%d,%d), (%d,%d)\n", p[i].x, p[i].y, p[j].x, p[j].y, a.x, a.y, b.x, b.y);
//                        printf("%d == %d\n", mp[a], mp[b]);
                    }
                }
            }
        cout << ans/4 << endl;
    }
    return 0;
}
View Code

 

以上是关于百练2805:正方形的主要内容,如果未能解决你的问题,请参考以下文章

递归经典--百练4147--汉诺塔问题

百练2811--熄灯问题

递归+DFS--简单迷宫问题--百练2802

百练:2181:Jumping Cows

百练:2181:Jumping Cows

android 片段事务