题目链接
题目
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
题目大意:
给你一个矩形区域,这个矩形区域有很多段,每段有两个门(空代表门,实代表墙)问你从起点(0,5)到终点(10,5)最短路径长度。
思路
先判断任意两点之间的距离能不能到达,能到达就赋值为两点距离,不能到达就赋值为无穷大,然后floyd找到最短路就是答案。
这里面判断是最麻烦的,当我要从num1点到num2点的时候,要判断在这两个距离之间的每段是不是都没有阻挡,要是存在一个墙挡住了,就不能到达,判断是不是有墙阻挡我们可以用跨立实验,也就是相互叉积之积小于0。
AC
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
using namespace std;
int n;
double eps=1e-8;
double a[20][5];
double e[120][120];
struct zxc
{
double x,y;
} s[120];
double chaji(double x1,double y1,double x2,double y2)
{
return (x1*y2-y1*x2);
}
int ju(int num1,int num2)
{
int flog=0;
int t1,t2;
double x1=s[num2].x-s[num1].x;
double y1=s[num2].y-s[num1].y;
if(num1==0)
{
t2=(num2-1)/4+1;
if(t2==1)
{
return 1;
}
for(int i=1; i<t2; i++)
{
double x2,y2,x3,y3;
x2=a[i][0]-s[num1].x;
y2=0-s[num1].y;
x3=a[i][0]-s[num1].x;
y3=a[i][1]-s[num1].y;
if(chaji(x1,y1,x2,y2)*chaji(x1,y1,x3,y3)<0)
{
return 0;
}
y2=a[i][2]-s[num1].y;
y3=a[i][3]-s[num1].y;
if(chaji(x1,y1,x2,y2)*chaji(x1,y1,x3,y3)<0)
{
return 0;
}
y2=a[i][4]-s[num1].y;
y3=10-s[num1].y;
if(chaji(x1,y1,x2,y2)*chaji(x1,y1,x3,y3)<0)
{
return 0;
}
}
}
else
{
t1=(num1-1)/4+1;
t2=(num2-1)/4+1;
if(t2==t1+1)
{
return 1;
}
for(int i=t1+1; i<t2; i++)
{
double x2,y2,x3,y3;
x2=a[i][0]-s[num1].x;
y2=0-s[num1].y;
x3=a[i][0]-s[num1].x;
y3=a[i][1]-s[num1].y;
if(chaji(x1,y1,x2,y2)*chaji(x1,y1,x3,y3)<0)
{
return 0;
}
y2=a[i][2]-s[num1].y;
y3=a[i][3]-s[num1].y;
if(chaji(x1,y1,x2,y2)*chaji(x1,y1,x3,y3)<0)
{
return 0;
}
y2=a[i][4]-s[num1].y;
y3=10-s[num1].y;
if(chaji(x1,y1,x2,y2)*chaji(x1,y1,x3,y3)<0)
{
return 0;
}
}
}
return 1;
}
double juli(zxc q,zxc w)
{
return sqrt((q.x-w.x)*(q.x-w.x)+(q.y-w.y)*(q.y-w.y));
}
int main()
{
while(~scanf("%d",&n))
{
if(n==-1)
{
break;
}
s[0].x=0,s[0].y=5;
int tot=1;
for(int i=1; i<=n; i++)
{
scanf("%lf%lf%lf%lf%lf",&a[i][0],&a[i][1],&a[i][2],&a[i][3],&a[i][4]);
s[tot].x=a[i][0];
s[tot++].y=a[i][1];
s[tot].x=a[i][0];
s[tot++].y=a[i][2];
s[tot].x=a[i][0];
s[tot++].y=a[i][3];
s[tot].x=a[i][0];
s[tot++].y=a[i][4];
}
s[tot].x=10,s[tot].y=5;
for(int i=0;i<=tot;i++)
{
for(int j=0;j<=tot;j++)
{
e[i][j]=100000000;
}
}
for(int i=1; i<=tot; i++)
{
if(ju(0,i))
{
e[0][i]=e[i][0]=juli(s[0],s[i]);
}
else
{
e[0][i]=e[i][0]=100000000;
}
e[i][i]=100000000;
}
for(int i=1; i<=tot; i++)
{
for(int j=((i-1)/4+1)*4+1; j<=tot; j++)
{
if(ju(i,j))
{
e[i][j]=e[j][i]=juli(s[i],s[j]);
}
else
{
e[i][j]=e[j][i]=100000000;
}
}
}
for(int i=0; i<=tot; i++)
for(int j=0; j<=tot; j++)
for(int k=0; k<=tot; k++)
e[i][j]=min(e[i][j],e[i][k]+e[k][j]);
printf("%.2lf\n",e[0][tot]);
}
return 0;
}