2013.7.18 acm_schooltraining解题报告

来源:互联网 发布:淘宝售假违规扣24分 编辑:程序博客网 时间:2024/05/22 07:40

A - A
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status

Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.

Note:
You can assume that two segments would not intersect at more than one point.


Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending.
A test case starting with 0 terminates the input and this test case is not to be processed.


Output
For each case, print the number of intersections, and one line one case.


Sample Input

0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00 
0.00 0.00 1.00 1.00 
0.00 1.00 1.00 0.000 
0.00 0.00 1.00 0.00 
0



Sample Output

1 3
分析:题目要求对输入的n条线段判断线段相交的交点,如果多条相交的线段共交与一点那么针对每条线段交点加1,当然相同的两条线段交点数只加1次。因为数据量不大,所以直接用两重循环判断每对线段
线段相交应满足:任一条线段的两端应在另一条线段的两侧(此为标准相交,两叉积异号),若在端点处相交则两叉积都为0(重叠,与共线有点区别),有一个为0(在端点)。因为本题假设不共线,所以为后一种情况,也就直接可加1;
判断两线段相交,程序中用的是快速排斥试验和跨立试验。虽说跨立试验独立可判断两条直线相交,但是先用快速排斥试验可提高速度。

way:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#include <map>#include <vector>#include <set>#include <string>#include <math.h>using namespace std;const double eps = 1e-8;const double PI = acos(-1.0);int sgn(double x)      {        if(fabs(x) < eps)return 0;        if(x < 0)return -1;        else return 1;}struct Point{        double x,y;        Point(){}        Point(double _x,double _y)        {                x = _x;y = _y;        }        Point operator -(const Point &b)const        {                return Point(x - b.x,y - b.y);        }        double operator ^(const Point &b)const        {                return x*b.y - y*b.x;        }        double operator *(const Point &b)const        {                return x*b.x + y*b.y;        }        void transXY(double B)        {                double tx = x,ty = y;                x = tx*cos(B) - ty*sin(B);                y = tx*sin(B) + ty*cos(B);        }};struct Line{        Point s,e;        Line(){}        Line(Point _s,Point _e)        {                s = _s;e = _e;        }    pair<int,Point> operator &(const Line &b)const    {        Point res = s;        if(sgn((s-e)^(b.s-b.e)) == 0)        {            if(sgn((s-b.e)^(b.s-b.e)) == 0)                return make_pair(0,res);//ÖØºÏ            else return make_pair(1,res);//ƽÐÐ        }        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));        res.x += (e.x-s.x)*t;        res.y += (e.y-s.y)*t;        return make_pair(2,res);    }};Line a[120];bool inter(Line l1,Line l2){        return    max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&    max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&    max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&    max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&    sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&   //判断端点在线段两侧    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;}int main(){  int n,i,ans;  while(cin>>n,n){      for(i=0;i<n;i++)          cin>>a[i].s.x>>a[i].s.y      >>a[i].e.x>>a[i].e.y;  /* cout<<a[1].s.x<<a[1].s.y   <<a[1].e.x<<a[1].e.y<<endl;*/ans=0;for(i=0;i<n-1;i++)           for(int j=i+1;j<n;j++)               if(inter(a[i],a[j]))                  ans++;        cout<<ans<<endl;     }      return 0;}
C - C

Description

We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point. In this problem you will use your algebraic knowledge to create a program that determines how and where two lines intersect.
Your program will repeatedly read in four points that define two lines in the x-y plane and determine how and where the lines intersect. All numbers required by this problem will be reasonable, say between -1000 and 1000.

Input

The first line contains an integer N between 1 and 10 describing how many pairs of lines are represented. The next N lines will each contain eight integers. These integers represent the coordinates of four points on the plane in the order x1y1x2y2x3y3x4y4. Thus each of these input lines represents two lines on the plane: the line through (x1,y1) and (x2,y2) and the line through (x3,y3) and (x4,y4). The point (x1,y1) is always distinct from (x2,y2). Likewise with (x3,y3) and (x4,y4).

Output

There should be N+2 lines of output. The first line of output should read INTERSECTING LINES OUTPUT. There will then be one line of output for each pair of planar lines represented by a line of input, describing how the lines intersect: none, line, or point. If the intersection is a point then your program should output the x and y coordinates of the point, correct to two decimal places. The final line of output should read "END OF OUTPUT".

Sample Input

50 0 4 4 0 4 4 05 0 7 6 1 0 2 35 0 7 6 3 -6 4 -32 0 2 27 1 5 18 50 3 4 0 1 2 2 5

Sample Output

INTERSECTING LINES OUTPUTPOINT 2.00 2.00NONELINEPOINT 2.00 5.00POINT 1.07 2.20END OF OUTPUT
分析:题目要求输入n对直线,判断直线的空间关系(平行,相交(标准相交,共线)),如果标准相交则输出交点。
way:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#include <map>#include <vector>#include <set>#include <string>#include <math.h>using namespace std;const double eps = 1e-8;const double PI = acos(-1.0);int sgn(double x)      //三态函数,精确度提高{        if(fabs(x) < eps)return 0;        if(x < 0)return -1;        else return 1;}struct Point{        double x,y;        Point(){}        Point(double _x,double _y)        {                x = _x;y = _y;        }        Point operator -(const Point &b)const        {                return Point(x - b.x,y - b.y);        }        //叉积        double operator ^(const Point &b)const        {                return x*b.y - y*b.x;        }        //点积        double operator *(const Point &b)const        {                return x*b.x + y*b.y;        }        //绕原点旋转角度B(弧度值),后x,y的变化        void transXY(double B)        {                double tx = x,ty = y;                x = tx*cos(B) - ty*sin(B);                y = tx*sin(B) + ty*cos(B);        }};struct Line{        Point s,e;        Line(){}        Line(Point _s,Point _e)        {                s = _s;e = _e;        }        //两直线相交求交点        //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交        //只有第一个值为2时,交点才有意义    pair<int,Point> operator &(const Line &b)const    {        Point res = s;        if(sgn((s-e)^(b.s-b.e)) == 0)        {            if(sgn((s-b.e)^(b.s-b.e)) == 0)                return make_pair(0,res);//重合            else return make_pair(1,res);//平行        }        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));        res.x += (e.x-s.x)*t;        res.y += (e.y-s.y)*t;        return make_pair(2,res);    }};Line L1,L2;int main(){  int n;  pair<int,Point> ans;  cin>>n;  int m=n;  while(m--){          cin>>L1.s.x>>L1.s.y>>L1.e.x>>L1.e.y             >>L2.s.x>>L2.s.y>>L2.e.x>>L2.e.y;         if(m==n-1) cout<<"INTERSECTING LINES OUTPUT"<<endl;         ans=L1&L2;         if(ans.first==1)       cout<<"NONE"<<endl;         else if(ans.first==0)  cout<<"LINE"<<endl;         else                   printf("POINT %.2f %.2f\n",ans.second.x,ans.second.y);         if(m==0)   cout<<"END OF OUTPUT"<<endl;  }  return 0;}




原创粉丝点击