题目:

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.


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的正方形 其中有数堵墙 每堵墙上都有两个门 给出两个门(每个门为一段区间)的坐标 求从(0,5)到(10,5)的最短路
思路:枚举每个门向后所有可直达的点的距离 然后建边跑最短路 因为数据很小 所以随便挑一个最短路跑就可以了

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double inf=0x3f3f3f3f;
const double eps=1e-;
const int maxn=;
int n;
double x,y_1,y2,y3,y4;
double dis[maxn][maxn]; int dcmp(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);
}
Point operator - (const Point &b) const{
return Point(x-b.x,y-b.y);
}
double operator * (const Point &b) const{
return x*b.x+y*b.y;
}
double operator ^ (const Point &b) const{
return x*b.y-y*b.x;
}
}; struct Line{
Point s,e;
Line(){}
Line(Point _s,Point _e){
s=_s,e=_e;
}
}line[maxn]; 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) &&
dcmp((l2.s-l1.s)^(l1.e-l1.s))*dcmp((l2.e-l1.s)^(l1.e-l1.s))<= &&
dcmp((l1.s-l2.s)^(l2.e-l2.s))*dcmp((l1.e-l2.s)^(l2.e-l2.s))<=;
} double dist(Point a,Point b){
return sqrt((b-a)*(b-a));
} int main(){
while(~scanf("%d",&n)){
if(n==-) break;
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf%lf",&x,&y_1,&y2,&y3,&y4);
line[*i-]=Line(Point(x,y_1),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+)/;
int flag=;
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=;
}
if(flag) dis[][i]=dis[i][]=dist(Point(,),tmp);
flag=;
for(int j=lid+;j<=n;j++){
if(inter(line[*j-],Line(Point(,),tmp))==false
&& inter(line[*j],Line(Point(,),tmp))==false)
flag=;
}
if(flag) dis[*n+][i]=dis[i][*n+]=dist(Point(,),tmp);
}
for(int i=;i<=*n;i++)
for(int j=i+;j<=*n;j++){
int lid1=(i+)/;
int lid2=(j+)/;
int flag=;
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=;
}
if(flag) dis[i][j]=dis[j][i]=dist(p1,p2);
}
int flag=;
for(int i=;i<=n;i++){
if(inter(line[*i-],Line(Point(,),Point(,)))==false
&& inter(line[*i],Line(Point(,),Point(,)))==false)
flag=;
}
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][j]>dis[i][k]+dis[k][j])
dis[i][j]=dis[i][k]+dis[k][j];
printf("%.2f\n",dis[][*n+]);
}
return ;
}

 

POJ 1556 The Doors(线段相交+最短路)的更多相关文章

  1. POJ 1556 - The Doors 线段相交不含端点

    POJ 1556 - The Doors题意:    在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少.    分析:        要么直达,要么 ...

  2. POJ 1556 The Doors(线段交+最短路)

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...

  3. POJ 1556 计算几何 判断线段相交 最短路

    题意: 在一个左下角坐标为(0,0),右上角坐标为(10,10)的矩形内,起点为(0,5),终点为(10,5),中间会有许多扇垂直于x轴的门,求从起点到终点在能走的情况下的最短距离. 分析: 既然是求 ...

  4. POJ 1556 The Doors 线段交 dijkstra

    LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...

  5. POJ 1556 The Doors 线段判交+Dijkstra

    The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6734   Accepted: 2670 Descrip ...

  6. POJ 2556 (判断线段相交 + 最短路)

    题目: 传送门 题意:在一个左小角坐标为(0, 0),右上角坐标为(10, 10)的房间里,有 n 堵墙,每堵墙都有两个门.每堵墙的输入方式为 x, y1, y2, y3, y4,x 是墙的横坐标,第 ...

  7. POJ 1556 The Doors --几何,最短路

    题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少. 解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短 ...

  8. POJ_1556_The Doors_判断线段相交+最短路

    POJ_1556_The Doors_判断线段相交+最短路 Description You are to find the length of the shortest path through a ...

  9. POJ 1556 The Doors【最短路+线段相交】

    思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...

  10. 简单几何(线段相交+最短路) POJ 1556 The Doors

    题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...

随机推荐

  1. SpringBoot使用JSP(官网Demo)

    最开始接触java的时候,前端页面基本都是用jsp来写,最近公司项目要使用SpringBoot重构,查看SpringBoot文档,发现SpringBoot不建议使用JSP,因为jsp在使用内嵌serv ...

  2. PTA 天梯赛练习 7-11 玩转二叉树-二叉树重建

    以前就思考过这个问题,但是没有深入的想过,这是一种叫二叉树重建的典型题目 如果给出中序和前序,求出后序遍历. 这道题则求的是交换儿子节点的层序遍历. 二叉树的重建应该怎么重建,首先我们知道,先根遍历, ...

  3. Kuro and Walking Route CodeForces - 979C (树上DFS)

    Kuro is living in a country called Uberland, consisting of nn towns, numbered from 11to nn, and n−1n ...

  4. HTML之超链接

    图像标签 图像标签为 <img> ,它是行内元素,其主要功能是在网页里面插入图像,所插入图片由属性 scr 属性决定.主要格式为 <img scr="URL"&g ...

  5. ORM框架SQLAlchemy

    SQLAlchemy orm英文全称object relational mapping,就是对象映射关系程序,简单来说就是类似python这种面向对象的程序来说一切皆对象,但是使用的数据库却都是关系型 ...

  6. docker(二) windows10下安装docker

    官方安装文档: https://docs.docker.com/docker-for-windows/install/ https://docs.docker.com/docker-for-windo ...

  7. Ubuntu 系统安装详解 19.04最新版本

    Ubuntu 19.04版本系统安装详解 1 .镜像的下载 推荐 阿里云镜像下载 2.安装 1.1.新建虚拟机 注意硬件的兼容性问题 当前只有5.x可以用,其他兼容各位可以尝试下,我也都试过,但只有5 ...

  8. [转帖]Scanners-Box 指引

    作者:杨文链接:https://zhuanlan.zhihu.com/p/26534414来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文地址:We5ter/Sca ...

  9. LODOP超文本简短问答和相关内容

    html样式查看lodop内部解析的html信息,见http://www.c-lodop.com/faq/pp8.html分析差异点,因浏览器版本不同遵循的html标准不同,造成某些标签属性显示有差异 ...

  10. vue实现点击展开,点击收起

    安装vue的步骤在这里就不进行赘述了,下面直接进入正题 首先定义一下data里面的数据: data () { return { toLearnList:[ 'html','css','javascri ...