题目大意:有一个房间(左上角(0,10),右下角(10,0)),然后房间里有N面墙,每面墙上都有两个门,求出来从初始点(0,5),到达终点(10,5)的最短距离。

 
分析:很明显根据两点之间直线最短,所以所走的路线一定是点之间的连线,只需要判断一下这两点间知否有墙即可。
 
代码如下:
======================================================================================================================================
#include<math.h>
#include<algorithm>
#include<stdio.h>
using namespace std; const int MAXN = ;
const double oo = 1e9+;
const double EPS = 1e-; struct point
{
double x, y, len;
point(double x=, double y=, double len=):x(x),y(y),len(len){}
point operator - (const point &t) const{
return point(x-t.x, y-t.y);
}
int operator *(const point &t) const{
double c = x*t.y - y*t.x;
if(c > EPS)return ;
if(fabs(c)<EPS)return ;
return -;
}
};
struct Wall
{
point A, B;
Wall(point A=, point B=):A(A), B(B){}
}; bool CanLine(point a, point b, Wall w[], int N)
{
for(int i=; i<N; i++)
{
if( w[i].A.x < b.x || w[i].A.x > a.x )
continue;
int t = (a-b)*(w[i].A-b) + (a-b)*(w[i].B-b); if(t == )
return false;
} return true;
} int main()
{
int M; while(scanf("%d", &M) != EOF && M != -)
{
int i, j, nw=, np=;
double x, y[];
Wall w[MAXN]; point p[MAXN]; p[] = point(, , );
while(M--)
{
scanf("%lf%lf%lf%lf%lf", &x, &y[], &y[], &y[], &y[]); p[np++] = point(x, y[], oo), p[np++] = point(x, y[], oo);
p[np++] = point(x, y[], oo), p[np++] = point(x, y[], oo);
w[nw++] = Wall(point(x, -), point(x, y[]));
w[nw++] = Wall(point(x, y[]), point(x, y[]));
w[nw++] = Wall(point(x, y[]), point(x, ));
}
p[np++] = point(, , oo); for(i=; i<np; i++)
for(j=; j<i; j++)
{
point t = p[i] - p[j];
t.len = sqrt(t.x*t.x+t.y*t.y); if(p[i].len > t.len + p[j].len && CanLine(p[i], p[j], w, nw) == true)
p[i].len = t.len + p[j].len;
} printf("%.2f\n", p[np-].len);
} return ;
}

The Doors - POJ 1556 (线段相交)的更多相关文章

  1. poj 1066 线段相交

    链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  2. poj 1269 线段相交/平行

    模板题 注意原题中说的线段其实要当成没有端点的直线.被坑了= = #include <cmath> #include <cstdio> #include <iostrea ...

  3. poj 2653 线段相交

    题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说 ...

  4. poj 2653 线段相交裸题(解题报告)

    #include<stdio.h> #include<math.h> const double eps=1e-8; int n; int cmp(double x) { if( ...

  5. poj 1410 线段相交判断

    http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  6. Pipe - POJ 1039(线段相交交点)

    题目大意:有一个不反光并且不透光的管道,现在有一束光线从最左端进入,问能达到的最右端是多少,输出x坐标.   分析:刚开始做是直接枚举两个点然后和管道进行相交查询,不过这样做需要考虑的太多,细节不容易 ...

  7. Pick-up sticks - POJ 2653 (线段相交)

    题目大意:有一个木棒,按照顺序摆放,求出去上面没有被别的木棍压着的木棍.....   分析:可以维护一个队列,如果木棍没有被压着就入队列,如果判断被压着,就让那个压着的出队列,最后把这个木棍放进队列, ...

  8. POJ 2074 | 线段相交

    #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #defi ...

  9. POJ 1556 The Doors 线段交 dijkstra

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

随机推荐

  1. js文件缓存之版本管理

    以前也做过不少项目,但从来就没有把关注的目光投向过js文件缓存.最近终于在毫无意识的情况下跳进了这个大坑. 近几个月来的工作是一个交易系统持续改进项目,迭代发布周期大约为2~3周.最近一次迭代是V16 ...

  2. 【转】怎样创建一个Xcode插件(Part 1)

      原文:How To Create an Xcode Plugin: Part 1/3 原作者:Derek Selander 译者:@yohunl 译者注:原文使用的是xcode6.3.2,我翻译的 ...

  3. 【html】【5】html class属性css样式

    必看参考: http://www.divcss5.com/css3-style/ http://www.jb51.net/css/142448.html http://www.w3school.com ...

  4. 菜鸟日记之JSP二 内置对象的理解

    ·最近学习JSP了,对编程和网络又有了一些理解.无论是现实中人与人的交流,还是网络世界的接触,都是在相互表达自己的意思让别人知道,并理解对方的信息.然后我们知道的事情不断的变多,会又交杂出新的内容,不 ...

  5. __init__ __new__区别

    请运行代码: class A: def __init__(self): print "A.__init" def __new__(self): print "A.__ne ...

  6. 读书笔记之 - javascript 设计模式 - 责任链模式

    责任链模式可以用来消除请求的发送者和接收者之间的耦合.这是通过实现一个由隐式地对请求进行处理的对象组成的链而做到的.链中的每个对象可以处理请求,也可以将其传给下一个对象. 责任链的结构: 责任链由多个 ...

  7. 自定义函数中的参数返回值 “-> (Int -> Int)”的问题

    func makeIncrementer() -> (Int -> Int) { func addOne(number: Int) -> Int { + number } retur ...

  8. centos下的lnmp环境搭建

    1.配置centos的第三方yum源,因为原始的yum是无法安装nginx的 wget http://www.atomicorp.com/installers/atomic  下载atomic yum ...

  9. ”ENV_IS_EMBEDDED“解惑以及相关的移植实验

    一.概述( ENV_IS_EMBEDDED的目的) 经典资料 认识     ENV_IS_EMBEDDED只有在CFG_ENV_IS_IN_FLASH或者CFG_ENV_IS_IN_NAND定义了才有 ...

  10. Meta 的两个 相关属性

    Meta标签中的apple-mobile-web-app-status-bar-style属性及含义: “apple-mobile-web-app-status-bar-style”作用是控制状态栏显 ...