POJ 1556 The Doors 线段判交+Dijkstra
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.2
4 2 7 8 9
7 3 4.5 6 7The 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
-1Sample 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的更多相关文章
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- POJ 1556 The Doors(线段交+最短路)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- (线段判交的一些注意。。。)nyoj 1016-德莱联盟
1016-德莱联盟 内存限制:64MB 时间限制:1000ms 特判: No通过数:9 提交数:9 难度:1 题目描述: 欢迎来到德莱联盟.... 德莱文... 德莱文在逃跑,卡兹克在追.... 我们 ...
- (叉积,线段判交)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 ...
- POJ 1556 The Doors(线段交+最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5210 Accepted: 2124 Descrip ...
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- poj 1556 The Doors(线段相交,最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7430 Accepted: 2915 Descr ...
随机推荐
- WBS说明
work breakdown structure(WBS) 工作分解结构 (英语:Work Breakdown Structure, WBS)是一个详尽的,层次的(从全面到细节)的树形结构,由可交付成 ...
- [转]Traits 编程技法+模板偏特化+template参数推导+内嵌型别编程技巧
STL中,traits编程技法得到了很大的应用,了解这个,才能一窥STL奥妙所在. 先将自己所理解的记录如下: Traits技术可以用来获得一个 类型 的相关信息的. 首先假如有以下一个泛型的迭代器类 ...
- Log4Net使用指南(转)
转自:http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html 声明:本文内容主要译自Nauman Leghari的Using log4 ...
- VBoxManage命令详解
转自:http://zhang-ly520.iteye.com/blog/300606 由于最近工作对vbox有一定涉猎,发现这个写的比较好,先转来,稍有空时再根据自己的心得整理一下. VBoxMan ...
- PHP搜索Solr文档(含高亮)
<?php $options = array ( 'hostname' => 'localhost', 'port' => '8080', 'path' => 'solr/he ...
- js Math函数
1.丢弃小数部分,保留整数部分parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.fl ...
- jQuery解析JSON的问题
在WEB数据传输过程中,json是以文本,即字符串的轻量级形式传递的,而客户端一般用JS操作的是接收到的JSON对象,所以,JSON对象和JSON字符串之间的相互转换.JSON数据的解析是关键. JS ...
- Eclipse+Pydev +Django搭建开发环境时容易出错的几点
1.注意安装的软件和系统的位数是否匹配. 2.安装Django框架的时候注意是否安装了setuptools工具.在Python中,安装第三方模块,是通过setuptools这个工具完成的.Python ...
- COJ 2003 选根 (树的重心)
我们可以用树形DP在线性复杂度内搞定重心. #include<iostream> #include<cstdio> #include<cmath> #include ...
- C# 多线程经典示例 吃苹果
本文主要讲述了多线程开发中经典示例,通过本示例,可以加深对多线程的理解. 示例概述: 下面用一个模拟吃苹果的实例,说明C#中多线程的实现方法.要求开发一个程序实现如下情况:一个家庭有三个孩子,爸爸妈妈 ...
