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. ipmitool使用手册(20200401)

    ipmitool使用手册原创xinqidian_xiao 最后发布于2018-07-05 12:15:47 阅读数 17579 收藏展开一.查找安装包 查看ipmitool属于哪个安装包 #yum p ...

  2. Samba服务配置及配置文件说明

    前言 1.配置Samba服务为什么要关闭防火墙(firewalld)和Selinux? 在linux操作系统中默认开启了防火墙,Selinux也处于启动状态,一般状态为enforing:所以,在我们搭 ...

  3. 运维常用shell脚本二(压缩文件、过滤不需要的文件、检测进程)

    一.压缩指定目录下的文件并删除原文件 #!/bin/bashZIP_DAY=7 function zip { local dir=$1 if [ -d $dir ];then local file_n ...

  4. 速度竟差9倍!6款32GB USB3.0优盘横评

    速度竟差9倍!6款32GB USB3.0优盘横评 2014-08-22 05:04:00  [  中关村在线 原创  ]   作者: 蒋丽 |  责编:孙玉亮 收藏文章 分页阅读 分享到 评论(90) ...

  5. stm32.cube(一)——系统架构及目录结构

    一.前言 Arm的应用场景往往比51单片机复杂得多,如果一个高级应用的开发需要连底层的结构性代码都要重构,那么在成本和研发周期上就会面临巨大的风险.为了简化编码过程,芯片厂商经常会提供一些板卡级支持的 ...

  6. 基于python内置方法进行代码混淆

    0x00 动态加载模块 在python脚本中,直接使用import os.import subprocess或from os import system这种方法很容易被规则检测,即使使用其它执行命令的 ...

  7. IDEA 通过ctrl+滚轮缩放字体大小

    能用图解决的问题,尽量简单粗暴通俗易懂 1.第一种方式 2.第二种方式

  8. oracle实现通过logminer实现日志抓取分析

    场景:现场库到前置库. 思考:使用触发器? 1.侵入性解决方案 2.需要时各种配置,不需要时又是各种配置 Change Data Capture:捕捉变化的数据,通过日志监测并捕获数据库的变动(包括数 ...

  9. Python+Selenium学习笔记16 - unittest单元测试框架

    unittest单元测试框架包括 Test Case,  Test Suite, Test Runner, Test Fixture Test Cases 组成Test Suite, Test Run ...

  10. 常用正则表达式RE(慕课网_Meshare_huang)

    import re str1 = 'imooc python' # str1.find('l1') 输出: -1 # str1.find('imooc') 0 # str1.startswith('i ...