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 ...
随机推荐
- 你好,C++(34)有一只叫做多利的羊 6.2.4 拷贝构造函数
6.2.4 拷贝构造函数 在C++世界中,除了需要使用构造函数直接创建一个新的对象之外,有时还需要根据已经存在的某个对象创建它的一个副本,就像那只叫做多利的羊一样,我们希望根据一只羊创建出来另外一只 ...
- JavaScript 目标装配式编程(Target Assemble Programming)
TAP概述 脚本中一切皆对象,若还以传统模式思考编程模式,那简直是对不起脚本解释器的强大支持:我们应该以最接近人类操作方式的来表达人的意图. 更接近工作实践的方式,比如游戏中,一个人物一个角色,人物的 ...
- WireShark过滤语法
1.过 滤IP,如来源IP或者目标IP等于某个IP 例子:ip.src eq 192.168.1.107 or ip.dst eq 192.168.1.107或者ip.addr eq 192.168. ...
- Java简介(3)-基本语法
1.大小写敏感 2.类名 3.方法名. 4.源文件名
- ftp文件操作
PHP FTP操作类( 上传.拷贝.移动.删除文件/创建目录 ) 2016-06-01 PHP编程 /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) */ class class_ ...
- js 获取页面高度和宽度(兼容 ie firefox chrome),获取鼠标点击位置
<script> //得到页面高度 var yScroll = (document.documentElement.scrollHeight >document.documentEl ...
- 002.AngularJs调用Restful实现CRUD
本节我们主要给大家介绍AngularJs如何调用Restful,实现数据的CRUD. 主要用到的技术: 后端:ASP.NET WebApi + SQLServer2008 前端:AngularJs,B ...
- S5PV210启动过程分析
一.三星官方推荐方式 1.数据手册<S5PV210_iROM_Application_note>中截取:
- 【转】Windows平台下Git服务器搭建
Windows平台下Git服务器搭建 Posted on 2015-05-18 21:29 阿祥当码农 阅读(7637) 评论(0) 编辑 收藏 该文章转自:http://www.codeceo.co ...
- 个人.net学习规划路线
