枚举每个端点,然后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. vue computed 源码分析

    我们来看看computed的实现.最简单的一个demo如下: <html> <head> <meta http-equiv="Content-Type" ...

  2. 深入vue

  3. CSS兼容问题资料汇集

    CSS兼容问题一直困扰着CSSer,面对各浏览器,往往感觉束手无策,愁眉不展.CSS Hack是在标准CSS没办法兼容各浏览器显示效果时才会用上的补救方法,在各浏览器厂商解析CSS没有达成一致前,我们 ...

  4. web APP 开发之踩坑手记

    屏蔽输入框怪异的内阴影 -webkit-appearance:none 禁止自动识别电话和邮箱 <meta content="telephone=no" name=" ...

  5. SolidWorks直线命令快捷转换为圆弧命令

    在进行草图绘制的时候,有时候需要切换到圆弧命令,此时来回切换比较麻烦, 我们可以将鼠标回碰线段起点,此时便成为了圆弧工具. 再次回碰,可改变圆心方向 利用鼠标操作,快捷切换绘图工具.

  6. 模拟+算贡献——cf1195D

    比赛的时候没看到模数,用java大数在写,最后看到的时候已经慌了.. 把贡献算清楚就可以 下面是贡献的推导 有五位数 abcde * 10个 有两位数 fg * 3 个 那么这两种数组成的情况就是 a ...

  7. weblogic重置控制台密码

    1.备份文件如下文件 %DOMAIN_HOME%/security/DefaultAuthenticatorInit.ldift 2.进入%DOMAIN_HOME%/security目录,执行下列命令 ...

  8. delphi 获取所有窗口标题

    unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  9. NX二次开发-UFUN重命名图纸页UF_DRAW_rename_drawing

    #include <uf.h> #include <uf_draw.h> #include <uf_drf.h> #include <uf_obj.h> ...

  10. hdu多校第九场 1006 (hdu6685) Rikka with Coin 暴力

    题意: 有一些1毛,2毛,5毛,1块的钢镚,还有一些价格不同的商品,现在要求你带一些钢镚,以保证这些商品中任选一件都能正好用这些钢镚付账,问最少带多少钢镚. 题解: 对于最优解,1毛的钢镚最多带1个, ...