POJ 1556 The Doors (线段交+最短路)
The Doors
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6617 | Accepted: 2634 |
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
题意:一个10*10的正方形地图,中间有n堵墙,每个墙上有2扇门,对于每堵墙,给你x坐标,2扇门的4点坐标,问你从从起点(0,5)到终点(10,5)的最短路。
思路:由于n较小,只用分别枚举2个顶点看是否被其他墙挡住,若没有就更新距离,最后跑最短路。
代码:
View Code#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-8;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
else return 1;
}
struct Point
{
int id;
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 wall
{
double xx,s1,s2,e1,e2;
} e[100];
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)) <= 0 &&
sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
}
double dist(Point a,Point b)
{
return sqrt((b-a)*(b-a));
}
const int N = 100;
Line line[N];
double dis[N][N];
const double INF = 1e20;
Point po[N];
void init(int n)
{
for(int i = 0; i <= 4*n+1; i++)
for(int j = 0; j <= 4*n+1; j++)
{
if(i == j)dis[i][j] = 0;
else dis[i][j] = INF;
}
po[0].id=0,po[0].x=0,po[0].y=5;
}
bool check(Point a,Point b)
{
for(int i=a.id+1;i<=b.id-1;i++)
{
Line l1=Line(Point(e[i].xx,e[i].s1),Point(e[i].xx,e[i].e1));
Line l2=Line(Point(e[i].xx,e[i].s2),Point(e[i].xx,e[i].e2));
Line l3=Line(Point(a.x,a.y),Point(b.x,b.y));
if(!inter(l1,l3) && !inter(l2,l3))
return false;
}
return true;
}
int main()
{
int n,cnt;
double x,y1,y2,y3,y4;
while(scanf("%d",&n) == 1 && n>=0)
{
init(n);
cnt=1;
for(int i = 1; i <= n; i++)
{
scanf("%lf%lf%lf%lf%lf",&e[i].xx,&e[i].s1,&e[i].e1,&e[i].s2,&e[i].e2);
po[cnt].id=po[cnt+1].id=po[cnt+2].id=po[cnt+3].id=i;
po[cnt].x=po[cnt+1].x=po[cnt+2].x=po[cnt+3].x=e[i].xx;
po[cnt].y=e[i].s1,po[cnt+1].y=e[i].e1,po[cnt+2].y=e[i].s2,po[cnt+3].y=e[i].e2;
cnt+=4;
}
po[cnt].id=n+1,po[cnt].x=10,po[cnt].y=5;
cnt++;
for(int i = 0; i < cnt; i++)
for(int j = i+1; j < cnt; j++)
{
if(po[i].id==po[j].id) continue;
if(check(po[i],po[j]))
dis[i][j]=min(dis[i][j],dist(po[i],po[j]));
}
for(int k = 0; k < cnt; k++)
for(int i = 0; i < cnt; i++)
for(int j = 0; j < cnt; j++)
if(dis[i][k] + dis[k][j] < dis[i][j])
dis[i][j] = dis[i][k] + dis[k][j];
printf("%.2lf\n",dis[0][cnt-1]);
}
return 0;
}