POJ 1556 The Doors(线段交+最短路)
#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-8;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
else return 1;
}
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)) <= 0 &&
sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
}
double dist(Point a,Point b)
{
return sqrt((b-a)*(b-a));
}
const int MAXN = 100;
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) == 1)
{
if(n == -1) break;
for(int i = 1;i <= n;i++)
{
scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
line[2*i-1] = Line(Point(x,y1),Point(x,y2));
line[2*i] = Line(Point(x,y3),Point(x,y4));
}
for(int i = 0;i <= 4*n+1;i++)
for(int j = 0;j <= 4*n+1;j++)
{
if(i == j)dis[i][j] = 0;
else dis[i][j] = INF;
}
for(int i = 1;i <= 4*n;i++)
{
int lid = (i+3)/4;
bool flag = true;
Point tmp;
if(i&1)tmp = line[(i+1)/2].s;
else tmp = line[(i+1)/2].e;
for(int j = 1;j < lid;j++)
if(inter(line[2*j-1],Line(Point(0,5),tmp)) == false
&& inter(line[2*j],Line(Point(0,5),tmp)) == false)
flag = false;
if(flag)dis[0][i] =dis[i][0] = dist(Point(0,5),tmp);
flag = true;
for(int j = lid+1;j <= n;j++)
if(inter(line[2*j-1],Line(Point(10,5),tmp)) == false
&& inter(line[2*j],Line(Point(10,5),tmp)) == false)
flag = false;
if(flag)dis[i][4*n+1] =dis[4*n+1][i] = dist(Point(10,5),tmp);
}
for(int i = 1;i <= 4*n;i++)
for(int j = i+1;j <=4*n;j++)
{
int lid1 = (i+3)/4;
int lid2 = (j+3)/4;
bool flag = true;
Point p1,p2;
if(i&1)p1 = line[(i+1)/2].s;
else p1 = line[(i+1)/2].e;
if(j&1)p2 = line[(j+1)/2].s;
else p2 = line[(j+1)/2].e;
for(int k = lid1+1;k < lid2;k++)
if(inter(line[2*k-1],Line(p1,p2)) == false
&& inter(line[2*k],Line(p1,p2)) == false)
flag = false;
if(flag) dis[i][j] = dis[j][i] = dist(p1,p2);
}
bool flag = true;
for(int i = 1;i <= n;i++)
if(inter(line[2*i-1],Line(Point(0,5),Point(10,5))) == false
&& inter(line[2*i],Line(Point(0,5),Point(10,5))) == false)
flag = false;
if(flag)dis[0][4*n+1] = dis[4*n+1][0] = 10;
for(int k = 0;k <= 4*n+1;k++)
for(int i = 0;i <= 4*n+1;i++)
for(int j = 0;j <= 4*n+1;j++)
if(dis[i][k] + dis[k][j] < dis[i][j])
dis[i][j] = dis[i][k] + dis[k][j];
printf("%.2lf\n",dis[0][4*n+1]);
} return 0;
}
POJ 1556 The Doors(线段交+最短路)的更多相关文章
- 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 1556 The Doors(线段交+最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5210 Accepted: 2124 Descrip ...
- POJ 3304 Segments 基础线段交判断
LINK 题意:询问是否存在直线,使得所有线段在其上的投影拥有公共点 思路:如果投影拥有公共区域,那么从投影的公共区域作垂线,显然能够与所有线段相交,那么题目转换为询问是否存在直线与所有线段相交.判断 ...
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
随机推荐
- Codeforces Round #372 (Div. 2)
Codeforces Round #372 (Div. 2) C. Plus and Square Root 题意 一个游戏中,有一个数字\(x\),当前游戏等级为\(k\),有两种操作: '+'按钮 ...
- spark新能优化之数据本地化
数据本地化的背景: 数据本地化对于Spark Job性能有着巨大的影响.如果数据以及要计算它的代码是在一起的,那么性能当然会非常高.但是,如果数据和计算它的代码是分开的,那么其中之一必须到另外一方的机 ...
- URAL 1004 Sightseeing Trip(最小环)
Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...
- LeetCode() Search a 2D MatrixII
一开始的超时思路 int row=a.size(),col=a[0].size(); for(int i=0;i<row;i++) { if(a[i][col-1] > target &a ...
- click 绑定(二)带参数的click 事件绑定
注1:传参数给你的click 句柄 最简单的办法是传一个function包装的匿名函数: <button data-bind="click: function() { viewMode ...
- Android应用开发高效工具集1---ant构建简单Android项目
本文转载于:http://blog.csdn.net/lincyang/article/details/40950153 在java编译那些事儿中提到了用ant去编译Java项目,今天扩展到用它来构建 ...
- Android TextView换行问题
本文转载于:http://niufc.iteye.com/blog/1729792 ndroid的TextView在显示文字的时候有个问题就是一行还没显示满就跳到下一行,原因是: 1) TextVie ...
- (转)The 9 Deep Learning Papers You Need To Know About (Understanding CNNs Part 3)
Adit Deshpande CS Undergrad at UCLA ('19) Blog About The 9 Deep Learning Papers You Need To Know Abo ...
- myBatis 参数配置
关于参数的类型 有四种类型: http://www.07net01.com/zhishi/402787.html 以上链接有所介绍,四种类型有:单个参数.多个参数.Map封装参数.List封装IN 如 ...
- SaaS、PaaS和IaaS
•SaaS(软件即服务) •PaaS(平台即服务) •IaaS(基础架构即服务)