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 ...
随机推荐
- OpenCV学习(1)-安装(Windows)
下载安装 在这里下载.我下载了2.4.9的Windows版本.双击安装即可. 配置环境变量 配置环境变量的目的是为了让系统找到OpenCV的动态链接库.因此需要把动态链接库添加到系统环境变量PATH中 ...
- Entity Framework 的事务 DbTransaction
事务代码实现如下: public static void Transaction() { myitEntities entity = null; DbTransaction tran = null; ...
- c语言实现交换两个数的值
C语言中要实现交换两个数的值,可以有很多种方法,具体如下所述. 不使用中间变量: // 异或, a^=b^=a^=b; a ^= b; b ^= a; a ^= b; // 加减 a = a + b; ...
- input表单
submit:点击submit按钮表单就会被提交给服务器,中文IE下默认按钮文本为“提交查询”,可以设置value属性修改按钮的显示文本 text:size属性为宽度,value为值,maxlengt ...
- javascript 笔记(1)
document.forms[0].names.value == '' forms[0]表示文档的第一个 form 表单,names 表示这个表单元素名为 names 的表单的元素,此时表示的是获取此 ...
- 结构型模式(Structural patterns)->外观模式(Facade Pattern)
动机(Motivate): 在软件开发系统中,客户程序经常会与复杂系统的内部子系统之间产生耦合,而导致客户程序随着子系统的变化而变化.那么如何简化客户程序与子系统之间的交互接口?如何将复杂系统的内部子 ...
- Windows(Vertrigo)下Apache与Tomcat的整合
为了实现整合,上网找了很多资料!尝试了很多次,终于成功整合了!下面我就把整合过程分享给大家,希望能给以后有需要的人提供帮助. 准备工作: 1.VertrigoServ 2.23 下载地址: http ...
- html5写的一个时钟
看到的一个html5写的时钟 <!doctype> <html> <head> <script> window.onload=function(){ v ...
- SDUT 1305 查找基因序列问题 dp
题目: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1305 这个题就是一个类似公共子串的dp ...
- 解决Android SDK Manager更新(一个更新Host的程序的原理实现和源码)
<ignore_js_op> 同学遇到了更新Android SDK的问题,而且Goagent现在也无法用来更新.就想到了用替代Host的方法,添加可用的谷歌地址来实现更新. ...