阿里巴巴公開賽 Fruit Ninja(找平面內共線最多的多邊形個數)

來源:互聯網
上載者:User

 假設有一種情況共線最多,那麼一定可以通過平移和轉動那條直線,使直線通過其中的至少2個多變形的頂點,而不改變是否穿過多邊形的性質,所以只需枚舉這樣的多邊形就可以了。

n為1 和2的時候直接輸出n就好了,一開始看錯了n的範圍,沒考慮1的情況,WA了好多次。

#include <cstdio>struct Node{    int x,y;}node[11][11];struct Line{    int A , B , C ;}tmp;int cnt[11];int n;bool is_through (int s)//judge the sth polygon{    for (int i=0 ; i<n ; ++i)     for (int j=i+1 ; j<n ; ++j)     {         int x1,x2,y1,y2;         x1=node[s][i].x,y1=node[s][i].y;         x2=node[s][j].x,y2=node[s][j].y;         if((tmp.A*x1+tmp.B*y1+tmp.C)*(tmp.A*x2+tmp.B*y2+tmp.C)<=0)return true;     }    return false ;}int getmax(){    int ans=0;    int maxans=0;    for (int i=0 ; i<n ; ++i)      for (int p=0 ; p<cnt[i] ; ++p)         for (int j=i+1 ; j<n ; ++j)            for (int q=0 ; q<cnt[j] ; ++q)             {                 ans=0;                 Node a=node[i][p],b=node[j][q];                 tmp.A = b.y - a.y ;                 tmp.B = a.x - b.x ;                 tmp.C = b.x * a.y - a.x * b.y ;                 for (int k=0 ; k<n ; ++k)                 if(k!=i && k!=j)                   if(is_through(k))++ans;                 //printf("%d\n",ans);                 if(ans>maxans)maxans=ans;             }     return maxans;}int main (){    int cas;    //freopen ("Fruit.in","r",stdin);    //freopen ("Fruit.txt","w",stdout);    scanf("%d",&cas);    for (int I=1 ; I<=cas ; ++I)    {        scanf("%d",&n);        for (int i=0 ; i<n ; ++i)        {            scanf("%d",cnt+i);            for (int j=0 ; j<cnt[i] ; ++j)            {                scanf("%d%d",&node[i][j].x,&node[i][j].y);            }        }        if(n==1 || n==2){printf("Case %d: %d\n",I,n); continue;}        printf("Case %d: %d\n",I,getmax()+2);    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.