计算几何——判线段规范相交+最短路zoj1721
枚举每个端点,然后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的更多相关文章
- POJ 2556 (判断线段相交 + 最短路)
题目: 传送门 题意:在一个左小角坐标为(0, 0),右上角坐标为(10, 10)的房间里,有 n 堵墙,每堵墙都有两个门.每堵墙的输入方式为 x, y1, y2, y3, y4,x 是墙的横坐标,第 ...
- HDU 1558 Segment set (并查集+线段非规范相交)
题目链接 题意 : 如果两个线段相交就属于同一集合,查询某条线段所属集合有多少线段,输出. 思路 : 先判断与其他线段是否相交,然后合并. #include <cstdio> #inclu ...
- 【计算几何初步-线段相交】【HDU1089】线段交点
You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/3 ...
- POJ_1556_The Doors_判断线段相交+最短路
POJ_1556_The Doors_判断线段相交+最短路 Description You are to find the length of the shortest path through a ...
- poj 1410 Intersection (判断线段与矩形相交 判线段相交)
题目链接 Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12040 Accepted: 312 ...
- codeForce-589D Boulevard(判断线段是否相交)
题目大意:n个人.一个区间.每个人都会在某个时间段内按相同的速度(所有人的速度都一样,都是1或-1)在他的区间内从一个端点走到另一个端点(只走一次).问每个人会与几个人碰面. 题目分析:将时间看成一个 ...
- 计算几何--判断两条线段相交--poj 2653
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8862 Accepted: 3262 De ...
- poj1066--Treasure Hunt(规范相交)
题目链接:点击打开链接 题目大意:一个正方形的墓葬内有n堵墙,每堵墙的两个顶点都在正方形的边界上,如今这些墙将墓葬切割成了非常多小空间,已知正方形内的一个点上存在宝藏,如今我们要在正方形的外面去得到宝 ...
- URAL 1966 Cycling Roads 点在线段上、线段是否相交、并查集
F - Cycling Roads Description When Vova was in Shenzhen, he rented a bike and spent most of the ...
随机推荐
- C语言static和局部变量
#include <stdio.h> void test(); int main() { /************************************************ ...
- session复制
环境描述:(三台服务器 系统:7.6)192.168.200.111 nginx192.168.200.112 tomcat192.168.200.113 tomcat环境配置:192.168.200 ...
- Centos Apache 80 代理Tomcat 8080端口
运行环境:Centos 6.5 Apache: 2.2.5 开启apache proxy的相应模块 编辑 /etc/httpd/conf/httpd.conf文件 sudo vim /etc/http ...
- 30 System类
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部.该类位于java.lang包.由于该类的构造方法是private的,所以无法创建该类的对象,也就是无法实例化该类.其内部的成员变量 ...
- git使用ssh连接服务器
git如何连接服务器呢? $ ssh -p 22 root@服务器ip 解释:ssh -p 端口号 登录的用户名@IP
- (getElementBy**)与 querySelector(querySelectorAll) 的区别
1. 通过类似于 document.getElementByTagName('div') 这种方式获取到的类数组,无法通过 forEach 进行遍历(可以通过for循环):而通过document.qu ...
- VS卸载不干净,再次安装盘符不能更改问题(转载)
下载文件,直接用. 链接:https://pan.baidu.com/s/1K1cbJUq_JC9DN2MoE6Z3RA 密码:cuad
- ionic-CSS:ionic checkbox(复选框)
ylbtech-ionic-CSS:ionic checkbox(复选框) 1.返回顶部 1. ionic checkbox(复选框) ionic 里面的 Checkbox 和普通的 Checkbox ...
- ashx后门[转]
https://www.cnblogs.com/Fluorescence-tjy/p/9855828.html 一.标准ASPX一句话木马 .NET平台下的一句话木马则百年不变,最常见的当属下面这句 ...
- scanf 与getchar区别
#include<stdio.h> void main() { int c; c=getchar(); //scanf("%c",&c); if(c!=' ...