The Doors
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5210 | Accepted: 2124 |
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.
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.
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
总共4*n+1个点个点。
根据线段交去判断会不会冲突,可以直接连的就建立一条路径。
然后求最短路。
因为点很小,所以无论哪种最短路算法都可使用。
/************************************************************
* Author : kuangbin
* Email : kuangbin2009@126.com
* Last modified : 2013-07-14 10:47
* Filename : POJ1556TheDoors.cpp
* Description :
* *********************************************************/ #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h> using namespace std; const double eps = 1e-;
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < ) return -;
else return ;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
};
//判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= &&
sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= ;
}
double dist(Point a,Point b)
{
return sqrt((b-a)*(b-a));
}
const int MAXN = ;
Line line[MAXN];
double dis[MAXN][MAXN];
const double INF = 1e20;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
double x,y1,y2,y3,y4;
while(scanf("%d",&n) == )
{
if(n == -) break;
for(int i = ;i <= n;i++)
{
scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
line[*i-] = Line(Point(x,y1),Point(x,y2));
line[*i] = Line(Point(x,y3),Point(x,y4));
}
for(int i = ;i <= *n+;i++)
for(int j = ;j <= *n+;j++)
{
if(i == j)dis[i][j] = ;
else dis[i][j] = INF;
}
for(int i = ;i <= *n;i++)
{
int lid = (i+)/;
bool flag = true;
Point tmp;
if(i&)tmp = line[(i+)/].s;
else tmp = line[(i+)/].e;
for(int j = ;j < lid;j++)
if(inter(line[*j-],Line(Point(,),tmp)) == false
&& inter(line[*j],Line(Point(,),tmp)) == false)
flag = false;
if(flag)dis[][i] =dis[i][] = dist(Point(,),tmp);
flag = true;
for(int j = lid+;j <= n;j++)
if(inter(line[*j-],Line(Point(,),tmp)) == false
&& inter(line[*j],Line(Point(,),tmp)) == false)
flag = false;
if(flag)dis[i][*n+] =dis[*n+][i] = dist(Point(,),tmp);
}
for(int i = ;i <= *n;i++)
for(int j = i+;j <=*n;j++)
{
int lid1 = (i+)/;
int lid2 = (j+)/;
bool flag = true;
Point p1,p2;
if(i&)p1 = line[(i+)/].s;
else p1 = line[(i+)/].e;
if(j&)p2 = line[(j+)/].s;
else p2 = line[(j+)/].e;
for(int k = lid1+;k < lid2;k++)
if(inter(line[*k-],Line(p1,p2)) == false
&& inter(line[*k],Line(p1,p2)) == false)
flag = false;
if(flag) dis[i][j] = dis[j][i] = dist(p1,p2);
}
bool flag = true;
for(int i = ;i <= n;i++)
if(inter(line[*i-],Line(Point(,),Point(,))) == false
&& inter(line[*i],Line(Point(,),Point(,))) == false)
flag = false;
if(flag)dis[][*n+] = dis[*n+][] = ;
for(int k = ;k <= *n+;k++)
for(int i = ;i <= *n+;i++)
for(int j = ;j <= *n+;j++)
if(dis[i][k] + dis[k][j] < dis[i][j])
dis[i][j] = dis[i][k] + dis[k][j];
printf("%.2lf\n",dis[][*n+]);
} return ;
}