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 ...
随机推荐
- JNI学习总结
JNI学习总结 标签(空格分隔): java JNI:Java Native Interface,是一种通过java调用本地方法的技术(当然也可以反过来),随着JDK版本的提升,JNI的效率也一直在提 ...
- erlang 练手 进程环
Erlang 编程指南第四章 练习4-2 编写一个程序,生成N个进程并相连成环,启动环后绕环发送M个消息,当收到退出消息后终止. ringStart(M,N,Message, Cp) -> io ...
- ubuntu 安装flash插件
参考文献: http://wiki.debian.org.hk/w/Install_Flash_Player_with_APTapt-get install adobe-flashplugin
- 跨域技术-jsonp
JSONP是JSON with padding 的简写,其有两个部分组成,一个是回调函数,一个是数据,其基本格式如下 function handleResult(result){ alert(resu ...
- ezSQL 数据库操作类
http://justinvincent.com 官网,一切尽在里面的下载帮助文件中,妙哉也!! ez_sql_core.php <?php /************************* ...
- Sql中Rank排名函数
A.对分区中的行进行排名 以下示例按照数量对指定清单位置的清单中的产品进行了排名. 结果集按 LocationID 分区并在逻辑上按 Quantity 排序. 注意,产品 494 和 495 具有相同 ...
- php代码锁
在为台湾公司开发“保证金交易系统”的过程中,出现了这样的情况: 一个间银行有n个操作员,可以同时在系统中下单,系统需要判断银行的保证金是否足够来决定是否可以下单成功.账号保证金足够,正常下单,账号保证 ...
- JavaScript中将JSON的字符串解析成JSON数据格式
1.一种为使用eval()函数 var jsonObj=eval("("+data+")"); 2.使用Function对象来进行返回解析 var jsonst ...
- 【转】成为Java顶尖程序员 ,看这11本书就够了
成为Java顶尖程序员 ,看这11本书就够了 转自:http://developer.51cto.com/art/201512/503095.htm 以下是我推荐给Java开发者们的一些值得一看的好书 ...
- Swift开发之 ---- Swift宏定义
swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本 let IS_IOS7 = (UIDevice.currentDevice().systemVersion ...