这道题目在黑皮书上有。思路很容易就想到,就是枚举每两个端点,判断两个点之间是否与墙相交,不想交的话则将路径距离加入矩阵中,相交的的话则赋值INF,然后就是一个求最短路径的问题,可以用dijkstra算法。
这道题麻烦在建图。
The Doors
Time Limit: 1000MS |
| Memory Limit: 10000K |
Total Submissions: 4176 |
| Accepted: 1742 |
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.
![](https://i-blog.csdnimg.cn/blog_migrate/645e625e62d18e2c5da4d99ab0713357.jpeg)
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.
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
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
#define MAXN 20
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps) //精度
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
const double INF=100000000;
struct point{double x,y;};
struct line{point a,b;};
line l[100];
point p[120];
double maps[120][120];
double dis[120];
int set[120],n,m;
double distances(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double multi(point p0, point p1, point p2)
{
return ( p1.x - p0.x )*( p2.y - p0.y )-( p2.x - p0.x )*( p1.y - p0.y );
}
bool isIntersected(point s1,point e1, point s2,point e2)
{
return (max(s1.x,e1.x) >= min(s2.x,e2.x)) &&
(max(s2.x,e2.x) >= min(s1.x,e1.x)) &&
(max(s1.y,e1.y) >= min(s2.y,e2.y)) &&
(max(s2.y,e2.y) >= min(s1.y,e1.y)) &&
(multi(s1,s2,e1)*multi(s1,e1,e2)>0)&&
(multi(s2,s1,e2)*multi(s2,e2,e1)>0);
}
void init() //初始化,建图
{
p[0].x=0;
p[0].y=5;
for(int i=1;i<=n;i++)
{
double x;
cin>>x;
cin>>p[i].y>>p[i+n].y>>p[i+n*2].y>>p[i+n*3].y;
p[i].x=p[i+n].x=p[i+n*2].x=p[i+n*3].x=x;
l[i].a.x=x;
l[i].a.y=0;
l[i].b=p[i];
l[i+n].a=p[i+n];
l[i+n].b=p[i+n*2];
l[i+2*n].a=p[i+n*3];
l[i+2*n].b.x=x;
l[i+2*n].b.y=10;
}
p[n*4+1].x=10;
p[n*4+1].y=5;
bool cs=true;
for(int i=0;i<=4*n+1;i++)
{
for(int j=0;j<=4*n+1;j++)
{
cs=true;
for(int k=1;k<=3*n;k++)
{
if(isIntersected(p[i],p[j],l[k].a,l[k].b))
{
cs=false;
break;
}
}
if(cs)
{
maps[i][j]=distances(p[i],p[j]);
}
else
{
maps[i][j]=INF;
}
}
}
}
double dijstra(int v0)
{
for(int i=0;i<=4*n+1;i++)
{
set[i]=0;
dis[i]=maps[v0][i];
}
set[v0]=1;
dis[v0]=0;
for(int i=1;i<=4*n+1;i++)
{
double mins=INF;
int u=v0;
for(int j=0;j<=4*n+1;j++)
{
if(!set[j] && dis[j]<mins)
{
u=j;
mins=dis[j];
}
}
set[u]=1;
for(int j=0;j<=4*n+1;j++)
{
if(!set[j] && maps[u][j]<INF && dis[u]+maps[u][j]<dis[j])
{
dis[j]=dis[u]+maps[u][j];
}
}
}
return dis[4*n+1];
}
int main()
{
while(cin>>n&& n!=-1)
{
init();
cout<<setprecision(2)<<setiosflags(ios::fixed)<<dijstra(0)<<endl;
}
return 0;
}