http://poj.org/problem?id=2074

题目大意:(下面的线段都与x轴平行)给两条线段,一个点在其中一条线段看另一条线段,但是中间有很多线段阻挡视线。求在线段上最大连续区间使得在上面的点都能看见另一条线段。

——————————————

这题的思路很简单,首先根据左端点先排个序,然后找前一条线段的右端点和房子左端点连,后一条线段的左端点和房子右端点连,那么两条连线与路的交的范围即是可行解。

但是debug真的累……好在poj有神犇提供了部分debug数据,经过多次尝试,发现:

1.筛除不在路和房子之间的线段。

2.可行解区域可能超过路。

3.线段可能互相遮挡。

第三条蛮麻烦的,一个简单粗暴的比较方法就是枚举所有线段,判断是否有和线段相交,如果相交则更换为该线段的左/右端点。

#include<cstdio>
#include<queue>
#include<cctype>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
typedef double dl;
const int INF=;
const int N=;
struct point{//既是向量又是点
dl x;
dl y;
};
struct line{
dl x1;
dl x2;
dl y;
}p[N],st,ed;
bool cmp(line a,line b){
return (a.x1<b.x1||(a.x1==b.x1&&a.x2<b.x2)||(a.x1==b.x1&&a.x2==b.x2&&a.y<b.y));
}
inline point getmag(point a,point b){
point s;
s.x=b.x-a.x;s.y=b.y-a.y;
return s;
}
inline int multiX(point a,point b){
return a.x*b.y-b.x*a.y;
}
inline bool check(point a,point b,point c,point d){
dl d1=multiX(getmag(c,d),getmag(c,a))*multiX(getmag(c,d),getmag(c,b));
dl d2=multiX(getmag(a,b),getmag(a,c))*multiX(getmag(a,b),getmag(a,d));
if(d1<=&&d2<=)return ;
return ;
}
inline point intersection(point a,point b,point c,point d){
point s;
dl a1=a.y-b.y,b1=b.x-a.x,c1=a.x*b.y-b.x*a.y;
dl a2=c.y-d.y,b2=d.x-c.x,c2=c.x*d.y-d.x*c.y;
s.x=(c1*b2-c2*b1)/(a2*b1-a1*b2);
s.y=(a2*c1-a1*c2)/(a1*b2-a2*b1);
return s;
}
int n;
int main(){
while(scanf("%lf%lf%lf",&st.x1,&st.x2,&st.y)!=EOF&&st.x1+st.x2+st.y!=){
scanf("%lf%lf%lf",&ed.x1,&ed.x2,&ed.y);
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%lf%lf%lf",&p[i].x1,&p[i].x2,&p[i].y);
sort(p+,p+n+,cmp);
double maxn=,l=,r=;
bool f=;
for(int i=n;i>=;i--){
if(p[i].y>st.y||p[i].y<ed.y)continue;
n=i;
break;
}
for(int i=;i<=n+;i++){
if(p[i].y>st.y||p[i].y<ed.y)continue;
if(!f){
l=ed.x1;
f=;
}
else{
point a,b,c,d;
a.x=ed.x1;a.y=ed.y;
b.x=ed.x2;b.y=ed.y;
c.x=st.x1;c.y=st.y;
d.x=p[i-].x2;d.y=p[i-].y;
for(int j=;j<=n;j++){
if(p[j].y>st.y||p[j].y<ed.y)continue;
if(i-==j)continue;
point e,f;
e.x=p[j].x1;e.y=p[j].y;
f.x=p[j].x2;f.y=p[j].y;
if(check(c,d,e,f)){
d=f;
}
}
l=intersection(a,b,c,d).x;
}
if(i==n+)r=ed.x2;
else{
point a,b,c,d;
a.x=ed.x1;a.y=ed.y;
b.x=ed.x2;b.y=ed.y;
c.x=st.x2;c.y=st.y;
d.x=p[i].x1;d.y=p[i].y;
for(int j=;j<=n;j++){
if(p[j].y>st.y||p[j].y<ed.y)continue;
if(i-==j)continue;
point e,f;
e.x=p[j].x1;e.y=p[j].y;
f.x=p[j].x2;f.y=p[j].y;
if(check(c,d,e,f)){
d=e;
}
}
r=intersection(a,b,c,d).x;
}
if(l<r){
if(l<ed.x1)l=ed.x1;
if(r>ed.x2)r=ed.x2;
maxn=max(maxn,r-l);
}
}
if(maxn==){
puts("No View");
}else{
printf("%.2f\n",maxn);
}
}
return ;
}

