POJ 1556 E - The Doors
题意:给定n堵墙,现在要你从(0,5)走去(10,5)的最短距离
思路:刚开始还想模拟,就是从(0,5)走,每次x向右一格,然后判断有没和线段相交就可以。但是它的们有可能是小数形式给出的,这样就GG了(x--x+1中可能存在很多门)。正确的方法应该是建图,对于所有门,他们都有端点的,先把他们加入到图中,包括起点的话,一共有num个点吧。然后暴力判断e[i][j]是否能到达就可以,这里用线段相交就可以判断。然后floyd一下就好。bug点:门的端点不应该加进来,就是(x,0)、(x,10)这样的点不应该加入图中,因为那个是死角,不能出去了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const double eps = 1e-;
const int maxn = +;
bool same (double a,double b)
{
return fabs(a-b)<eps;
}
struct coor
{
double x,y;
coor(){}
coor(double xx,double yy):x(xx),y(yy){}
double operator ^(coor rhs) const //计算叉积(向量积)
{
return x*rhs.y - y*rhs.x;
}
coor operator -(coor rhs) const //坐标相减,a-b得到向量ba
{
return coor(x-rhs.x,y-rhs.y);
}
double operator *(coor rhs) const //数量积
{
return x*rhs.x + y*rhs.y;
}
bool operator ==(coor rhs) const
{
return same(x,rhs.x)&&(y,rhs.y);//same的定义其实就是和eps比较
}
}a[maxn]; struct Line
{
coor point1,point2;
Line(){}
Line(coor xx,coor yy):point1(xx),point2(yy){}
bool operator &(Line rhs) const //判断直线和rhs线段是否相交
{
//自己表示一条直线,然而rhs表示的是线段
//思路,判断rhs线段上两个端点是否在this直线的同一侧即可,用一侧,就不相交
coor ff1 = point2 - point1; //直线的方向向量
return ( ((rhs.point1-point1)^ff1) * ((rhs.point2-point1)^ff1) ) <= ;//符号不同或者有0,证明相交
}
}LINE[maxn]; bool OnSegment (coor a,coor b,coor c) //判断点C是否在线段ab上
{
double min_x = min(a.x,b.x), min_y = min(a.y,b.y);
double max_x = max(a.x,b.x), max_y = max(a.y,b.y);
if (c.x>=min_x && c.x<=max_x && c.y>=min_y && c.y<=max_y) return true;
else return false;
}
bool SegmentIntersect (coor a,coor b,coor c,coor d)
{
double d1 = (b-a)^(d-a); //direction(a,b,d);以a为起点,计算ab和ab的叉积
double d2 = (b-a)^(c-a);
double d3 = (d-c)^(a-c);
double d4 = (d-c)^(b-c);
if (d1*d2< && d3*d4<) return true;
else if (d1 == && OnSegment(a,b,d)) return true;
else if (d2 == && OnSegment(a,b,c)) return true;
else if (d3 == && OnSegment(c,d,a)) return true;
else if (d4 == && OnSegment(c,d,b)) return true;
else return false;
} int n;
int all=;
double dis (coor a,coor b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double e[maxn][maxn];
void work ()
{
all = ;
int num=;
for (int i=;i<=n;++i)
{
double aa,bb,cc,dd,ee;
scanf("%lf%lf%lf%lf%lf",&aa,&bb,&cc,&dd,&ee);
++all;
LINE[all].point1 = coor(aa,); //a[++num]=coor(aa,0);
LINE[all].point2 = coor(aa,bb);a[++num]=coor(aa,bb); ++all;
LINE[all].point1 = coor(aa,cc);a[++num]=coor(aa,cc);
LINE[all].point2 = coor(aa,dd);a[++num]=coor(aa,dd); ++all;
LINE[all].point1 = coor(aa,ee);a[++num]=coor(aa,ee);
LINE[all].point2 = coor(aa,);//a[++num]=coor(aa,10);
}
a[++num]=coor(,);
a[++num]=coor(,);
//memset(e,0x3f,sizeof e);
for (int i=;i<=maxn-;++i)
{
for (int j=;j<=maxn-;++j)
{
e[i][j]=100000000.0;
} }
for (int i=;i<=num;++i)
{
for (int j=i+;j<=num;++j)
{
int k;
//if (same(a[j].y,0)||same(a[j].y,10)||same(a[i].y,0)||same(a[i].y,10)) continue;
for (k=;k<=all;++k)
{
if (LINE[k].point1==a[i]||LINE[k].point1==a[j]||LINE[k].point2==a[i]||LINE[k].point2==a[j]) continue;
if (SegmentIntersect(a[i],a[j],LINE[k].point1,LINE[k].point2)) break;
}
if (k==all+) e[i][j]=e[j][i]=dis(a[i],a[j]);
}
}
for (int i=;i<=num;++i)
{
for (int j=;j<=num;j++)
{
for (int k=;k<=num;++k)
{
if (e[j][k]>e[j][i]+e[i][k]) e[j][k]=e[j][i]+e[i][k];
}
}
}
printf ("%0.2f\n",e[num-][num]);
//printf ("%0.2f\n",e[num-1][1]);
return;
} int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
while(scanf("%d",&n)!=EOF && n!=-) work();
return ;
}
POJ 1556 E - The Doors的更多相关文章
- 【POJ 1556】The Doors 判断线段相交+SPFA
黑书上的一道例题:如果走最短路则会碰到点,除非中间没有障碍. 这样把能一步走到的点两两连边,然后跑SPFA即可. #include<cmath> #include<cstdio> ...
- 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 好题
// 最短路+线段交 POJ 1556 好题 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终 ...
- POJ 1556 - The Doors - [平面几何+建图spfa最短路]
题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...
- poj 1556 The Doors
The Doors Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java ...
- POJ 1556 The Doors(线段交+最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5210 Accepted: 2124 Descrip ...
- poj 1556 The Doors(线段相交,最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7430 Accepted: 2915 Descr ...
- POJ 1556 The Doors 线段判交+Dijkstra
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6734 Accepted: 2670 Descrip ...
随机推荐
- Docker容器里的进程为什么要前台运行
<第一本Docker书>里面,讲到Docker容器启动web服务时,都指定了前台运行的参数,例如apache: ENTRYPOINT [ "/usr/sbin/apache2&q ...
- CCS V5 使用教程二:创建工程和配置软件仿真
新建CCS项目 选择File/New/CCS Project: 新建项目对话框 1) Project name: 指项目名称,这里要注意的项目名称不区分大小写. 2) Ou ...
- SQL介绍(1)
SQL 是用于访问和处理数据库的标准的计算机语言. SQL,指结构化查询语言,全称是 Structured Query Language. SQL 让您可以访问和处理数据库. SQL 是一种 ANSI ...
- java ----一个函数传回多个值的总结
java 一个函数如何返回多个值 参考方法: 1.使用map返回值:这个方法问题是,你并不知道如何返回值的key是什么,只能通过doc或者通过源代码来查看. 2.传入一个引用进去,修改引用的属性值.问 ...
- Python-连接Redis并操作
首先开启redis的外连 sch01ar@ubuntu:~$ sudo vim /etc/redis/redis.conf 把bind 127.0.0.1这行注释掉 然后重启redis sudo /e ...
- springMVC绑定json参数之二(2.2.2)
二.springmvc 接收不同格式的json字符串 2).格式二:json字符串数组 前台: test = function () { var test = ["123",&qu ...
- Windows环境下 Hadoop Error: JAVA_HOME is incorrectly set. 问题
最近尝试在windows开发MR程序并且提交Job,在解压缩好Hadoop,配置好环境变量后, 打开cmd 输入hadoop version 的时候出现以下错误: Error: JAVA_HOME i ...
- Struts2学习第三课 Action
action VS Action类 action:代表一个Struts2的请求 Action类:能够处理struts2请求的类. 属性的名字必须遵守与JavaBean属性名相同的命名规则. 属性的 ...
- hdu1073
#include<iostream> using namespace std; #define N 5050 char a[N],b[N],tmp[N]; void Read(char p ...
- ERROR: JDWP Unable to get JNI 1.2 environment的解决方法
当执行如下代码时: //从控制台获取输入 InputStream is = System.in; Scanner scanner = new Scanner(is); System.out.print ...