POJ 1556 The Doors(线段交+最短路)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 5210 | Accepted: 2124 |
Description

Input
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
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
/************************************************************
* Author : kuangbin
* Email : kuangbin2009@126.com
* Last modified : 2013-07-14 10:47
* Filename : POJ1556TheDoors.cpp
* Description :
* *********************************************************/ #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h> using namespace std; const double eps = 1e-;
int sgn(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);
}
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e)
{
s = _s;e = _e;
}
};
//判断线段相交
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) &&
sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= &&
sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= ;
}
double dist(Point a,Point b)
{
return sqrt((b-a)*(b-a));
}
const int MAXN = ;
Line line[MAXN];
double dis[MAXN][MAXN];
const double INF = 1e20;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
double x,y1,y2,y3,y4;
while(scanf("%d",&n) == )
{
if(n == -) break;
for(int i = ;i <= n;i++)
{
scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
line[*i-] = Line(Point(x,y1),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+)/;
bool flag = true;
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 = false;
if(flag)dis[][i] =dis[i][] = dist(Point(,),tmp);
flag = true;
for(int j = lid+;j <= n;j++)
if(inter(line[*j-],Line(Point(,),tmp)) == false
&& inter(line[*j],Line(Point(,),tmp)) == false)
flag = false;
if(flag)dis[i][*n+] =dis[*n+][i] = dist(Point(,),tmp);
}
for(int i = ;i <= *n;i++)
for(int j = i+;j <=*n;j++)
{
int lid1 = (i+)/;
int lid2 = (j+)/;
bool flag = true;
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 = false;
if(flag) dis[i][j] = dis[j][i] = dist(p1,p2);
}
bool flag = true;
for(int i = ;i <= n;i++)
if(inter(line[*i-],Line(Point(,),Point(,))) == false
&& inter(line[*i],Line(Point(,),Point(,))) == false)
flag = false;
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][k] + dis[k][j] < dis[i][j])
dis[i][j] = dis[i][k] + dis[k][j];
printf("%.2lf\n",dis[][*n+]);
} return ;
}
POJ 1556 The Doors(线段交+最短路)的更多相关文章
- POJ 1556 The Doors(线段交+最短路)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- POJ 1556 The Doors 线段判交+Dijkstra
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6734 Accepted: 2670 Descrip ...
- POJ 1556 The Doors --几何,最短路
题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少. 解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短 ...
- POJ 1556 计算几何 判断线段相交 最短路
题意: 在一个左下角坐标为(0,0),右上角坐标为(10,10)的矩形内,起点为(0,5),终点为(10,5),中间会有许多扇垂直于x轴的门,求从起点到终点在能走的情况下的最短距离. 分析: 既然是求 ...
- POJ 3304 Segments 基础线段交判断
LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
随机推荐
- CSS控制背景
一.设置背景颜色:background-color 十六进制 background-color:#ff0000; 英文名称 background-color:red; 三原色 background-c ...
- how bootstrap fit into our website design?
在web相关技术中,HTML代表了content,structure, CSS则负责styling,决定内容是如何展示的,javascript则主要负责interactive, behaviour. ...
- Windows 桌面软件:不绑定bing搜索的缤纷桌面
bing:世界上最好的壁纸提供商 ^.^一直垂涎着Bing的壁纸,总是想找机会来一番邂逅. 之前使用bing自家的缤纷桌面.这个软件缺点就是和bing搜索绑定太厉害,放在桌面上感觉那个黑色的条框很碍 ...
- 51nod1055 最长等差数列
完全一脸懵逼!.dp[i][j]表示i,j为相邻的两项的最大值.两个指针两边扫的思想好劲啊这个!%%% #include<cstdio> #include<cstring> # ...
- SkinPP for VC
1.下载文件:SkinPPWTL.h,SkinPPWTL.dll,SkinPPWTL.lib以及Skin++皮肤库: 2.新建一个工程,如:基于多文档的工程,名为:MySkin: 3.将下载的Skin ...
- mysql 表日常变化前几
mysql 表日常变化前几use performance_schema create table test.snap1 as SELECT OBJECT_SCHEMA, OBJECT_NAME, CO ...
- Android 下实现图片的移动
网上看到的demo,感觉很有趣,但是 实用性不是太强,记录一下. 源码下载地址:请戳这里---------------->
- OpenGL学习之路(一)
1 引子 虽然是计算机科班出身,但从小对几何方面的东西就不太感冒,空间想象能力也较差,所以从本科到研究生,基本没接触过<计算机图形学>.为什么说基本没学过呢?因为好奇(尤其是惊叹于三维游戏 ...
- C# 对Excel文档打印时的页面设置
1.对打印页面的朝向,页宽,页高进行设置 参考源码[1] using Excel = Microsoft.Office.Interop.Excel; Excel.Application tmpExce ...
- linux中ulimit作用
一.作用 Linux对于每个用户,系统限制其最大进程数.为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数. ulimit主要是用来限制进程对资源的使用情况的,它支持各种类型的限制, ...