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. 本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法。

    本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法. 一.前言 我希望用windows远程访问centos图形界面.xmanager连接centos远程桌 ...

  2. tar -zxvf file.tar.gz //解压tar.gz

    http://apps.hi.baidu.com/share/detail/37384818 download ADT link http://dl.google.com/android/ADT-0. ...

  3. Linux_rpm包管理

    一.rpm包命令规范 1.包的组成 主包:bind-9.7.1-1.el5.i586.rpm 子包:bind-libs-9.7.1-1.el5.i586.rpm bind-utils-9.7.1-1. ...

  4. Log4j 配置多个 Logger

    引言 Log4j 是 Java 的主流日志框架,通过灵活的配置可以提供各种类型的日志服务. 在使用 Log4j 进行实际项目开发的过程中,有时不想使用 rootLogger 记录器,把所有的日志都输出 ...

  5. unity UGUI填坑 之 HorizontalLayoutGroup 和 ContentSizeFitter配合使用

    今天在项目中遇到一个问题,我们的ui过来找我,问为什么Content里的Item显示的不完全 花了半个小时看了一下,发现个小小的坑,记录一下 这些属性是用来实现,Content下的Item的偏移和间隔 ...

  6. logstash收集时filebeat区分日志

    logstash收集时filebeat区分日志     1.场景 filebeat在服务器中同时收集nginx和web项目日志,需要对两个日志在logstash中分别处理 2.版本区别 ==6.x之前 ...

  7. TensorFlow+TVM优化NMT神经机器翻译

    TensorFlow+TVM优化NMT神经机器翻译 背景 神经机器翻译(NMT)是一种自动化的端到端方法,具有克服传统基于短语的翻译系统中的弱点的潜力.本文为全球电子商务部署NMT服务. 目前,将Tr ...

  8. 如何写新的C++ OP

    如何写新的C++ OP 概念简介 简单介绍需要用到基类,详细介绍请参考设计文档. framework::OperatorBase: Operator(简写,Op)基类. framework::OpKe ...

  9. CVPR2020:点云分析中三维图形卷积网络中可变形核的学习

    CVPR2020:点云分析中三维图形卷积网络中可变形核的学习 Convolution in the Cloud: Learning Deformable Kernels in 3D Graph Con ...

  10. Task01:初识数据库

    本章主要讲解数据库安装和数据库基本介绍,考虑易用性及普及度,本课程采取mysql进行教学. 1.1 初识数据库 数据库是将大量数据保存起来,通过计算机加工而成的可以进行高效访问的数据集合.该数据集合称 ...