计算几何——The Doors(Dijkstra和线段相交)

原文链接:http://poj.org/problem?id=1556

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

题意翻译:

您将找到通过包含阻塞墙壁的室内的最短路径的长度。造型室的边始终为 x = 0,x = 10,y = 0,y = 10。路径的初始点和最后点始终为 (0、 5) 和 (10, 5)。室内还将有0至18个垂直墙,每面墙有两个门道。下图显示了这样一个腔室,并显示了最小长度的路径。‎

‎输入‎

‎插图室的输入数据如下所示。‎
‎ ‎
‎ ‎
‎ ‎
‎2 ‎
‎4 2 7 8 9 7 3 4.5 6 7 第一行包含内墙‎
‎数量。然后,每个这样的墙都有一条线,包含五个真实数字。第一个数字是墙的 x 坐标 (0 < x & lt; 10),其余四个是墙中门的末端的 y 坐标。墙的 x 坐标是增加顺序的,在每个行中 y 坐标是增加的。输入文件将至少包含一组这样的数据。当墙数为 -1 时,数据结束。‎

‎输出‎

‎输出应包含每个腔室的一行输出。该行应包含小数点后四舍五入到小数点两位的最小路径长度,并始终显示小数点后两个小数位。行不应包含空格。‎

比较难的一道题,需要用Dijkstra求最短路以及建图和线段相交的判断 

#include <iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
typedef double db;
const int INF =0x3f3f3f3f;
const db eps=1e-6;
struct Point{
	db x,y;
	Point(db x=0,db y=0):x(x),y(y){}
}a[100][2][2];
db G[105][105],dis[100];
int visited[100];
int total_id;
int n;
Point s(0,5),e(10,5);
typedef Point Vector;
Vector operator+ (Vector a,Vector b){
	return Vector(a.x+b.x,a.y+b.y);
}
Vector operator- (Vector a,Vector b){
	return Vector(a.x-b.x,a.y-b.y);
}
Vector operator* (Vector a,db p){
	return Vector(a.x*p,a.y*p);
}
Vector operator/ (Vector a,db p){
	return Vector(a.x/p,a.y/p);
}
db Cross(Vector a,Vector b){
	return a.x*b.y-a.y*b.x;
}
int sgn(db x){
	if(fabs(x)<eps) return 0;
	if(x>0) return 1;
	return -1;
} 
db d(Point a,Point b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
    db c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
    db c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
    return (sgn(c1)*sgn(c2) < 0 && sgn(c3)*sgn(c4) < 0);
}
bool judge(int i,int j,Point m,Point n)
{
	for(;i<=j;++i){
		if(!SegmentProperIntersection(a[i][0][0],a[i][0][1],m,n)&&!SegmentProperIntersection(a[i][1][0],a[i][1][1],m,n))
		   return false;
	}
	return true;
}
void Build_Graph()
{
	for(int i = 0;i<n-1;++i){
		for(int j = 0;j<2;++j){
			for(int k=0;k<2;++k){
				int x=i*4+2*j+k+1;
				for(int r=i+1;r<n;++r){
					for(int s=0;s<2;++s){
						for(int t=0;t<2;++t){
							int y=r*4+s*2+t+1;
							if(judge(i+1,r-1,a[i][j][k],a[r][s][t])){
								G[x][y]=d(a[i][j][k],a[r][s][t]);
							} 
						}
					}
				}
			}
		}
	}
}
void Dijkstra()
{
	memset(visited,0,sizeof(visited));
	for(int i = 1;i<=total_id;++i) dis[i]=INF; 
	dis[0]=0;
	for(int i = 1;i<=total_id;++i){
		int minid,minn=INF;
		for(int j = 0;j<=total_id;++j){
			if(dis[j]<minn&&!visited[j]){
				minn=dis[j];
				minid=j;
			}
		}
		visited[minid]=1;
		if(minid==total_id) break; 
		for(int j = 0;j<=total_id;++j){
			if(dis[minid]+G[minid][j]+eps<dis[j]&&!visited[j]&&G[minid][j]>eps){
				dis[j]=dis[minid]+G[minid][j];
			}
		}
	}
} 
int main(int argc, char** argv) {
	while(cin>>n&&n!=-1){
		for(int i = 0;i<n;++i){
			db x,y1,y2,y3,y4;
			cin>>x>>y1>>y2>>y3>>y4;
			a[i][0][0].x=a[i][0][1].x=a[i][1][0].x=a[i][1][1].x=x;
			a[i][0][0].y=y1;
			a[i][0][1].y=y2;
			a[i][1][0].y=y3;
			a[i][1][1].y=y4;
		}
		memset(G,0,sizeof(G));
		total_id=4*n+1;
		if(judge(0,n-1,s,e)){
			printf("%.2f\n",d(s,e));
			continue;
		}
		for(int i = 0;i<n;++i){
			for(int j=0;j<2;++j){
				for(int k=0;k<2;++k){
					int id=4*i+2*j+k+1;
					if(judge(0,i-1,s,a[i][j][k])) G[0][id]=d(s,a[i][j][k]);
					if(judge(i+1,n-1,a[i][j][k],e)) G[id][total_id]=d(a[i][j][k],e);
				}
			}
		} 
		Build_Graph();
		Dijkstra();
		printf("%.2f\n",dis[total_id]);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值