POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图。求最短路。
思路很简单,但是实现比较复杂,模版一定要可靠。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
const int N=,M=N*N;
const double INF=0x3f3f3f3f;
const double eps=1e-;
int sgn(double x){
if(fabs(x)<eps) return ;
if(x>) return ;
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.x+y*b.y;
}
double operator ^(const point &b)const{
return x*b.y-y*b.x;
}
}po[N];
struct line{
point s,e;
line(){}
line(point s_,point e_){
s=s_,e=e_;
}
}li[N];
double dis(point a,point b){//距离
return sqrt((b-a)*(b-a));
}
double cal(point p0,point p1,point p2){//叉积
return (p1-p0)^(p2-p0);
}
int xj(line a,line b){//判断线段相交
point A=a.s,B=a.e,C=b.s,D=b.e;
return
max(A.x,B.x)>=min(C.x,D.x) &&
max(C.x,D.x)>=min(A.x,B.x) &&
max(A.y,B.y)>=min(C.y,D.y) &&
max(C.y,D.y)>=min(A.y,B.y) &&
sgn(cal(A,C,D))*sgn(cal(B,C,D))< &&
sgn(cal(C,A,B))*sgn(cal(D,A,B))<=;
}
//最短路部分
struct node{
int v,next;
double w;
}e[M];
int p[N],head[N],cnt,q[M],l,r,n;
double d[N];
void add(int u,int v,double w){
e[cnt].v=v,e[cnt].w=w;
e[cnt].next=head[u],head[u]=cnt++;
}
void spfa(){
l=r=;
memset(p,,sizeof(p));
for(int i=;i<=n;i++) d[i]=INF;
q[++r]=;d[]=;
int u,v,i;
double w;
while(l<r){
p[u=q[++l]]=;
for(i=head[u];i!=-;i=e[i].next){
w=e[i].w,v=e[i].v;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!p[v]){
q[++r]=v;
p[v]=;
}
}
}
}
printf("%.2f\n",d[n]);
} int main(){
int t,i,j,k,js;
double x,y;
while(scanf("%d",&t)!=EOF&&t!=-){
cnt=js=n=;po[]=point(,);
memset(head,-,sizeof(head));
//¹¹½¨µãºÍÏß
while(t--){
scanf("%lf",&x);
for(i=;i<=;i++){
scanf("%lf",&y);
po[++n]=point(x,y);
if(i==) li[++js]=line(point(x,),po[n]);
else if(i==) li[++js]=line(po[n],point(x,)) ;
else if(i==) li[++js]=line(po[n-],po[n]);
}
}
po[++n]=point(,);
//½¨Í¼
for(i=;i<=n;i++){
for(j=i+;j<=n;j++){
int f=;
for(k=;k<=js;k++){
if(xj(li[k],line(po[i],po[j]))){
f=;
break;
}
}
if(!f){
double tmp=dis(po[i],po[j]);
add(i,j,tmp);
add(j,i,tmp);
}
}
}
spfa();
}
return ;
}
POJ 1556 The Doors【最短路+线段相交】的更多相关文章
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
- POJ 1556 The Doors(线段交+最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5210 Accepted: 2124 Descrip ...
- POJ 1556 The Doors(线段交+最短路)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- poj 1556 zoj1721 BellmanFord 最短路+推断直线相交
http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- POJ 1556 The Doors(计算几何+最短路)
这题就是,处理出没两个点.假设能够到达,就连一条边,推断可不能够到达,利用线段相交去推断就可以.最后求个最短路就可以 代码: #include <cstdio> #include < ...
- ●POJ 1556 The Doors(简单计算几何+最短路)
●赘述题目 10*10的房间内,有竖着的一些墙(不超过18个).问从点(0,5)到(10,5)的最短路. 按照输入样例,输入的连续5个数,x,y1,y2,y3,y4,表示(x,0--y1),(x,y2 ...
- POJ1556 最短路 + 线段相交问题
POJ1556 题目大意:比较明显的题目,在一个房间中有几堵墙,直着走,问你从(0,5)到(10,5)的最短路是多少 求最短路问题,唯一变化的就是边的获取,需要我们获取边,这就需要判断我们想要走的这条 ...
随机推荐
- JS的window.location应用实例
window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. Window Locationwindow.location 对象在编写时可不使用 window ...
- [deviceone开发]-viewShower和listView
一.简介 viewshower里嵌套listview,实现复杂的列表效果. 二.效果图 三.相关下载 http://source.deviceone.net/source-detail.html?do ...
- HTML5移动开发学习笔记
最近做webapp项目过程中,发现坑还是挺多的,以下是网络收集一些开发中的常见问题及知识汇总,以便查阅,慢慢更新:). meta 基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 < ...
- AE常见接口之间的关系(较笼统)+arcgis常见概念
常见的接口有如下关系 IworkspaceFactory-------------->IworkSpace------------------>IfeatureWorkSpace ---- ...
- ArcGIS中国工具2.2正式发布
ArcGIS中国工具2.2新功能 1. 2.0全面支持ArcGIS10.3 2. 全面修改成插件,原来部分是独立运行的EXE 3. 可以制作倾斜的矩形图框 4. 修改宗地(地块)左上角为第一个点,填写 ...
- 对Xcode菜单选项的详细探索(干货)
本文调研Xcode的版本是 7.1,基本是探索了菜单的每一个按钮.虽然从xcode4一直用到了xcode7,但是一般都只是用了一些基础的功能,说来也惭愧.在一次偶然的机遇突然发现了“显示调用层级”的选 ...
- NSURLSession从网络上下载资源,此程序下载的是视频
#import "ViewController.h" @interface ViewController ()<NSURLSessionDelegate, NSURLSess ...
- 【C语言】C语言常量和变量
目录: [常量] · 定义 · 分类 · 特殊字符型常量 [变量] · 定义 · 定义变量 · 变量的使用 · 变量使用注意 · 变量常见问题 1.常量 · 定义 常量 ...
- 用css3绘制你需要的几何图形
1.圆形 示例: 思路:给任何正方形元素设置一个足够大的 border-radius ,就可以把它变成一个圆形.代码如下: html: <div class="size example ...
- Java数组的12个常用方法
以下是12个关于Java数组最常用的方法,它们是stackoverflow得票最高的问题. 声明一个数组 String[] aArray = new String[5]; String[] bArra ...