枚举每个端点,然后i点j点连线作为一条路径,逐一判断这条路径是否可行即可

注意的地方:判一条线段是否可行,需要判其余线段是否和其相交,但是这个相交比较难判(因为会不规范相交),所以将问题转化为墙以外的线是否和其相交,所有墙以外的线都要和其相交!

//判断线段相交
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)) < ;
}
#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<<];
Point p[MAXN<<];
double dis[MAXN][MAXN];
const double INF = 1e20;
int n; int check(Line tmp){
for(int i=;i<=*n;i++)
if(inter(tmp,line[i]))return ;
return ;
} int main(){
double x,y1,y2,y3,y4;
while(scanf("%d",&n) == )
{
if(n == -) break;
p[]=Point(,),p[*n+]=Point(,);
for(int i = ;i <= n;i++)
{
scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
line[*i-] = Line(Point(x,),Point(x,y1));
line[*i-] = Line(Point(x,y2),Point(x,y3));
line[*i] = Line(Point(x,y4),Point(x,));
p[*i-]=Point(x,y1);
p[*i-]=Point(x,y2);
p[*i-]=Point(x,y3);
p[*i]=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++)
for(int j=i+;j<=*n+;j++){
Line tmp=Line(p[i],p[j]);
if(check(tmp))
dis[i][j]=dis[j][i]=dist(p[i],p[j]);
} 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 ;
}

计算几何——判线段规范相交+最短路zoj1721的更多相关文章

  1. POJ 2556 (判断线段相交 + 最短路)

    题目: 传送门 题意:在一个左小角坐标为(0, 0),右上角坐标为(10, 10)的房间里,有 n 堵墙,每堵墙都有两个门.每堵墙的输入方式为 x, y1, y2, y3, y4,x 是墙的横坐标,第 ...

  2. HDU 1558 Segment set (并查集+线段非规范相交)

    题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. #include <cstdio> #inclu ...

  3. 【计算几何初步-线段相交】【HDU1089】线段交点

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  4. POJ_1556_The Doors_判断线段相交+最短路

    POJ_1556_The Doors_判断线段相交+最短路 Description You are to find the length of the shortest path through a ...

  5. poj 1410 Intersection (判断线段与矩形相交 判线段相交)

    题目链接 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12040   Accepted: 312 ...

  6. codeForce-589D Boulevard(判断线段是否相交)

    题目大意:n个人.一个区间.每个人都会在某个时间段内按相同的速度(所有人的速度都一样,都是1或-1)在他的区间内从一个端点走到另一个端点(只走一次).问每个人会与几个人碰面. 题目分析:将时间看成一个 ...

  7. 计算几何--判断两条线段相交--poj 2653

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8862   Accepted: 3262 De ...

  8. poj1066--Treasure Hunt(规范相交)

    题目链接:点击打开链接 题目大意:一个正方形的墓葬内有n堵墙,每堵墙的两个顶点都在正方形的边界上,如今这些墙将墓葬切割成了非常多小空间,已知正方形内的一个点上存在宝藏,如今我们要在正方形的外面去得到宝 ...

  9. URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集

    F - Cycling Roads     Description When Vova was in Shenzhen, he rented a bike and spent most of the ...

随机推荐

  1. 2019-4-29-.NET-Standard

    title author date CreateTime categories .NET Standard lindexi 2019-4-29 12:7:26 +0800 2018-2-13 17:2 ...

  2. thrift 的一些相关知识

    thrift是一个很好用的跨语言的rpc框架. 但是其也有一些需要注意的问题: 第一: 发现其对于类型检查没有那么严格:    最近工作中发现是可以把一个int类型直接付给string,而没有任何wa ...

  3. could not load the tomcat server configuration ...

    参考文档:  https://blog.csdn.net/u013381651/article/details/51567758 问题解决. 原因是你把项目里的tomcat的server项目也关闭了, ...

  4. Apache Tomcat下载、安装、环境变量配置以及项目部署

    前言 针对在本地访问文件或资源出现的跨域问题,可以通过搭建本地服务器来解决,本篇随笔主要介绍通过搭建Apache Tomcat服务器来解决跨域.包括Apache Tomcat的下载.安装.环境变量的配 ...

  5. mysql数据分组

    创建分组 分组是在SELECT语句中的GROUP BY 子句中建立的. 例: SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY ...

  6. docker 命令操作

    1.停止所有的container,这样才能够删除其中的images: docker stop $(docker ps -a -q) 如果想要删除所有container的话再加一个指令: docker ...

  7. bootstrap img自适应

    img 添加class名img-responsive适配屏幕

  8. C中为什么不能用==比较字符串?

    通常的回答是,==比较的不是字符串的内容,它是在比较指针.或者说,==(或者!=)仅比较两个字符串的首地址,而不会比较字符串每个字符. 那其实接下来应该问的问题是,为什么会只比较首地址呢? 因为早期的 ...

  9. JUC 一 CountDownLatch(闭锁)

    java.util.concurrent 介绍 CountDownLatch是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待 CountDownLatch cou ...

  10. jQuery的两把利器

    1 jQuery核心函数 * 简称: jQuery函数($/jQuery) * jQuery库向外直接暴露的就是$/jQuery * 引入jQuery库后, 直接使用$即可 * 当函数用: $(xxx ...