POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客

下面三种情况比较特殊,特别是第三种

G++怎么交都是WA,同样的代码C++A了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const double eps = 1e-8;
const double PI = acos(-1.0);
const double INF = 1e5; int sgn(double x)
{
if(fabs(x) < eps) return 0;
return x < 0 ? -1: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;
}
}; struct Line
{
Point p,q;
Line() {};
Line(Point _p,Point _q)
{
p = _p,q = _q;
}
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为2表示相交
//只有第一个值为2时,交点才有意义
pair<int,Point> operator &(const Line &b)const
{
Point res = p;
if(sgn((p-q)^(b.p-b.q)) == 0)
{
if(sgn((p-b.q)^(b.p-b.q)) == 0)
return make_pair(0,res);//重合
else return make_pair(1,res);//平行
}
double t = ((p-b.p)^(b.p-b.q))/((p-q)^(b.p-b.q));
res.x += (q.x-p.x)*t;
res.y += (q.y-p.y)*t;
return make_pair(2,res);
}
}; //*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.p.x,l1.q.x) >= min(l2.p.x,l2.q.x) &&
max(l2.p.x,l2.q.x) >= min(l1.p.x,l1.q.x) &&
max(l1.p.y,l1.q.y) >= min(l2.p.y,l2.q.y) &&
max(l2.p.y,l2.q.y) >= min(l1.p.y,l1.q.y) &&
sgn((l2.p-l1.q)^(l1.p-l1.q))*sgn((l2.q-l1.q)^(l1.p-l1.q)) <= 0 &&
sgn((l1.p-l2.q)^(l2.p-l2.q))*sgn((l1.q-l2.q)^(l2.p-l2.q)) <= 0;
} Point pot[105],peg;
double rad; int main()
{
// freopen("in.txt","r",stdin);
int t;
double x1,y1,x2,y2;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line L1=Line(Point(x1,y1),Point(x2,y2));
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
Line L2=Line(Point(x1,y1),Point(x2,y2));
if(sgn(L1.p.y - L1.q.y)==0 || sgn(L2.p.y - L2.q.y)==0)
{
puts("0.00");
continue;
}
if(!inter(L1,L2))
{
puts("0.00");
continue;
}
if(sgn(L1.p.y - L1.q.y) < 0) swap(L1.p,L1.q);
if(sgn(L2.p.y - L2.q.y) < 0) swap(L2.p,L2.q);
if(inter(Line(L1.p,Point(L1.p.x,INF)),L2))
{
puts("0.00");
continue;
}
if(inter(Line(L2.p,Point(L2.p.x,INF)),L1))
{
puts("0.00");
continue;
}
pair<int,Point> pr=L1 & L2;
Point cp=pr.second;
pr=Line(L1.p,Point(INF,L1.p.y)) & L2;
double ans=fabs((L1.p-cp)^(pr.second-cp))/2;
pr=Line(L2.p,Point(INF,L2.p.y)) & L1;
ans=min(ans,fabs((L2.p-cp)^(pr.second-cp))/2);
printf("%.2lf\n",ans);
}
return 0;
}

POJ 2826 An Easy Problem? 判断线段相交的更多相关文章

  1. POJ 2826 An Easy Problem?!(线段交点+简单计算)

    Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Be ...

  2. POJ 2826 An Easy Problem?!

    An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7837   Accepted: 1145 ...

  3. 【POJ 2653】Pick-up sticks 判断线段相交

    一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...

  4. POJ 2653 Pick-up sticks(判断线段相交)

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7699   Accepted: 2843 De ...

  5. POJ 2826 An Easy Problem?![线段]

    An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12970   Accepted: 199 ...

  6. POJ 1066 - Treasure Hunt - [枚举+判断线段相交]

    题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...

  7. POJ 2653 - Pick-up sticks - [枚举+判断线段相交]

    题目链接:http://poj.org/problem?id=2653 Time Limit: 3000MS Memory Limit: 65536K Description Stan has n s ...

  8. 简单几何(线段相交) POJ 2826 An Easy Problem?!

    题目传送门 题意:两条线段看成两块木板,雨水从上方往下垂直落下,问能接受到的水的体积 分析:恶心的分类讨论题,考虑各种情况,尤其是入口被堵住的情况,我的方法是先判断最高的两个点是否在交点的同一侧,然后 ...

  9. POJ 2826 An Easy Problem?! 好的标题

    受该两块木板以形成槽的效果.Q槽可容纳雨水多,注意雨爆跌,思想是非常easy,分类讨论是有点差. 1.假定两条线段不相交或平行,然后再装0: 2.有一个平行x轴.连衣裙0. 3.若上面覆盖以下的,装0 ...

随机推荐

  1. 【转载】fedora22和win10之间的文件共享互访

    fedora22和win10之间的文件共享互访 钢铁侠与孔子 关注 2016.06.04 14:10* 字数 1327 阅读 2170评论 0喜欢 1 一,相关知识了解(本文执行环境为fedora22 ...

  2. [转载]centos 7(1611)安装笔记

    centos 7(1611)安装笔记   麻烦 前天我把双系统笔记本里的 deepin 的磁盘分区直接从 Windows 7 磁盘管理里格式化了,结果悲催了,开不了机了,显示: 我以为是 Window ...

  3. 安装oracle 时“[INS-30014]无法检查指定的位置是否位于 CFS上”问题

    错误截图: 错误信息: [INS-30014]无法检查指定的位置是否位于 CFS上 解决方案: 通过修改hosts文件,向C:\Windows\System32\drivers\etc\hosts文件 ...

  4. 继承(extends), 多态 , 抽象(abstract)接口() 易混难点解析

    特性 java是单继承的,一个类直接继承的父类只能有唯一的一个 java中父类可以有多个子类 Object是所有类的父类,一个类没有父类则默认继承Object; 继承中的重写 子类重写方法访问权限不能 ...

  5. SystemVerilog MCDF验证结构

    MCDF的设计和验证花费的时间:(工作中假设的时间) design  cycle time  ==10days how about 验证?verify? 模块越往上(大')验证花费的时间越来越大,但是 ...

  6. MySQL之数据查询语言(DQL)

    数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHERE子句组成的查询块: SELECT <字段> FROM <表名> WHERE <查询条件> - ...

  7. 用华为MindSpore框架训练数据库类型的数据集

    技术背景 在前面一篇博客我们讲到三种用python去读取一个文件的指定行的操作,最终给出的一个结论大概是,对于大型的数据而言,最快的找到指定行的方法是Linux系统自带的sed指令,那么是否只有这一种 ...

  8. 字符串算法(string_algorithm)

    format 作用 格式化输出对象,可以不改变流输出状态实现类似于printf()的输出 头文件 #include <boost/format.hpp> using namespace b ...

  9. 让ThreadPoolExecutor的workQueue占满时自动阻塞submit()方法

    public class BlockingSubmitExecutor { private ExecutorService executor = new ThreadPoolExecutor(2, 2 ...

  10. CodeGen字段循环Field Loop

    CodeGen字段循环Field Loop 字段循环是一个模板文件构造,它允许迭代CodeGen拥有的有关字段的集合.这些字段定义可以来自以下两个位置之一: •如果基于从存储库结构中获取的信息生成代码 ...