POJ2074:Line of Sight——题解的更多相关文章

  1. POJ2074 Line of Sight

    嘟嘟嘟 题意:用一条水平线段表示以栋房子:\((x_0, y_0)(x_0', y_0)\).然后有一条低于房子的水平线段\(l_0\),代表你可以到的位置.接下来输入一个数\(n\),一下\(n\) ...

  2. Poj 2074 Line of Sight

    地址:http://poj.org/problem?id=2074 题目: Line of Sight Time Limit: 1000MS   Memory Limit: 30000K Total ...

  3. unity下的Line of Sight(LOS)的绘制

    先说说什么是Linf of Sight.在很多RTS游戏中,单位与单位之间的视野关系经常会受到障碍物遮挡.Line of Sight指的就是两个物体之间是否没有障碍物遮挡. 比如在dota中,玩家的视 ...

  4. 【转】Using Raycasts and Dynamically Generated Geometry to Create a Line of Sight on Unity3D

    http://www.linkedin.com/pulse/using-raycasts-dynamically-generated-geometry-create-line-thomas José ...

  5. 【转】unity下的Line of Sight(LOS)的绘制

    http://www.cnblogs.com/yangrouchuan/p/6366629.html 先说说什么是Linf of Sight.在很多RTS游戏中,单位与单位之间的视野关系经常会受到障碍 ...

  6. G - Line of Sight

    来源poj2074 An architect is very proud of his new home and wants to be sure it can be seen by people p ...

  7. 简单几何(直线求交点) POJ 2074 Line of Sight

    题目传送门 题意:从一条马路(线段)看对面的房子(线段),问连续的能看到房子全部的最长区间 分析:自己的思路WA了:先对障碍物根据坐标排序,然后在相邻的障碍物的间隔找到区间,这样还要判断是否被其他障碍 ...

  8. poj 2074 Line of Sight 计算几何

    /** 大意:给定一个建筑--水平放置,给定n个障碍物, 给定一条街道,从街道上能看到整个建筑的最长的连续的区域 思路: 分别确定每一个障碍物所确立的盲区,即----建筑物的终点与障碍物的起点的连线, ...

  9. Lintcode395 Coins in a Line II solution 题解

    [题目描述] There are n coins with different value in a line. Two players take turns to take one or two c ...

随机推荐

  1. 友晶Altera Cyclone V GX Starter Kit开发板使用ADC-第一篇

    1. 拿到板子在,做工很好,属于GX系列,GX应该是高速收发器 2. 去探究下GX是什么用途,大约有6个型号,这个板子是5CGXFX5,有77 LE逻辑单元,这个收发器不知道是什么?6个 3.125G ...

  2. 使用materialization

    explain select `countries`.`id` AS `id`,`countries`.`sortname` AS `sortname`,`countries`.`name` AS ` ...

  3. git基础(2)

    三.查看提交历史日志查看·提交历史:git log 命令一个常用的选项是 -p,用来显示每次提交的内容差异. 你也可以加上 -2 来仅显示最近两次提交如果你想看到每次提交的简略的统计信息,你可以使用 ...

  4. 初学Direct X (2)

    初学Direct X (2) 这一次要学习如何现实位图,尽管看过对双缓冲机制还有很多疑问,但是这并不阻碍我对他的入门了解 Direct3D提供了一个双重/后台缓冲区,在调用CreateDevice之时 ...

  5. Unity Lighting - Lighting overview 照明概述

    Lighting overview 照明概述     In order to calculate the shading of a 3D object, Unity needs to know the ...

  6. Java进阶知识点:协变与逆变

    一.背景 要搞懂Java中的协办与逆变,不得不从继承说起,如果没有继承,协变与逆变也天然不存在了. 我们知道,在Java的世界中,存在继承机制.比如MochaCoffee类是Coffee类的派生类,那 ...

  7. 【递归入门】组合+判断素数:dfs(递归)

    题目描述 已知 n 个整数b1,b2,…,bn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和. 例如当 n=4,k=3,4 个整数分别为 3,7,12, ...

  8. Apache——访问控制

    Order 指定执行允许访问规则和拒绝访问规则 Deny 定义拒绝访问列表 Allow 定义允许访问列表 Order allow,deny  先执行允许,再执行拒绝 Order deny,allow ...

  9. python函数学习之装饰器

    装饰器 装饰器的本质是一个python函数,它的作用是在不对原函数做任何修改的同时,给函数添加一定的功能.装饰器的返回值也是一个函数对象. 分类: 1.不带参数的装饰器函数: def wrapper( ...

  10. Django基本目录详解

    1.app是自己建立的一个存放app的文件夹,因为项目大了之后会存在很多app(pycharm生成app方法 Tools-Run manage.py Task-输入startapp app名称) 2. ...