POJ - 1556 The Doors(判断线段与线段相交模板+最短路)

链接:https://cn.vjudge.net/problem/POJ-1556

题意:在一个10*10矩形内,有一些竖着的墙,问从(0,5)到(10,5)的最短距离。

思路:由于墙的个数很少,把不是墙的线段存下来,再把不是墙的线段两端的点存下来。判断线段相交建图,跑最短路。

#include <cstdio>
#include <iostream>
#include <cmath>
#define ll long long
using namespace std;
const double eps = 1e-8;
const int N = 1e2+10;
const double inf = 2e18;
int sgn(double x)
{
	if(fabs(x)<eps) return 0;
	else if(x<0) return -1;
	else return 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;
	}
}s,e;
struct Line
{
	Point s,e;
	Line(){}
	Line(Point ss,Point ee){ s=ss,e=ee;}
}ls[N];
double mp[N][N],dis[N][N];
int n;
double x,y1,y2,y3,y4;
double dist(Point a,Point b)
{
	return sqrt((a-b)*(a-b));
}
//判断线段相交 
bool inter(Line l1,Line l2)
{
	return max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x)
	&& max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x)
	&& max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y)
	&& max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y)
	&& sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s))<=0
	&& sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s))<=0;
}
void buildse()
{
	int temp;
	bool can;
	Point p;
	for(int i=1;i<=(n<<2);i++)
	{
		temp=(i+3)/4;
		can=1;
		if(i&1) p=ls[(i+1)>>1].s;
		else p=ls[(i+1)>>1].e;	
		for(int j=1;j<temp;j++)
			if(!inter(ls[(j<<1)-1],Line(s,p)) && !inter(ls[j<<1],Line(s,p)))		
				can=0;
		if(can) mp[0][i]=mp[i][0]=dist(s,p);
		can=1;
		for(int j=temp+1;j<=n;j++)
			if(!inter(ls[(j<<1)-1],Line(p,e)) && !inter(ls[j<<1],Line(p,e)))		
				can=0;
		if(can) mp[i][(n<<2)|1]=mp[(n<<2)|1][i]=dist(p,e);	
	}		
} 
void build()
{
	int temp1,temp2;
	bool can;
	Point p1,p2;
	for(int i=1;i<=(n<<2);i++)
		for(int j=i+1;j<=(n<<2);j++)
		{
			temp1=(i+3)/4,temp2=(j+3)/4;
			if(i&1) p1=ls[(i+1)>>1].s;
			else p1=ls[(i+1)>>1].e;
			if(j&1) p2=ls[(j+1)>>1].s;
			else p2=ls[(j+1)>>1].e;
			can=1;
			for(int k=temp1+1;k<temp2;k++)
				if(!inter(ls[(k<<1)-1],Line(p1,p2)) && !inter(ls[k<<1],Line(p1,p2)))
					can=0;
			if(can) mp[i][j]=mp[j][i]=dist(p1,p2);
		}
	can=1;
	for(int i=1;i<=n;i++)
		if(!inter(ls[(i<<1)-1],Line(s,e)) && !inter(ls[i<<1],Line(s,e)))
			can=0;
	if(can) mp[0][(n<<2)|1]=mp[(n<<2)|1][0]=10;
}
void floyd()
{
	for(int k=0;k<=((n<<2)|1);k++)
		for(int i=0;i<=((n<<2)|1);i++) 
			for(int j=0;j<=((n<<2)|1);j++)
				mp[i][j]=min(mp[i][k]+mp[k][j],mp[i][j]);
}
int main(void)
{
	s=Point(0,5),e=Point(10,5);
	while(~scanf("%d",&n)&&(n!=-1))
	{
		for(int i=0;i<=((n<<2)|1);i++)
			for(int j=0;j<=((n<<2)|1);j++)
				if(i==j) dis[i][j]=mp[i][j]=0;
				else dis[i][j]=mp[i][j]=inf;
		for(int i=1;i<=n;i++)
		{
			scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
			ls[(i<<1)-1]=Line(Point(x,y1),Point(x,y2));
			ls[i<<1]=Line(Point(x,y3),Point(x,y4));	
		}
		buildse();
		build();
		floyd();
		printf("%.2f\n",mp[0][(n<<2)|1]);
	}
		
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值