poj 1556 The Doors(线段相交,最短路)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7430 | Accepted: 2915 |
Description

Input
2
4 2 7 8 9
7 3 4.5 6 7
The first line contains the number of interior walls. Then there is a
line for each such wall, containing five real numbers. The first number
is the x coordinate of the wall (0 < x < 10), and the remaining
four are the y coordinates of the ends of the doorways in that wall. The
x coordinates of the walls are in increasing order, and within each
line the y coordinates are in increasing order. The input file will
contain at least one such set of data. The end of the data comes when
the number of walls is -1.
Output
output should contain one line of output for each chamber. The line
should contain the minimal path length rounded to two decimal places
past the decimal point, and always showing the two decimal places past
the decimal point. The line should contain no blanks.
Sample Input
1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1
Sample Output
10.00
10.06
Source
【思路】
枚举所有点,如果不与竖边相交则连边,做最短路即可。
【代码】
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define FOR(a,b,c) for(int a=(b);a<=(c);a++)
using namespace std; const int N = +;
const double INF = 1e9;
const double eps = 1e-; int dcmp(double x) {
if(x<eps) return ; else return x<? -:;
} struct Pt {
double x,y;
Pt(double x=,double y=):x(x),y(y) {};
};
struct Seg { Pt a1,a2; };
typedef Pt vec; vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); }
bool operator != (Pt A,Pt B) {
if(dcmp(A.x-B.x)== && dcmp(A.y-B.y)==) return ;
else return ;
} double cross(Pt A,Pt B) { return A.x*B.y-A.y*B.x; } bool SegInter(Pt s1, Pt e1, Pt s2, Pt e2) {
if(
cross(e1-s1,s2-s1) * cross(e1-s1,e2-s1) <= &&
cross(e2-s2,s1-s2) * cross(e2-s2,e1-s2) <=
) return true;
return false;
}
double dist(Pt a,Pt b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double f[N][N];
Seg L[N]; int lc;
Pt P[N]; int pc;
int n; int main() {
while(scanf("%d",&n)== && n>) {
pc=lc=;
FOR(i,,n) {
double x,y1,y2,y3,y4;
scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
L[++lc]=(Seg) {Pt(x,),Pt(x,y1)};
L[++lc]=(Seg) {Pt(x,y2),Pt(x,y3)};
L[++lc]=(Seg) {Pt(x,y4),Pt(x,)};
P[++pc]=Pt(x,y1) , P[++pc]=Pt(x,y2);
P[++pc]=Pt(x,y3) , P[++pc]=Pt(x,y4);
}
P[++pc]=Pt(,), P[++pc]=Pt(,);
FOR(i,,pc) FOR(j,,pc) f[i][j]=INF;
FOR(i,,pc) FOR(j,i+,pc) {
bool flag=;
FOR(k,,lc)
if(SegInter(P[i],P[j],L[k].a1,L[k].a2))
{ flag=; break; }
if(flag)
f[i][j]=f[j][i]=dist(P[i],P[j]);
}
FOR(i,,n) {
FOR(j,i+,n) if(f[i][j]!=INF)
printf("%d,%d : %.2lf\n",i,j,f[i][j]);
}
FOR(k,,pc) FOR(i,,pc) FOR(j,,pc)
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
printf("%.2lf\n",f[pc-][pc]);
}
return ;
}
poj 1556 The Doors(线段相交,最短路)的更多相关文章
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- POJ 1556 The Doors(线段交+最短路)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- POJ 1556 计算几何 判断线段相交 最短路
题意: 在一个左下角坐标为(0,0),右上角坐标为(10,10)的矩形内,起点为(0,5),终点为(10,5),中间会有许多扇垂直于x轴的门,求从起点到终点在能走的情况下的最短距离. 分析: 既然是求 ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- POJ 1556 The Doors 线段判交+Dijkstra
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6734 Accepted: 2670 Descrip ...
- POJ 2556 (判断线段相交 + 最短路)
题目: 传送门 题意:在一个左小角坐标为(0, 0),右上角坐标为(10, 10)的房间里,有 n 堵墙,每堵墙都有两个门.每堵墙的输入方式为 x, y1, y2, y3, y4,x 是墙的横坐标,第 ...
- POJ 1556 The Doors --几何,最短路
题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少. 解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短 ...
- POJ_1556_The Doors_判断线段相交+最短路
POJ_1556_The Doors_判断线段相交+最短路 Description You are to find the length of the shortest path through a ...
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
随机推荐
- jQuery插件综合应用(一)注册
一.介绍 注册和登录是每个稍微有点规模的网站就应该有的功能.登陆功能与注册功能类似,也比注册功能要简单些.所以本文就以注册来说明jQuery插件的应用. jQuery插件的使用非常简单,如果只按照jQ ...
- 数组-去重、排序方法、json排序
1.数组去重 /*方法一: 1,'1' 会被认为是相同的; 所有hash对象,如:{x;1},{y:1}会被认为是相同的 //10ms */ Array.prototype.unique=functi ...
- PHP接口(interface)和抽象类(abstract)
interface 定义了一个接口类,它里面的方法其子类必须实现.接口是类的一个模板,其子类必须实现接口中定义的所有方法. interface User{ function getHeight ...
- Laravel学习第一天(创建laravel项目、路由、视图、blade模板)
创建laravel项目 composer create-project laravel/laravel learnlv 4.1.* 查看帮助:composer create-project 使用 ...
- 列表页url参数格式分析【求指教】
运营对列表页url制定静态化模式,与区区观点相悖.遂请大家指教点解. 动态参数包含6个,分别是: 1认证(有机),2品类(水果),3地区(丰台),4状态(众筹中),5排序(评分),6分页 使用状态非常 ...
- PowerDesigner 如何添加每个表中共用的字段及自动添加注释
PowerDesigner 如何添加每个表中共用的字段: 有时候在创建表的时候会有一些共用的字段,但是每一张表都要去创建,这样做很麻烦,特别是这样重复的工作,稍不留意就会出现问题,实际上在PD中有这样 ...
- 关于 jquery.showLoading 中 出现的 图标不在页面中间的问题
很多人喜欢 showLoading 因为 这个实在是太简单了直接 showLoading() hideLoading() 就可以解决这个问题. 今天我们就来看一下 这个插件里面的一个错误 或者说 ...
- JDK神坑:JAVA中Calendar的月份Month少1
很多朋友在使初次使用Calendar时,会发现月份莫名其妙对不上,显示的结果总是比预期中小1个月,检查好几遍也没发现程序有错,于是开始抓狂.其实这个时候,只要去看JDK就会明白问题所在.JDK告诉我们 ...
- win7 安装SQL Server 2005 开发版 图文教程
转自win7 安装SQL Server 2005 开发版 图文教程 ----------------------------写在安装前------------------------------ 一. ...
- Django自定义上传目录
由于数据库的upload_to功能,有时不能满足每次上传灵活自定义的需求, 基于DEF的上传,有时不能满足基于CLASS的视图要求, 于是,只好慢慢用土法实现. 当然,首先,要使用上传功能时,form ...