小白算法练习 计算几何 线段与线段相交 最短路

The Doors

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9974 Accepted: 3730

Description

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length. 

Input

The input data for the illustrated chamber would appear as follows. 


4 2 7 8 9 
7 3 4.5 6 7 

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1. 

Output

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input

1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1

Sample Output

10.00
10.06

思路:如图所示,要判断BG是否有墙存在,没有墙距离就好算了(线段与线段相交),有墙的话就找四个窗的顶点建边,因为你不知道下一堵墙的窗在什么地方,代码以后再写。。

关于线段与线段的相交除了要用叉积之外还要满足以下四个条件!!!

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
using namespace std;
const int maxm=111111;
const int esp=0x3f3f3f;
int T,index=1,lndex=1; 
struct Point{
	int t;
	double x,y;
	double dis;
	Point(int xx,int yy):x(xx),y(yy){};
	Point(){};
}p[maxm];
struct Line{
	Point h,t;
}l[maxm]; 
vector<Point>vec[maxm]; 
queue<int>que;
int vis[maxm],out[maxm];
double dist[maxm];
int cross(Point p1,Point p2,Point p3){ //叉积 
	double ans=((p1.x-p3.x)*(p1.y-p2.y)-(p1.x-p2.x)*(p1.y-p3.y));
	if(ans==0) return 0;
	else 
	{
		if(ans<0) return -1;
		else return 1;
	}
}
int ispara(Point A,Point B,Point C,Point D){  //线段与线段是否相交 
	double hhigh=max(A.y,B.y);double hlow=min(A.y,B.y);double hright=max(A.x,B.x);double hleft=min(A.x,B.x);
	double thigh=max(C.y,D.y);double tlow=min(C.y,D.y);double tright=max(C.x,D.x);double tleft=min(C.x,D.x);
	if(hlow>thigh || hhigh<tlow || hright<tleft || hleft>tright) return 0;//不相交 
	if(cross(C,A,B)*cross(D,A,B)>=0) return 0;
	if(cross(A,C,D)*cross(B,C,D)>=0) return 0;
	return 1; //相交 
}
double diss(Point A,Point B){
	return sqrt( (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y) );
}
double calc(Point A,Point B){
	for(int i=1;i<=lndex;i++)
	{
		if(ispara(A,B,l[i].h,l[i].t)==1)
		{
			return 0;
		}
	}
	return diss(A,B);
}
void initq(double L)
{
	Point ph,pt;
	ph.x=L;ph.y=0;
	pt.x=L;pt.y=p[index-4].y;
	l[lndex].h=ph;
	l[lndex++].t=pt;
	ph.x=L;ph.y=p[index-3].y;
	pt.x=L;pt.y=p[index-2].y;
	l[lndex].h=ph;
	l[lndex++].t=pt;
	ph.x=L;ph.y=p[index-1].y;
	pt.x=L;pt.y=10;
	l[lndex].h=ph;
	l[lndex++].t=pt; 	
}
void initb()
{
	for(int i=1;i<=index;i++)
	{
		p[i].dis=0.0;
		for(int j=1;j<=index;j++)
		{
			double len=calc(p[i],p[j]); // 0代表两点之间有交点  // >0 代表两点之间的距离 
			if(len==(double)(0)) continue;
			else
			{
				Point pp;
				pp.x=p[j].x;
				pp.y=p[j].y;
				pp.t=j;
				pp.dis=len;
				vec[i].push_back(pp);
				pp.x=p[i].x;
				pp.y=p[i].y;
				pp.t=i;
				vec[j].push_back(pp);
			} 
		}
	}
}
void spfa(int s=1){
	memset(out,0,sizeof(out));
	for(int i=1;i<=index;i++)
	{
		dist[i]=esp; 
	}
	que.push(s);
	vis[s]=true;
	dist[s]=0;
	while(!que.empty())
	{
		int top=que.front();
		que.pop();
		vis[top]=false;
		out[top]++;
		if(out[top]>index) return;
		int size=vec[top].size();
		for(int i=0;i<size;i++)
		{
			int v=vec[top][i].t;
			double w=vec[top][i].dis;
			if(dist[v]>dist[top]+w)
			{
				dist[v]=dist[top]+w;
				if(!vis[v])
				{
					que.push(v);
					vis[v]=true;
				}
			}
		}
	}
	return;
}
int main()
{
	while(scanf("%d",&T)!=EOF && T!=-1)
	{
		//init() //初始化点 
		memset(dist,0,sizeof(dist));
		memset(vis,0,sizeof(vis));
		memset(out,0,sizeof(out));
		while (!que.empty()) que.pop();
		for(int i=0;i<=index;i++)
		{
			vec[i].clear();
		}
		index=1;lndex=1;
		p[index].x=0;
		p[index++].y=5.0;
		for(int i=1;i<=T;i++)
		{
			double L;scanf("%lf",&L); 
			for(int j=1;j<=4;j++)
			{
				double H;scanf("%lf",&H);  
				//记录所有的点 
				p[index].x=L;
				p[index++].y=H;	  	
			} 
			//记录墙所在的边 //以后判断是否相交 
			initq(L);
		}
		p[index].x=10;
		p[index].y=5.0;
		//init() //初始化边 
		initb(); 
		spfa();
		if(T==0) dist[index]=10.00;
		printf("%.2lf\n",dist[index]);
	}
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值