POJ1556The Doors【判断线段相交+dijkstra】

 

Language:
The Doors
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7177 Accepted: 2832

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

题意:给一个10*10的正方形房间中间用墙隔开每个墙上有两个门给出门的两个端点坐标求从左边中点走到右边中点所需要的最短路程;

解题思路:列举每个点到它后面所有可直达的点的距离然后dijkstra即可;

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define eps 1e-6
#define inf 0x3f3f3f3f
using namespace std;
int vis[110];
double dist[110];
double map[110][110];
struct point{
	int pos;
	double x,y;
};
struct line{
	point a,b,c,d;
}B[110],A[2];
double MAX(double a,double b){
	return a>b?a:b;
}
double MIN(double a,double b){
	return a<b?a:b;
}
bool judge(int a,int b){        
    if(MIN(A[a].a.x,A[a].b.x)>MAX(A[b].a.x,A[b].b.x)||MIN(A[a].a.y,A[a].b.y)>MAX(A[b].a.y,A[b].b.y)||MIN(A[b].a.x,A[b].b.x)>MAX(A[a].a.x,A[a].b.x)||MIN(A[b].a.y,A[b].b.y)>MAX(A[a].a.y,A[a].b.y))        
    return false;        
    double h,i,j,k;        
    h=(A[a].b.x-A[a].a.x)*(A[b].a.y-A[a].a.y)-(A[a].b.y-A[a].a.y)*(A[b].a.x-A[a].a.x);        
    i=(A[a].b.x-A[a].a.x)*(A[b].b.y-A[a].a.y)-(A[a].b.y-A[a].a.y)*(A[b].b.x-A[a].a.x);        
    j=(A[b].b.x-A[b].a.x)*(A[a].a.y-A[b].a.y)-(A[b].b.y-A[b].a.y)*(A[a].a.x-A[b].a.x);        
    k=(A[b].b.x-A[b].a.x)*(A[a].b.y-A[b].a.y)-(A[b].b.y-A[b].a.y)*(A[a].b.x-A[b].a.x);        
    return h*i<=eps&&j*k<=eps;        
}        
int getnext(int k){
	int u=-1,i,temp=inf;
	for(i=0;i<k;++i){
		if(!vis[i]&&temp>dist[i]){
			temp=dist[i];
			u=i;
		}
	}
	return u;
}
double dijkstra(int k){
	memset(vis,0,sizeof(vis));//竟然把这里忘了wa了三次
	int i,j,u=0;
	dist[0]=0;
	while(u!=-1){
		for(i=0;i<k;++i){
			if(dist[i]>dist[u]+map[u][i])
				dist[i]=dist[u]+map[u][i];
		}
		vis[u]=1;
		if(vis[k-1])return dist[k-1];
		u=getnext(k);
	}
}
int main()
{
	int i,j,n,k;
	while(scanf("%d",&n)){
		if(n==-1)break;
		point N[110];
		double X,y1,y2,y3,y4;
		k=0;N[k].x=0;N[k].y=5;N[k++].pos=0;
		for(i=1;i<=n;++i){
			scanf("%lf%lf%lf%lf%lf",&X,&y1,&y2,&y3,&y4);
			B[i].a.x=B[i].b.x=B[i].c.x=B[i].d.x=X;
			B[i].a.y=y1;B[i].b.y=y2;B[i].c.y=y3;B[i].d.y=y4;
			N[k].x=X;N[k].y=y1;N[k++].pos=i;
			N[k].x=X;N[k].y=y2;N[k++].pos=i;
			N[k].x=X;N[k].y=y3;N[k++].pos=i;
			N[k].x=X;N[k].y=y4;N[k++].pos=i;
		}
		N[k].x=10;N[k].y=5;N[k++].pos=n+1;
		for(i=0;i<=k;++i){
			dist[i]=inf;
			for(j=0;j<=k;++j){
				map[i][j]=inf;
			}
		}
		for(i=0;i<k;++i){
			A[0].a.x=N[i].x;A[0].a.y=N[i].y;
			for(j=i+1;j<k;j++){
				if(N[j].pos==N[i].pos)continue;
				A[0].b.x=N[j].x;A[0].b.y=N[j].y;
				int u;
				for(u=N[i].pos+1;u<N[j].pos;++u){
					A[1].a.x=B[u].a.x;A[1].a.y=0;
					A[1].b.x=B[u].a.x;A[1].b.y=B[u].a.y;
					if(judge(0,1))break;
					A[1].a.x=B[u].b.x;A[1].a.y=B[u].b.y;
					A[1].b.x=B[u].c.x;A[1].b.y=B[u].c.y;
					if(judge(0,1))break;
					A[1].a.x=B[u].d.x;A[1].a.y=B[u].d.y;
					A[1].b.x=B[u].d.x;A[1].b.y=10;
					if(judge(0,1))break;
				}
				if(u>=N[j].pos){
					double ans=sqrt((N[i].x-N[j].x)*(N[i].x-N[j].x)+(N[i].y-N[j].y)*(N[i].y-N[j].y));
					map[i][j]=MIN(map[i][j],ans);
				}
			}
		}
		printf("%.2lf\n",dijkstra(k));
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值