POJ 2398 Toy Storage (calculating geometry, judging points and segment relationships)

Source: Internet
Author: User

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

Test instructions is about dividing a box into n+1 parts with n board.

Then put toy inside, given the box, board, and toy coordinates.

Ask all the toy put out, how many parts have T toy;

Simple computational geometry

The relationship between the point and the line needs to be judged.

Judge a point on the left and right side of the line

The left and right direction is relative to the forward direction, as long as you specify the direction of the forward to know about (for example, specify the direction of progress from the straight line from the beginning to the end). Determining whether a point is on the left side of a line is the most basic algorithm in computational geometry. Use vectors to judge.
Definition: The area of three points P1 (x1,y1), P2 (X2,y2), P3 (X3,Y3) on the plane:


S (p1,p2,p3) =|y1 y2 y3|= (x1-x3) * (y2-y3)-(y1-y3) * (X2-X3)

When the P1P2P3 is counterclockwise, S is positive, and S is negative when the p1p2p3 is clockwise.

The starting point of the vector is a, the end is B, the point of Judgment is C,
If S (a,b,c) is positive, C is on the left side of Vector AB;
If S (a,b,c) is negative, C is on the right side of vector ab;
If S (a,b,c) is 0, then C is on the straight line AB.

/************************************************************************* > File name:code/2015summer/0718/ B.cpp > Author:111qqz > Email: [email protected] > Created time:2015 July 18 Saturday 11:58 14 seconds *********** *************************************************************/#include<iostream>#include<iomanip>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>using namespacestd;#defineREP (i, n) for (int i=0;i<int (n); ++i)typedefLong Longll;typedef unsignedLong LongULL;Const intn=2e3+5;structnode{intx, y;};structnode rec,rec2;structnode Par[n],par2[n];structnode Toy[n];intAns[n],cnt[n];intn,m;BOOLJudge (node P1,node p2,node p3)//determine if the point is in the straight line [right!!]{    ints = (p1.x-p3.x) * (P2.Y-P3.Y)-(P1.Y-P3.Y) * (p2.x-p3.x); if(s>0)return false; if(s<0)return true;}BOOLCMP (node A,node b) {if(a.x<b.x)return true; if(A.X==B.X&AMP;&AMP;A.Y&LT;B.Y)return true; return false;}intMain () { while(SCANF ("%d", &n)!=eof&&N) {memset (ans,0,sizeof(ans)); memset (PAR,0,sizeof(PAR)); memset (PAR2,0,sizeof(PAR2)); memset (Toy,0,sizeof(toy)); CIN>>m>>rec.x>>rec.y>>rec2.x>>rec2.y;  for(inti =1; I <= N; i++) {cin>>par[i].x>>par2[i].x; Par[i].y=rec.y; Par2[i].y=rec2.y; }       for(inti =1; I <= N-1; i++)      {         for(intj = i+1; J <= N; J + +)        {            if(par[i].x>par[j].x)            {swap (par[i].x,par[j].x); //swap (PAR[I].Y,PAR[J].Y);swap (par2[i].x,par2[j].x); //swap (PAR2[I].Y,PAR2[J].Y);            }        }      }//for (int i = 1; I <= N; i++)//cout<<par[i].x<<endl;       for(inti =1; I <= m; i++) {cin>>toy[i].x>>toy[i].y; }      intp; Sort (Toy+1, toy+m+1, CMP);//If the first doll is in the K-sub-division, then the first i+1 doll is at least in the K-sub-section. (a great God said, can write the optimization handy//for (int i = 1; I <= m; i++) cout<< "x[i]:" <<toy[i].x<< "y[i]:" <<toy[i].y<< Endl;       for(inti =1;  I <= m; i++) {p= n +1;//if it is on the right side of all board, then it must be in the last sub-division (N boards form a n+1)        BOOLok=false;  for(intj =1; J <= N; J + +) {OK=judge (Par2[j],par[j],toy[i]); if(!OK) {        //cout<< "I:" <<i<< "J:" <<j<< "<<par2[j].x<<" "<<par2[j].y< < "<<par[j].x<<" <<par[j].y<<endl;p =J;  Break; //cout<< "hhhhhh" << "I:" <<i<< "J:" <<j<<endl;}} Ans[p]++; } cout<<"Box"<<Endl; memset (CNT,0,sizeof(CNT));  for(inti =1; I <= n+1; i++)      {        if(ans[i]==0)Continue; Cnt[ans[i]]++; //printf ("%d:%d\n", I,ans[i]);      }       for(inti =1;  I <= m; i++)      {        if(cnt[i]==0)Continue; printf ("%d:%d\n", I,cnt[i]); }    }    return 0;}

POJ 2398 Toy Storage (calculating geometry, judging points and segment relationships)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.