hdu 1558 Segment set

來源:互聯網
上載者:User
Segment set

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2509 Accepted Submission(s): 960

Problem DescriptionA segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set.

InputIn the first line there is an integer t - the number of test case. For each test case in first line there is an integer n (n<=1000) - the number of commands.

There are two different commands described in different format shown below:

P x1 y1 x2 y2 - paint a segment whose coordinates of the two endpoints are (x1,y1),(x2,y2).
Q k - query the size of the segment set which contains the k-th segment.

k is between 1 and the number of segments in the moment. There is no segment in the plane at first, so the first command is always a P-command.

OutputFor each Q-command, output the answer. There is a blank line between test cases.

Sample Input

110P 1.00 1.00 4.00 2.00P 1.00 -2.00 8.00 4.00Q 1P 2.00 3.00 3.00 1.00Q 1Q 3P 1.00 4.00 8.00 2.00Q 2P 3.00 3.00 6.00 -2.00Q 5

Sample Output

12225

AuthorLL

SourceHDU 2006-12 Programming Contest

RecommendLL知道兩直線相交判定方法後其實這道題就很簡單了。但是被一些小問題卡主了。尤其是那個?  :....的以前沒注意過它的優先順序和結合律。改了一下午。。。哎悲劇。。。留下調程式的代碼。以警示自己以後要細心!

#include <stdio.h>typedef struct//定義一個直線{    double x1,y1,x2,y2;} line;line l[1010];int seter[1010],sum[1010];//seter用於建立並查集。sum記錄幾合中元素個數int ok(line a,line b)//判斷兩條直線是否相交{    int flag=0;    double c,d,e,f;    double ax1,ay1,ax2,ay2,bx1,by1,bx2,by2;//構造矩形分別存為矩形左下角和右上方座標    a.x1>a.x2?ax1=a.x2,ax2=a.x1:(ax1=a.x1,ax2=a.x2);//優先順序呀。。。改死我了!    a.y1>a.y2?ay1=a.y2,ay2=a.y1:(ay1=a.y1,ay2=a.y2);//不加括弧它會看成    b.x1>b.x2?bx1=b.x2,bx2=b.x1:(bx1=b.x1,bx2=b.x2);//(?:),...;    b.y1>b.y2?by1=b.y2,by2=b.y1:(by1=b.y1,by2=b.y2);//氣死了!    if(ax1<=bx2&&ay1<=by2&&bx1<=ax2&&by1<=ay2)//快速排斥。其為兩矩形相交或內含條件        flag=1;        //if(b.x2==4.00&&flag==0)            //{printf("ok\n");       // printf("%lf  %lf  %lf %lf %lf %lf %lf %lf\n",ax1,ay1,ax2,ay2,bx1,by1,bx2,by2);}    if(flag==0)//矩形不相交直接pass掉        return 0;//其下為判斷兩線段是否跨列。求叉積    c=(b.x1-a.x1)*(b.y1-b.y2)-(b.x1-b.x2)*(b.y1-a.y1);    d=(b.x1-a.x2)*(b.y1-b.y2)-(b.x1-b.x2)*(b.y1-a.y2);    e=(a.x1-b.x1)*(a.y1-a.y2)-(a.x1-a.x2)*(a.y1-b.y1);    f=(a.x1-b.x2)*(a.y1-a.y2)-(a.x1-a.x2)*(a.y1-b.y2);    if(c*d<=0&&e*f<=0)//兩線段相交條件為相互跨列        return 1;    else        return 0;}int root(int p)//並查集找根節點加路徑壓縮{    int i,j,t;    i=p;    while(seter[i]!=i)        i=seter[i];    j=p;    while(j!=i)    {        t=seter[j];        seter[j]=i;        j=t;    }    return i;}void merge(int p,int q)//合并兩個集合{    //printf("%d to %d\n",p,q);    int r1,r2;    r1=root(p);    r2=root(q);    if(r1==r2)        return ;    if(sum[r1]>sum[r2])    {        seter[r2]=r1;        sum[r1]+=sum[r2];    }    else    {        seter[r1]=r2;        sum[r2]+=sum[r1];    }}int main(){    int i,j,k,t,n,m,c;    char com;    double x1,y1,x2,y2;    scanf("%d",&t);    for(i=1;i<=t;i++)    {        scanf("%d",&n);        getchar();        c=1;        for(j=1;j<=n;j++)//初始化            seter[j]=j,sum[j]=1;           // printf("hello\n");        for(j=1;j<=n;j++)        {            scanf("%c",&com);         //   printf("com is  %c\n",com);            if(com=='P')            {                scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);                //printf("%lf %lf %lf %lf\n",x1,y1,x2,y2);                //if(x1>x2){temp=x1,x1=x2,x2=temp;}                //if(y1>y2){temp=y1,y1=y2,y2=temp;}開始天真了。這不直接改變線段了麼。。                l[c].x1=x1,l[c].x2=x2;                l[c].y1=y1,l[c].y2=y2;                for(k=1;k<c;k++)                  if(ok(l[c],l[k]))                    merge(k,c);                c++;            }            else if(com=='Q')            {                scanf("%d",&m);                printf("%d\n",sum[root(m)]);            }            while(getchar()!='\n');//過濾掉每行後面的符號        }        if(i<t)            printf("\n");    }    return 0;}/*110P 1.00 1.00 4.00 2.00P 1.00 -2.00 8.00 4.00Q 1P 2.00 3.00 3.00 1.00Q 1Q 3P 1.00 4.00 8.00 2.00Q 2P 3.00 3.00 6.00 -2.00Q 5*/

聯繫我們

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