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

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

题目大意: 给定有多少个墙,每个墙有两个门,问从(0, 5)->(10, 5)这个点的最短距离是多少,不能穿墙而过

解题思路: 既然是求最短路径的,那就转化到最短路径来算. 关键是怎么转化问题. 因为每个门都有可能成为必须要走的路线,而且每个门都有两个端点. 所以只需要枚举每个端点就行了.如果任意两个短线没有墙阻挡,那么就是可以直达的, 就把他加到图中,如果有墙阻隔,那么就是不直接可达.所以图的建立就是遍历所有的端点,包括起始点和终点.

/*************************************************************************
> File Name: poj_1556.cpp
> Author:
> Mail:
> Created Time: 2015年04月02日 星期四 14时55分17秒
************************************************************************/ #include<iostream>
#include <cstdio>
#include <cstring>
#include <math.h>
using namespace std;
const int N = ;
const int INF = ;
struct point{
double x, y;
};
point p[N];
double dis[N];
double Map[N][N];
bool vis[N];
int n;
double getDistance(int i, int j)//得到两个点之间的距离
{
return sqrt((p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y));
}
double getDirection(point a, point b, point c)//判断c与直线ab的关系
{
point t1, t2;
t1.x = c.x - a.x; t1.y = c.y - a.y;
t2.x = b.x - a.x; t2.y = b.y - a.y;
return (t1.x * t2.y - t1.y * t2.x);
}
bool segment_intersect(point a, point b, point c, point d)//判断线段是否相交
{
double d1 = getDirection(a, b, c);
double d2 = getDirection(a, b, d);
double d3 = getDirection(c, d, a);
double d4 = getDirection(c, d, b);
if (d1 * d2 < && d3 * d4 < )
return true;
return false;
}
bool isCross(int a, int b)//判断两点之间是否有墙阻隔
{
point t1, t2;
int i;
if (a == )
i = ;
else
i = a / + (a % != ) + ;
for (; i < b / + (b % != ); i++)
{
t1.x = p[ * i - ].x;
t1.y = ;
t2.x = t1.x; t2.y = 10.0;
if (segment_intersect(t1, p[ * i - ], p[a], p[b]) || segment_intersect(p[ * i - ], p[ * i - ], p[a], p[b])
|| segment_intersect(p[ * i], t2, p[a], p[b]))
return true; }
return false;
}
void Dijkstra()//单源最短路径
{
memset(vis, false, sizeof(vis));
int m = * n + ;
for (int i = ; i < m; i++)
dis[i] = Map[][i];
dis[] = ;
vis[] = true;
double min;
int k;
for (int i = ; i < m; i++)
{
min = INF;
for (int j = ; j < m; j++)
{
if (!vis[j] && min > dis[j])
{
k = j;
min = dis[j];
}
}
vis[k] = true;
for (int j = ; j < m; j++)
{
if (!vis[j] && dis[j] > dis[k] + Map[k][j])
dis[j] = dis[k] + Map[k][j];
} }
}
int main()
{
while (~scanf("%d", &n) && n != -)
{
double a, b, c, d, e;
p[].x = ; p[].y = ;
p[ * n + ].x = ; p[ * n + ].y = ;//一共4*n+2个点
for (int i = ; i <= * n; i++)
{
scanf("%lf %lf %lf %lf %lf", &a, &b, &c, &d, &e);
p[i].x = a; p[i].y = b;
p[++i].x = a; p[i].y = c;
p[++i].x = a; p[i].y = d;
p[++i].x = a; p[i].y = e;
}
for (int i = ; i < * n + ; i++)
{
for (int j = ; j < * n + ; j++)
if (i != j)
Map[i][j] = INF;
else
Map[i][j] = ;
}
for (int i = ; i < * n + ; i++)//建立地图
{
for (int j = i + ; j < * n + ; j++)
{
if (!isCross(i, j))
Map[i][j] = getDistance(i, j);
}
}
Dijkstra();//求最短路径
printf("%.2f\n", dis[ * n + ]); } return ;
}

POJ 1556 The Doors 线段判交+Dijkstra的更多相关文章

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

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

  2. POJ 1556 The Doors 线段交 dijkstra

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

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

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

  4. (线段判交的一些注意。。。)nyoj 1016-德莱联盟

    1016-德莱联盟 内存限制:64MB 时间限制:1000ms 特判: No通过数:9 提交数:9 难度:1 题目描述: 欢迎来到德莱联盟.... 德莱文... 德莱文在逃跑,卡兹克在追.... 我们 ...

  5. (叉积,线段判交)HDU1086 You can Solve a Geometry Problem too

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

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

    The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5210   Accepted: 2124 Descrip ...

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

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

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

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

  9. poj 1556 The Doors(线段相交,最短路)

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

随机推荐

  1. 【USACO 1.5.2】回文质数

    [题目描述] 因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数. 写一个程序来找出范围[a,b](5 <= a < b <= 100,0 ...

  2. JDK1.5中LOCK,Condition的使用

    import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.uti ...

  3. QQ情侣头像~

                       

  4. ORACLE数据库多表关联查询效率问题解决方案

    最近在做项目中遇到多表关联查询排序的效率问题(5张以上40W+数据的表),查询一次大概要20多秒,经过一番苦思冥想,处理方案如下: 1.软件设计初期,需要一对一关联的表应该设计在一张大表里,这样虽然字 ...

  5. JS中 submit提交与Form表单里的onsubmit的调用问题?

    最近在开发中遇到了表单提交前验证的问题,用一个普通的button按钮代替submit按钮,在提交前触发这个button的onclick事件,在其事件中触发form的submit事件.问题出现了: &l ...

  6. Powerpoin怎么制作电子相册|PPT制作电子相册教程

    Powerpoin怎么制作电子相册?你是不是也对这一问题颇感兴趣呢?下面小编就为大家带来PPT制作电子相册详细教程,赶紧准备好你的自拍照什么的,开启Powerpoin制作电子相册之旅吧! Powerp ...

  7. Java System类

    java 不支持 全局方法 和 变量, system 类 中所有成员都是静态的, 而要引用这些变量和方法,可直接system作为前缀,

  8. WM_CLOSE、WM_DESTROY、WM_QUIT的区别(询问,销毁窗口,退出进程,都不是一回事)

    1.发送消息SendMessage.PostMessage PostMessage将消息放入消息队列后马上返回,而SendMessage直到窗口过程处理完消息后才返回 2.三个消息的区别 WM_CLO ...

  9. 转:implementing cons/car/cdr without explicit storage

    I know this is old wine but it’s just too cool! It elegantly demonstrates closure and higher-order f ...

  10. 百度识图API

    http://stu.baidu.com/ http://www.360doc.com/content/14/0801/17/21412_398653199.shtml http://download ...