POJ 2074 | 线段相交

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-8
using namespace std;
bool dcmp(double x,double y)
{
    if (fabs(x-y)>eps) return 1;
    return 0;
}
struct point
{
    double x,y;
    point () {};
    point (double _x,double _y)
	{
	    x=_x,y=_y;
	}
    point operator + (const point &a)const
	{
	    return point(x+a.x,y+a.y);
	}
    point operator - (const point &a) const
	{
	    return point(x-a.x,y-a.y);
	}
    double operator * (const point &a)const
	{
	    return x*a.y-a.x*y;
	}
    double dot (const point &a,const point &b)
	{
	    return a.x*b.x+a.y*b.y;
	}
    bool operator < (const point &a)const
	{
	    return x<a.x;
	}
    bool operator == (const point &a)const
	{
	    return dcmp(x,a.x) && dcmp(y,a.y);
	}
};
struct range
{
    double x1,x2,y;
    bool operator < (const range &a)const
	{
	    return x1<a.x1;
	}
}house,block[1010],road;
bool cmp(range a,range b)
{
    return a.x1<b.x1;
}
double findinter(point p1,point p2,point p3,point p4){
    double ans=((p1.x-p3.x)*(p3.y-p4.y)-(p1.y-p3.y)*(p3.x-p4.x))/((p1.x-p2.x)*(p3.y-p4.y)-(p1.y-p2.y)*(p3.x-p4.x));
   return  p1.x+(p2.x-p1.x)*ans;
}
double getIntersect(point a,point b,point c,point d)
{
    double A1=b.y-a.y,B1=a.x-b.x,C1=(b.x-a.x)*a.y-(b.y-a.y)*a.x;
    double A2=d.y-c.y,B2=c.x-d.x,C2=(d.x-c.x)*c.y-(d.y-c.y)*c.x;
    return (C2*B1-C1*B2)/(A1*B2-A2*B1);
}
int n;
int main()
{
//    freopen("1.out","w",stdout);
    while (scanf("%lf%lf%lf",&house.x1,&house.x2,&house.y)!=0)
    {
	if (house.x1==0 && house.x2==0 && house.y==0) break;
	scanf("%lf%lf%lf",&road.x1,&road.x2,&road.y);
	scanf("%d",&n);
	for (int i=0;i<n;i++)
	    scanf("%lf%lf%lf",&block[i].x1,&block[i].x2,&block[i].y);
	sort(block,block+n,cmp);
	point p1,p2,p3,p4;
	p3=point(road.x1,road.y);
	p4=point(road.x2,road.y);
	double ans=-1,Lmax=-1.0;
	for (int i=0;i<=n;i++)
	{
	    double l,r;
	      if (block[i].y>=house.y) continue;
	    if (i==0)
		l=road.x1;
	    else
	    {
		p1=point(house.x1,house.y);
		p2=point(block[i-1].x2,block[i-1].y);
		l=findinter(p1,p2,p3,p4);
	    }
	    if (i==n)
		r=road.x2;
	    else
	    {
		p1=point(house.x2,house.y);
		p2=point(block[i].x1,block[i].y);
		r=findinter(p1,p2,p3,p4);
	    }
	    if (l<road.x1) l=road.x1;
	    if (r>road.x2) r=road.x2;
	    if (l<Lmax) l=Lmax;
	    Lmax=max(Lmax,l);
	    ans=max(ans,r-l);
	}
	if (ans<=0) puts("No View");
	else printf("%.2f\n",ans);
    }
    return 0;
}
时间: 2024-10-13 03:57:33

POJ 2074 | 线段相交的相关文章

poj 1410 线段相交判断

http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11329   Accepted: 2978 Description You are to write a program that has to decide whether a given line segment intersects a given rectangle. An ex

poj 1066 线段相交

链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5431   Accepted: 2246 Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid

Line of Sight POJ - 2074 (线段覆盖 )

Line of Sight POJ - 2074 题目链接:https://vjudge.net/problem/POJ-2074 题意:有房子属性线和障碍物,要求你通过属性线能够看到完整房子的最大属性上的距离 思路: 其实将房子右端点和障碍物左端连线,房子左端点和障碍物右端点连线在属性线上的投影部分就是看不到房子的区域,即图中地下红色部分,那么所求就是途中属性线剩下的蓝色大括号,求其最大值,求解过程中还要注意线段的合并,如果没有障碍物那么就输出属性线的长度就行,如果有一个投影线段完全覆盖了属性

poj 1269 线段相交/平行

模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = 1 #include <cmath> 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 7 #define eps 1e-8 8 #define PI acos(-1.0)//3.14159265358979323846 9 //判断一个数是否为0,是则返回true,否

poj 2653 线段相交

题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说了没被压住的线段不超过1000个,所以不会爆 1 #include<math.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 bool ans[100100]; 6 int n; 7 double X1,X2,Y1,Y2;

Pipe - POJ 1039(线段相交交点)

题目大意:有一个不反光并且不透光的管道,现在有一束光线从最左端进入,问能达到的最右端是多少,输出x坐标. 分析:刚开始做是直接枚举两个点然后和管道进行相交查询,不过这样做需要考虑的太多,细节不容易掌控.后来发现其实只需要对接口进行一下相交查询就简单多了,因为只需要考虑能不能通过每个截口就可以了,而且这样做的好处还有没有平行线和重叠线的情况,因为所有的截口都是垂直于x轴的,换一种想法海阔太空啊. 代码如下: =============================================

Pick-up sticks - POJ 2653 (线段相交)

题目大意:有一个木棒,按照顺序摆放,求出去上面没有被别的木棍压着的木棍..... 分析:可以维护一个队列,如果木棍没有被压着就入队列,如果判断被压着,就让那个压着的出队列,最后把这个木棍放进队列,不过速度并不快,枚举才是最快的......据说是任意时刻没有超过1000个top sticks.....很难注意到. 代码如下: =======================================================================================

The Doors - POJ 1556 (线段相交)

题目大意:有一个房间(左上角(0,10),右下角(10,0)),然后房间里有N面墙,每面墙上都有两个门,求出来从初始点(0,5),到达终点(10,5)的最短距离. 分析:很明显根据两点之间直线最短,所以所走的路线一定是点之间的连线,只需要判断一下这两点间知否有墙即可. 代码如下: =============================================================================================================

poj 3304 直线与线段相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12161   Accepted: 3847 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments