The Doors
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7430   Accepted: 2915

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.

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

【思路】

  枚举所有点,如果不与竖边相交则连边,做最短路即可。

【代码】

 #include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int N = +;
const double INF = 1e9;
const double eps = 1e-; int dcmp(double x) {
if(x<eps) return ; else return x<? -:;
} struct Pt {
double x,y;
Pt(double x=,double y=):x(x),y(y) {};
};
struct Seg { Pt a1,a2; };
typedef Pt vec; vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); }
bool operator != (Pt A,Pt B) {
if(dcmp(A.x-B.x)== && dcmp(A.y-B.y)==) return ;
else return ;
} double cross(Pt A,Pt B) { return A.x*B.y-A.y*B.x; } bool SegInter(Pt s1, Pt e1, Pt s2, Pt e2) {
if(
cross(e1-s1,s2-s1) * cross(e1-s1,e2-s1) <= &&
cross(e2-s2,s1-s2) * cross(e2-s2,e1-s2) <=
) return true;
return false;
}
double dist(Pt a,Pt b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double f[N][N];
Seg L[N]; int lc;
Pt P[N]; int pc;
int n; int main() {
while(scanf("%d",&n)== && n>) {
pc=lc=;
FOR(i,,n) {
double x,y1,y2,y3,y4;
scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
L[++lc]=(Seg) {Pt(x,),Pt(x,y1)};
L[++lc]=(Seg) {Pt(x,y2),Pt(x,y3)};
L[++lc]=(Seg) {Pt(x,y4),Pt(x,)};
P[++pc]=Pt(x,y1) , P[++pc]=Pt(x,y2);
P[++pc]=Pt(x,y3) , P[++pc]=Pt(x,y4);
}
P[++pc]=Pt(,), P[++pc]=Pt(,);
FOR(i,,pc) FOR(j,,pc) f[i][j]=INF;
FOR(i,,pc) FOR(j,i+,pc) {
bool flag=;
FOR(k,,lc)
if(SegInter(P[i],P[j],L[k].a1,L[k].a2))
{ flag=; break; }
if(flag)
f[i][j]=f[j][i]=dist(P[i],P[j]);
}
FOR(i,,n) {
FOR(j,i+,n) if(f[i][j]!=INF)
printf("%d,%d : %.2lf\n",i,j,f[i][j]);
}
FOR(k,,pc) FOR(i,,pc) FOR(j,,pc)
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
printf("%.2lf\n",f[pc-][pc]);
}
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. 代码笔记-触摸事件插件hammer.js使用

    如果要使用jquery,则需要下载jquery.hammer.min.js版本 新建一个hammer对象生成的对象是dom对象,不能直接使用jqeury 的  $(this)方法,需要先将其转成jqu ...

  2. eval(phpcode) 字符当代码执行

    eval(phpcode)eval() 函数把字符串按照 PHP 代码来计算.相当于在字符串两边分别加上PHP语 法标签 该字符串必须是合法的 PHP 代码,且必须以分号结尾. 如果没有在代码字符串中 ...

  3. JS将搜索的关键字高亮显示实现代码

    这篇文章介绍了JS将搜索的关键字高亮显示实现代码,有需要的朋友可以参考一下 用JS让文章内容指定的关键字加亮 是这样的.. 现在有这些关键字:美容,生活,购物 当在文章里头出现这些关键字,就把它加亮显 ...

  4. [转]PHP Session原理分析及使用

    之前在一个叫魔法实验室的博客中看过一篇<php session原理彻底分析>的文章,作者从session的使用角度很好阐述了在代码运行过程中,每个环节的变化以及相关参数的设置及作用.本来想 ...

  5. 参数TFilterPredicate 类型说明

    类型名称:TFilterPredicate 类型定义: type TFilterPredicate = reference to function(const Path: string, const ...

  6. jbpm4.4 spring整合

    jBPM-4.4与Spring集成配置比较容易,这里我使用的是Spring-2.5.6,数据库连接池使用C3P0,将相关的两个jar文件加入到CLASSPATH中. jBPM-4.4与Spring集成 ...

  7. JavaNIO之Channel

    Channel的本质是通道,用来连接JVM之外数据向JVM内传输数据,比如来自于硬盘的文件,来自于网络的数据包.JVM之外的数据就是通过Channel进行数据传输:如果把Channel比作河道,那么作 ...

  8. 单片微机原理P0:80C51结构原理

    本来我真的不想让51的东西出现在我的博客上的,因为51这种东西真的太low了,学了最多就所谓的垃圾科创利用一下,但是想一下这门课我也要考试,还是写一点东西顺便放博客上吧. 这一系列主要参考<单片 ...

  9. QT窗口渐现效果,窗口震动效果,鼠标移动窗口

    //窗口渐现效果void MainWindow::closeWindowAnimation() //关闭窗口效果 { QPropertyAnimation *animation = new QProp ...

  10. E.164 Format

    From http://en.wikipedia.org/wiki/E.164 E.164 is an ITU-T recommendation, titled The international p ...