现在的位置: 首页 > 综合 > 正文

POJ 1556 The Doors

2013年02月27日 ⁄ 综合 ⁄ 共 3242字 ⁄ 字号 评论关闭
The Doors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5639   Accepted: 2268

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.

2
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

Mid-Central USA 1996
解题思路:开始做计算几何了,来写一些题解当做以后用的模板,这一题先判断直线与线段是否相交,建图后做最短路即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#define Max 105
#define eps 1e-8
#define Inf 0x7fffffff
using namespace std;
int head[Max],cnt;
struct
{
	int e;
	double w;
	int next;
}edge[Max*Max];
struct Point 
{
	double x,y;
}point[Max];
struct Seg
{
	Point a,b;
}seg[Max];
void add(int s,int e,double w)
{
	edge[cnt].e=e;
	edge[cnt].w=w;
	edge[cnt].next=head[s];
	head[s]=cnt++;
}
double distance(double x1,double y1,double x2,double y2)
{
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int dblcmp(double d)  
{  
        if(fabs(d)<eps) return 0;  
        return d>0?1:-1;  
}  
double det(double x1,double y1,double x2,double y2)//area2  
{  
        return x1*y2-x2*y1;  
}  
double cross(Point a,Point b,Point c)  
{  
        return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);  
}  
int segcross(Point a,Point b,Point c,Point d)  
{  
        return (dblcmp(cross(a,c,d))^dblcmp(cross(b,c,d)))==-2&&  
                (dblcmp(cross(c,a,b))^dblcmp(cross(d,a,b)))==-2;  
}  
double spfa(int n)
{
	int i,st,ed;
	bool visit[Max];
	double dis[Max];
	queue<int> q;
	memset(visit,false,sizeof(visit));
	visit[0]=true;
	for(i=1;i<=n;i++)
		dis[i]=Inf*1.0;
	dis[0]=0;
	q.push(0);
	while(!q.empty())
	{
		st=q.front();
		q.pop();
		visit[st]=false;
		for(i=head[st];i!=-1;i=edge[i].next)
		{
			ed=edge[i].e;
			if(dis[ed]>dis[st]+edge[i].w)
			{
				dis[ed]=dis[st]+edge[i].w;
				if(!visit[ed])
				{
					visit[ed]=true;
					q.push(ed);
				}
			}
		}
	}
	return dis[n];
}
int main()
{
	int i,j,k,n,t,p;
	bool flag;
	double x,y[6];
	while(scanf("%d",&n),n!=-1)
	{
		cnt=p=0;
		memset(head,-1,sizeof(head));
		t=1;
		point[0].x=0.0;point[0].y=5.0;
        for(i=1;i<=n;i++)
		{
			scanf("%lf",&x);
			y[0]=0.0;y[5]=10.0;
			for(j=1;j<=4;j++)
			{
				scanf("%lf",&y[j]);
				point[++p].x=x;
				point[p].y=y[j];
			}
			seg[t].a.x=x;seg[t].a.y=y[0];
			seg[t].b.x=x;seg[t++].b.y=y[1];
			seg[t].a.x=x;seg[t].a.y=y[2];
			seg[t].b.x=x;seg[t++].b.y=y[3];
			seg[t].a.x=x;seg[t].a.y=y[4];
			seg[t].b.x=x;seg[t++].b.y=y[5];
		}
		point[++p].x=10.0;point[p].y=5.0;
		for(i=0;i<=p;i++)
			for(j=i+1;j<=p;j++)
			{
				flag=true;
				for(k=1;k<t;k++)
				{
					if(point[i].x!=seg[k].a.x&&point[j].x!=seg[k].a.x&&segcross(point[i],point[j],seg[k].a,seg[k].b))
					{
						flag=false;
						break;
					}
				}
				if(flag)
					add(i,j,distance(point[i].x,point[i].y,point[j].x,point[j].y));
			}
		printf("%.2f\n",spfa(p));
	}
	return 0;
}

抱歉!评论已关闭.