poj 1556 The Doors

本文介绍了一种利用叉乘寻找可行路径并通过迪杰斯特拉算法计算最短路径的方法,解决了一个二维迷宫环境中从起点到终点的问题。该方法考虑了垂直墙壁及门的位置,确保路径的可行性。

The Doors
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9106 Accepted: 3489

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

分析:还是叉乘,将能走的线路都找出来,能走的路径必定是经过那些线段的边界,再用迪杰斯特拉求最短路。


代码如下:


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

int n,cnt,inde;
struct Edge{
	double x1,x2,y1,y2;
}edge[100];	//cnt
double Map[110][110];
struct Data {
	double xi,yi;
}data[100];//inde+1
	int vi[100];
	double dis[100];
double calc(double x1,double y1,double x2,double y2,double x3,double y3)
{
	return (x2-x1)*(y3-y1)-(y2-y1)*(x3-x1);
}
int judge(int a,int b,int x)//判断两点所连线段与其他的都不相交,即题干中两点之间没有障碍物
{
	double ans1=calc(data[a].xi,data[a].yi,data[b].xi,data[b].yi,edge[x].x1,edge[x].y1);
	double ans2=calc(data[a].xi,data[a].yi,data[b].xi,data[b].yi,edge[x].x2,edge[x].y2);
	double ans3=calc(edge[x].x1,edge[x].y1,edge[x].x2,edge[x].y2,data[a].xi,data[a].yi);
	double ans4=calc(edge[x].x1,edge[x].y1,edge[x].x2,edge[x].y2,data[b].xi,data[b].yi);
	if(ans1*ans2<=0 && ans3*ans4<=0)
		return 0;
	return 1;
}
int Check(int a,int b)
{
	if(data[a].xi==data[b].xi)
		return 0;
	for(int i=0;i<cnt;i++)
	{
		if(data[a].xi!=edge[i].x1 && data[a].xi!=edge[i].x2 && data[b].xi!=edge[i].x1 && data[b].xi!=edge[i].x2)
		{
			if(judge(a,b,i)==0)
				return 0;
		}
	}
	return 1;
}

double Distence(int a,int b)
{
	return sqrt((data[a].xi-data[b].xi)*(data[a].xi-data[b].xi)+(data[a].yi-data[b].yi)*(data[a].yi-data[b].yi));
}

void Dj()
{
	int p;
	int n=inde;
	for(int i=0;i<=n;i++)
    {
        dis[i]=Map[0][i];
        vi[i]=0;
    }
    vi[0]=1;
    for(int i=0;i<=n;i++)
    {
        double MIN=0x3f3f3f3f;
        for(int j=0;j<=n;j++)
        {
            if(!vi[j] && dis[j]<MIN)
            {
                MIN=dis[j];
                p=j;
            }
        }
        vi[p]=1;
        for(int j=0;j<=n;j++)
        {
            if(!vi[j] && dis[j]>Map[p][j]+dis[p])
                dis[j]=Map[p][j]+dis[p];
        }
    }
}

int main()
{
	double xy[5];
	while(scanf("%d",&n))
	{
		cnt=0;		inde=1;
		data[0].xi=0;	data[0].yi=5;
		for(int i=0;i<=100;i++)
			for(int j=0;j<=100;j++)
				Map[i][j]=0x3f3f3f3f;
		if(n==-1)
			break;
		for(int j=0;j<n;j++)
		{
			for(int i=0;i<5;i++)
				scanf("%lf",&xy[i]);
			edge[cnt].x1=xy[0];	edge[cnt].y1=0;	edge[cnt].x2=xy[0];	edge[cnt].y2=xy[1];	cnt++;
			edge[cnt].x1=xy[0];	edge[cnt].y1=xy[2];	edge[cnt].x2=xy[0];	edge[cnt].y2=xy[3];	cnt++;
			edge[cnt].x1=xy[0];	edge[cnt].y1=xy[4];	edge[cnt].x2=xy[0];	edge[cnt].y2=10;	cnt++;
			for(int i=1;i<=4;i++)
			{
				data[inde].xi=xy[0]; data[inde].yi=xy[i];
				inde++;
			}
		}
		data[inde].xi=10; data[inde].yi=5;
		for(int i=0;i<inde;i++)
		{
			for(int j=i+1;j<=inde;j++)
			{
				if(Check(i,j))
				{
					Map[i][j]=Map[j][i]=Distence(i,j);
				}
			}
		}
		Dj();
		printf("%.2lf\n",dis[inde]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值