The Doors

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HYNU_zhizuzhe/article/details/44833147

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

Source

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;

#define Min(a,b) (a<b ? a:b)

const double eps=1e-6;
int dcmp(double x)
{
	if(fabs(x)<eps) return 0;
	else 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);}
	point operator -(const point &b) const {return point(x-b.x,y-b.y);}
	point operator *(const double &b) const {return point(x*b,y*b);}
	point operator /(const double &b) const {return point(x/b,y/b);}
	double norm() {return sqrt(x*x+y*y);}
}p[20][6];

double det(const point &a,const point &b) {return a.x*b.y-a.y*b.x;}

bool CrossLine(point s1,point t1,point s2,point t2) //判断两直线相交 
{
	if(dcmp(det(s1-t1,s2-t1))*dcmp(det(s1-t1,t2-t1))>=0) return false;
	if(dcmp(det(s2-t2,s1-t2))*dcmp(det(s2-t2,t1-t2))>=0) return false;
	return true;
}

double dfs(point s,int n,point t,int m)
{
	if(m==n) return (s-t).norm();
	double res=1e9;
	for(int i=n;i>m;i--)
	{
		for(int j=0;j<6;j+=2)
		{
			if(CrossLine(s,t,p[i][j],p[i][j+1]))
			{
				double ans;
				if(j==0) ans=dfs(p[i][j+1],i-1,t,m)+dfs(s,n,p[i][j+1],i);
				else if(j==4) ans=dfs(p[i][j],i-1,t,m)+dfs(s,n,p[i][j],i);
				else ans=Min(dfs(p[i][j],i-1,t,m)+dfs(s,n,p[i][j],i),dfs(p[i][j+1],i-1,t,m)+dfs(s,n,p[i][j+1],i));
			    res=Min(res,ans);
			}
		}
	}
	if(res==1e9) return (s-t).norm();
	return res;
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF&&n!=-1)
	{
		for(int i=0;i<n;i++) {
			scanf("%lf%lf%lf%lf%lf",&p[i][0].x,&p[i][1].y,&p[i][2].y,&p[i][3].y,&p[i][4].y);
			p[i][1].x=p[i][2].x=p[i][3].x=p[i][4].x=p[i][5].x=p[i][0].x;
			p[i][0].y=0.0; p[i][5].y=10.0;
		}
		printf("%.2lf\n",dfs(point(10.0,5.0),n-1,point(0.0,5.0),-1));
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/HYNU_zhizuzhe/article/details/44833147