poj 2826 An Easy Problem?!(线段交,细节题)

来源:互联网 发布:如何查看软件源代码 编辑:程序博客网 时间:2024/05/16 18:34

【题目大意】:给出两条线段,问这两条线段能够接住多少水。


【解题思路】:先大概估算了一下,这道题一共wa了不下20次。题目的大意不难,难在下面几个注意点上:

1)线段不能平行或者重合

                        2)不能够出现斜率为0的线段(10+wa以上才发现)

                        3)线段相交后开口应该向上

                        4)处于高位的线段的在x轴上的投影不能遮住地位线段的投影(两个方向)

5)求面积的时候,利用叉积/2来求,注意要以地位点的纵坐标为主

【代码】:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;#define eps 1e-8double p,q;int cnt;struct Point{    double x, y;    Point() {}    Point(double a, double b)    {        x = a, y = b;    }}point[4];struct Line{    Point a, b;    Line() {}    Line(Point x, Point y)    {        a = x, b = y;    }}line[2];inline int sig(double k){    return k < -eps ? -1 : k > eps;}inline double det(double x1, double y1, double x2, double y2){    return x1 * y2 - x2 * y1;}inline double dotdet(double x1, double y1, double x2, double y2){    return x1 * x2 + y1 * y2;}inline double dot(Point o, Point a, Point b){    return dotdet(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);}inline double xmult(Point o, Point a, Point b){    return det(a.x - o.x, a.y - o.y, b.x - o.x, b.y - o.y);}inline int between(Point o, Point a, Point b){    return sig(dot(o, a, b));}inline bool sameline(Line u, Line v){    if (sig(xmult(u.a, v.a, v.b)) == 0 && sig(xmult(u.b, v.a, v.b)) == 0)  return true;    return false;}inline bool parallel(Line u, Line v){//利用斜率判平行,返回true表示平行    return sig((u.a.x - u.b.x) * (v.a.y - v.b.y) - (v.a.x - v.b.x) * (u.a.y - u.b.y)) == 0;}inline int intersectt(Point a, Point b, Point c, Point d, Point &p){    double s1, s2, s3, s4;    int d1 = sig(s1 = xmult(a, b, c));    int d2 = sig(s2 = xmult(a, b, d));    int d3 = sig(s3 = xmult(c, d, a));    int d4 = sig(s4 = xmult(c, d, b));    if ((d1^d2) == -2 && (d3^d4) == -2)    {        p.x = (c.x * s2 - d.x * s1) / (s2 - s1);        p.y = (c.y * s2 - d.y * s1) / (s2 - s1);        return 1;    }    if((d1==0&&d2==0)||(d3==0&&d4==0)) return 0;    if (d1 == 0 && between(c, a, b) <= 0) {p=c; return 2;}    if (d2 == 0 && between(d, a, b) <= 0) {p=d; return 2;}    if (d3 == 0 && between(a, c, d) <= 0) {p=a; return 2;}    if (d4 == 0 && between(b, c, d) <= 0) {p=b; return 2;}    return 0;}inline int intersect(Line u, Line v, Point &p){    return intersectt(u.a, u.b, v.a, v.b, p);}inline double get_area(Point a,Point b,Point c){    return fabs((a.x*b.y-a.y*b.x+b.x*c.y-c.x*b.y+c.x*a.y-a.x*c.y)/2);}double solve(Line a,Line b){    if ((point[0].y==point[1].y) ||(point[2].y==point[3].y)) return 0;    if (sameline(a,b)==true) return 0;  //判重合    if (parallel(a,b)==true) return 0;  //判平行    Point tmp;    int k;    k=intersect(a,b,tmp);               //判相交,顺便求交点    if (k==0) return 0;    else    {        //判断开口向上        cnt=0;        for (int i=0; i<4; i++)            if (point[i].y>tmp.y) cnt++;        if (cnt<=1) return 0;        //判断线段的覆盖        if (point[1].x==point[3].x) return 0;        if ((point[1].x<tmp.x&&point[3].x<point[1].x&&xmult(tmp,point[3],point[1])>0)||            (point[1].x>tmp.x&&point[3].x>point[1].x&&xmult(tmp,point[3],point[1])<0))            return 0;        /*        if (point[1].x<=tmp.x && point[3].x<=tmp.x)        {            if (point[1].x>=point[3].x) return 0;        }        //右侧线段的覆盖        if (point[1].x>=tmp.x && point[3].x>=tmp.x)        {            if (point[3].x>=point[1].x) return 0;        }*/        //求面积,以低的木板作为标准        double anss;        Point tmp1;        tmp1=Point((point[1].y-tmp.y)*(point[3].x-tmp.x)/(point[3].y-tmp.y)+tmp.x,point[1].y);        anss=get_area(tmp,point[1],tmp1);        return anss;    }}int main(){    int T;    scanf("%d",&T);    while (T--)    {        scanf("%lf%lf",&p,&q);        point[0]=Point(p,q);        scanf("%lf%lf",&p,&q);        point[1]=Point(p,q);        scanf("%lf%lf",&p,&q);        point[2]=Point(p,q);        scanf("%lf%lf",&p,&q);        point[3]=Point(p,q);        if (point[0].y>point[1].y) swap(point[0],point[1]);        if (point[2].y>point[3].y) swap(point[2],point[3]);        if (point[1].y>point[3].y) {swap(point[0],point[2]); swap(point[1],point[3]); }        line[0]=Line(point[0],point[1]);        line[1]=Line(point[2],point[3]);        printf("%.2lf\n",solve(line[0],line[1]));    }    return 0;}

【运行结果】wa的太多一个版都放不下。。。。



原创粉丝点击