OpenJ_Bailian - 2805 正方形 【map】

题目大意,给你n个点,让你判断有几个正方形

n<=1000
题目分析:这个题就简单暴力枚举任意两个点,判断剩下两个点是否在内就可,一开始试图用hash去判断是否存在,后来没有找到合适的hash方式=-= 还是太菜,这个题的话,直接用map就可以了,先对点进行排序,然后放到map里,然后再判断是否存在就行
还有一个坑点就是,枚举任意两个点 的时候有顺序关系,所以,注意循环的时候i [1…n ] ,j[1….n] 即可。i≠j的时候,求点就可以了。最后因为这样算有重复,所以要除以四

#include <bits/stdc++.h>
#define cl(a) memset(a,0,sizeof(a))
using namespace std;
struct point
{
    int x,y;
    friend bool operator <( point a,point b)
    {
        if(a.x==b.x)return a.y<b.y;
        else return a.x<b.x;
    }
};
const int maxn=5500;
map<point,int>mp;
int n;
point a[maxn];
int ans=0;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while(cin>>n&&n)
    {
        ans=0;
       // cin>>n;
        mp.clear();
        cl(a);
        for(int i=1; i<=n; i++)
        {
            point t;
            cin>>t.x>>t.y;
            a[i]=t;
            mp[t]++;
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(i==j)continue;
                point p1,p2,p3,p4;
                p1=a[i];
                p2=a[j];
                int dx = p2.x-p1.x;
                int dy = p2.y-p1.y;
                p3.x = p1.x+dy;
                p3.y = p1.y-dx;
                p4.x = p2.x +dy;
                p4.y = p2.y-dx;
                if(mp.count(p3)!=0&&mp.count(p4)!=0) ans++;
            }
        }
        cout<<ans/4<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/becky_w/article/details/80774273
今日推荐