poj1556 The Doors

29 篇文章 0 订阅
18 篇文章 0 订阅

The Doors
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6410 Accepted: 2571

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

题意:给一个10*10的房间内,入口在(0,5)位置,出口在(10,5)的位置,只能沿直线行走,然后给你n堵墙,接下来n行分别表示:x的坐标,y1,y2,y3,y4。具体看图就知道了。然后问从入口到出口的最短距离是多少。

题解:首先我把所有端点和线段记录下来,然后从入口到每个端点能不能直接到达,条件是不能有其他线段和这条路径相交。然后变成了很多边,然后用DP的原理求出入口到每个端点的最小距离,最后求出每个端点到出口最小距离就OK,当然其中还要判断是否被墙挡住了,也就是是否有其他线段与这条路径相交。就这样了。然后C++AC、G++Wrong了。

从这个题看出光会几何或者光会图论是没有用的。还是得全面发展自己。当队友不在自己身边的时候,多学点东西总是好的。加油!↖(^ω^)↗

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
using namespace std;
struct point 
{
	double x,y;
	point (){}
	point (double x,double y):x(x),y(y){}
};
struct line
{
	point a,b;
	line(){};
};
point operator -(point a,point b)
{
	return point(a.x-b.x,a.y-b.y);
}
int n;
double d[222][222];
line l[100][5];
point p[111][5];
double cross(point a,point b)
{
	return a.x*b.y-a.y*b.x;
}
int pd1(point a,point b,point c,point d)
{
	double k1=cross(a-c,b-c);
	double k2=cross(a-d,b-d);
	if (k1*k2<=0 || fabs(k1*k2)<eps) return 1;
	else return 0;
}
int pd(point a,point b,int begin,int end)
{
	for (int i=begin+1;i<end;i++)
		for (int j=1;j<=3;j++)
			if (pd1(a,b,l[i][j].a,l[i][j].b)) return 0;
	return 1;
}
double dis(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void gg()
{
	int i,j,k,k1;
	for (i=1;i<=n;i++)
	{
		for (j=1;j<=4;j++)
		{
			if (pd(p[0][1],p[i][j],0,i))
				d[i][j]=dis(p[0][1],p[i][j]);
			else 
			{
				d[i][j]=500000.0;
				for (k=1;k<i;k++)
					for (k1=1;k1<=4;k1++)
					{
						if (pd(p[k][k1],p[i][j],k,i))
							if (d[k][k1]+dis(p[i][j],p[k][k1])<=d[i][j])
								d[i][j]=d[k][k1]+dis(p[i][j],p[k][k1]);
					}
			}
		}
	}
	double ans=500000.0;
	for (i=1;i<=n;i++)
		for (j=1;j<=4;j++)
			if (pd(p[i][j],p[n+1][1],i,n+1))
				if (d[i][j]+dis(p[i][j],p[n+1][1])<=ans)
					ans=d[i][j]+dis(p[i][j],p[n+1][1]);
	printf("%.2lf\n",ans);
}
int main()
{
	double x,y1,y2,y3,y4;
	while (~scanf("%d",&n) && n!=-1)
	{
		memset(p,0,sizeof(p));
		memset(l,0,sizeof(l));
		p[0][1].x=0;p[0][1].y=5.0;
		p[n+1][1].x=10;p[n+1][1].y=5.0;
		for (int i=1;i<=n;i++)
		{
			scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
			p[i][1].x=x;p[i][1].y=y1;
			p[i][2].x=x;p[i][2].y=y2;
			p[i][3].x=x;p[i][3].y=y3;
			p[i][4].x=x;p[i][4].y=y4;
			l[i][1].a=point(x,0);l[i][1].b=point(x,y1);
			l[i][2].a=point(x,y2);l[i][2].b=point(x,y3);
			l[i][3].a=point(x,y4);l[i][3].b=point(x,10.0);
		}
		memset(d,0,sizeof(d));
		if (pd(p[0][1],p[n+1][1],0,n+1))
		{
			printf("%.2lf\n",dis(p[0][1],p[n+1][1]));
			continue;
		}
		else gg();
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值