POJ2002 Squares(二維點雜湊)

來源:互聯網
上載者:User

標籤:

題目連結:

http://poj.org/problem?id=2002


題意:

給定n個點 判斷這n個點可以構成多少正方形。


分析:

暴力枚舉一條邊的兩個端點,然後根據全等三角形可以求出可以構成正方形的另外兩個邊的端點,然後判斷這兩個兩存不存在。

因此首先要把所有的點雜湊一下,然後依次暴力枚舉,因此四條邊都統計了一次 因此最後要除4.


代碼如下:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;const int mod = 10007;const int maxn = 1010;struct nod{    int x,y;};inline int read()//輸入外掛{    char ch=getchar();    int x=0,f=1;    while(ch>'9'||ch<'0')    {        if(ch=='-')f=-1;        ch=getchar();    }    while(ch<='9'&&ch>='0')    {        x=x*10+ch-'0';        ch=getchar();    }    return x*f;}struct hashmap{    nod a[maxn];    int head[mod],next[maxn],cnt;    void init()    {        cnt=0;        memset(head,-1,sizeof(head));        memset(next,0,sizeof(next));    }    bool find(nod val)    {        int tmp = (val.x*val.x+val.y*val.y)%mod;        for(int i=head[tmp]; i!=-1; i=next[i])            if(a[i].x==val.x&&a[i].y==val.y)                return true;        return false;    }    void add(nod val)    {        int tmp = (val.x*val.x+val.y*val.y)%mod;        for(int i=head[tmp]; i!=-1; i=next[i])            if(a[i].x==val.x&&a[i].y==val.y)                return;        a[cnt]=val;        next[cnt]=head[tmp];        head[tmp]=cnt++;    }} h;nod p[maxn];int main(){    int n;    while(~scanf("%d",&n))    {        if(n==0) break;        h.init();        for(int i=0; i<n; i++)        {            p[i].x=read();            p[i].y=read();            h.add(p[i]);        }        int ans = 0;        for(int i=0; i<n; i++)        {            for(int j=i+1; j<n; j++)            {                nod tmp1,tmp2;                tmp1.x = p[i].x - (p[i].y - p[j].y);                tmp1.y = p[i].y + (p[i].x - p[j].x);                tmp2.x = p[j].x - (p[i].y - p[j].y);                tmp2.y = p[j].y + (p[i].x - p[j].x);                if(h.find(tmp1)&&h.find(tmp2)) ans++;            }        }        for(int i=0; i<n; i++)        {            for(int j=i+1; j<n; j++)            {                nod tmp1,tmp2;                tmp1.x = p[i].x + (p[i].y - p[j].y);                tmp1.y = p[i].y - (p[i].x - p[j].x);                tmp2.x = p[j].x + (p[i].y - p[j].y);                tmp2.y = p[j].y - (p[i].x - p[j].x);                if(h.find(tmp1)&&h.find(tmp2)) ans++;            }        }        ans >>= 2;        printf("%d\n",ans);    }    return 0;}



POJ2002 Squares(二維點雜湊)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.