The Doors
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4278 | Accepted: 1759 |
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.
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
Source
题意:给你起点和终点,还有一些线段,要求在不穿过线段的情况下,找到一条从起点到终点的最短路
分析:枚举每个点判断这条路径是否可行,可行的话就建边,之后就最短路,关键是建边时很复杂,不过细心写还是可以搞定,很久没1Y了啊 T_T 这题没有新的模板,算是前面模板的应用吧,还有代码好长啊= =
代码:
#include<cmath>
#include<cstdio>
#include<iostream>
using namespace std;
const int mm=222;
typedef double mType;
struct Tpoint
{
mType x,y;
Tpoint(){}
Tpoint(mType _x,mType _y):x(_x),y(_y){}
};
struct Tsegment
{
Tpoint start,end;
Tsegment(){}
Tsegment(Tpoint _start,Tpoint _end):start(_start),end(_end){}
Tsegment(mType sx,mType sy,mType ex,mType ey):start(sx,sy),end(ex,ey){}
};
Tpoint MakeVector(Tpoint P,Tpoint Q)
{
return Tpoint(Q.x-P.x,Q.y-P.y);
}
mType CrossProduct(Tpoint P,Tpoint Q)
{
return P.x*Q.y-P.y*Q.x;
}
mType MultiCross(Tpoint P,Tpoint Q,Tpoint R)
{
return CrossProduct(MakeVector(Q,P),MakeVector(Q,R));
}
bool IsIntersect(Tsegment P,Tsegment Q)
{
if(max(P.start.x,P.end.x)<min(Q.start.x,Q.start.x)||max(Q.start.x,Q.end.x)<min(P.start.x,P.end.x)||
max(P.start.y,P.end.y)<min(Q.start.y,Q.start.y)||max(Q.start.y,Q.end.y)<min(P.start.y,P.end.y))return 0;
return (MultiCross(P.end,P.start,Q.start)*MultiCross(P.end,P.start,Q.end)<=0&&
MultiCross(Q.end,Q.start,P.start)*MultiCross(Q.end,Q.start,P.end)<=0);
}
mType d[mm][mm],dis[mm],x[mm],y[mm][4];
bool vis[mm];
Tsegment g[mm];
int i,j,k,l,n,m;
bool ok(mType sx,mType sy,mType ex,mType ey,int a,int b)
{
Tsegment tmp=Tsegment(Tpoint(sx,sy),Tpoint(ex,ey));
for(int i=0;i<m;++i)
if(i!=a&&i!=b&&IsIntersect(tmp,g[i]))return 0;
return 1;
}
mType GetDis(mType sx,mType sy,mType ex,mType ey)
{
return sqrt((sx-ex)*(sx-ex)+(sy-ey)*(sy-ey));
}
void test()
{
if(IsIntersect(Tsegment(0,0,1,1),Tsegment(0,1,0.5,0.50001)))puts("They are intersect!!");
else puts("Not intersect!!");
}
int main()
{
//test();
mType tmp;
while(scanf("%d",&n),n!=-1)
{
for(m=i=0;i<n;++i)
{
scanf("%lf%lf%lf%lf%lf",&x[i],&y[i][0],&y[i][1],&y[i][2],&y[i][3]);
g[m++]=Tsegment(x[i],0,x[i],y[i][0]);
g[m++]=Tsegment(x[i],y[i][1],x[i],y[i][2]);
g[m++]=Tsegment(x[i],y[i][3],x[i],10);
}
if(ok(0,5,10,5,-1,-1))puts("10.00");
else
{
for(i=0;i<n*4+2;++i)
for(j=0;j<n*4+2;++j)
d[i][j]=1e20;
for(i=0;i<n;++i)
for(j=0;j<4;++j)
{
if(ok(0,5,x[i],y[i][j],-1,i*3+((j+1)>>1)))d[n*4][i*4+j]=GetDis(0,5,x[i],y[i][j]);
if(ok(10,5,x[i],y[i][j],-1,i*3+((j+1)>>1)))d[n*4+1][i*4+j]=GetDis(10,5,x[i],y[i][j]);
}
for(i=0;i<n;++i)
for(k=i+1;k<n;++k)
for(j=0;j<4;++j)
for(l=0;l<4;++l)
if(ok(x[i],y[i][j],x[k],y[k][l],i*3+((j+1)>>1),k*3+((l+1)>>1)))
d[i*4+j][k*4+l]=GetDis(x[i],y[i][j],x[k],y[k][l]);
for(i=0;i<n*4+2;++i)
for(j=i+1;j<n*4+2;++j)
d[i][j]=d[j][i]=min(d[i][j],d[j][i]);
for(i=0;i<n*4+2;++i)dis[i]=1e20,vis[i]=0;
dis[4*n]=0;
for(i=0;i<n*4+2;++i)
{
tmp=1e20;
for(j=0;j<n*4+2;++j)
if(!vis[j]&&dis[j]<tmp)
{
tmp=dis[j];
k=j;
}
vis[k]=1;
if(k>4*n)break;
for(j=0;j<n*4+2;++j)
if(!vis[j]&&dis[j]>tmp+d[k][j])
dis[j]=tmp+d[k][j];
}
printf("%.2lf\n",dis[4*n+1]);
}
}
return 0;
}
代码: