题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少。

解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短路SPFA,即可得出答案。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#define Mod 1000000007
#define eps 1e-8
using namespace std;
#define N 100017 struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
bool operator < (const Line &L)const { return ang < L.ang; }
};
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); } double DisP(Point A,Point B){
return Length(B-A);
}
bool SegmentIntersection(Point A,Point B,Point C,Point D) {
if(dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) < && dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) < ) return true;
return false;
}
//data segment
struct node{
Point P[];
}line[];
Point p[];
vector<pair<int,double> > G[];
double dis[];
int vis[],tot,Ltot,S,E;
//data ends void SPFA()
{
for(int i=;i<=tot;i++) dis[i] = Mod;
memset(vis,,sizeof(vis));
queue<int> q;
q.push(S);
vis[S] = , dis[S] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i].first;
double w = G[u][i].second;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if(!vis[v]) { q.push(v), vis[v] = ; }
}
}
}
} int main()
{
int n,i,j,k,h;
double x,a,b,c,d;
while(scanf("%d",&n)!=EOF && n!=-)
{
tot = ,Ltot = ;
p[] = Point(,);
for(i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf%lf",&x,&a,&b,&c,&d);
p[++tot] = Point(x,a);
p[++tot] = Point(x,b);
p[++tot] = Point(x,c);
p[++tot] = Point(x,d);
line[++Ltot].P[] = Point(x,), line[Ltot].P[] = Point(x,a);
line[++Ltot].P[] = Point(x,b), line[Ltot].P[] = Point(x,c);
line[++Ltot].P[] = Point(x,d), line[Ltot].P[] = Point(x,);
}
p[++tot] = Point(,);
Point A,B;
for(i=;i<=tot;i++) G[i].clear();
for(i=;i<=tot;i++) //start
{
for(j=i+;j<=tot;j++) //end
{
A = p[i], B = p[j];
for(k=;k<=Ltot;k++)
{
if(SegmentIntersection(A,B,line[k].P[],line[k].P[]))
break;
}
if(k == Ltot+)
{
G[i].push_back(make_pair(j,DisP(A,B)));
G[j].push_back(make_pair(i,DisP(A,B)));
}
}
}
S = , E = tot;
SPFA();
printf("%.2f\n",dis[E]);
}
return ;
}

POJ 1556 The Doors --几何,最短路的更多相关文章

  1. POJ 1556 The Doors【最短路+线段相交】

    思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...

  2. POJ 1556 The Doors(计算几何+最短路)

    这题就是,处理出没两个点.假设能够到达,就连一条边,推断可不能够到达,利用线段相交去推断就可以.最后求个最短路就可以 代码: #include <cstdio> #include < ...

  3. POJ 1556 - The Doors 线段相交不含端点

    POJ 1556 - The Doors题意:    在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少.    分析:        要么直达,要么 ...

  4. POJ 1556 The Doors 线段交 dijkstra

    LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...

  5. 简单几何(线段相交+最短路) POJ 1556 The Doors

    题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...

  6. POJ 1556 - The Doors - [平面几何+建图spfa最短路]

    题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...

  7. POJ 1556 The Doors(线段交+最短路)

    The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5210   Accepted: 2124 Descrip ...

  8. poj 1556 The Doors(线段相交,最短路)

      The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7430   Accepted: 2915 Descr ...

  9. ●POJ 1556 The Doors(简单计算几何+最短路)

    ●赘述题目 10*10的房间内,有竖着的一些墙(不超过18个).问从点(0,5)到(10,5)的最短路. 按照输入样例,输入的连续5个数,x,y1,y2,y3,y4,表示(x,0--y1),(x,y2 ...

随机推荐

  1. ServiceStack.Text反序列化lowercase_underscore_names格式的JSON

    代码: [Test] public void Test() { JsConfig.PropertyConvention = JsonPropertyConvention.Lenient; var js ...

  2. ASP.NET Web API 特性

    ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP.NET Web API 也是构建 RES ...

  3. android的Project has no default.properties file! Edit the project properties to set one. 的解决

    网上找来这种方法基本解决: 在我们导入Android工程时,有时候会出现如题所述的错误,打开工程目录可以看到,目录下的default.properties文件没有了或者多出了一个project.pro ...

  4. [Tips] Useful link ... on going

    1. CSS Bootstrap http://getbootstrap.com/ Bootstrap 中文文档 http://getbootstrap.com/2.3.2/ 最全的 Twitter ...

  5. Oauth笔记

    上周的工作有安全验证这一块,但不懂,只知道有几个关键字Oauth.secret-key .token.签名等.今天就查下资料做笔记. Oauth是什么 不依靠用户账号和密码就能获得访问资源权限 本质: ...

  6. JS框架的一些小总结

    闭包结构 为了防止和别的库的冲突,用闭包把整个框架安全地保护好. 我们待会的代码都写在里面.这里创建一个全局变量"window.O",就是在window对象里加个O,它等价于 &q ...

  7. Android项目实战(十四):TextView显示html样式的文字

    项目需求: TextView显示一段文字,格式为:(消息个数,不确定)条消息 这段文字中名字和数字的长度是不确定的,还要求名字和数字各自有各自的颜色. 一开始我想的是用(转) SpannableStr ...

  8. 操作数据库系统(OLTP)和联机分析处理系统(OLAP)的区别

    联机操作数据库系统的主要任务是执行联机事务和查询处理.这种系统称为联机事务处理(OnLine Transaction Processing,OLTP) 系统.它们涵盖了单位的大部分日常操作,如购物,库 ...

  9. 如何发布得到.ipa文件

    第一个方法: 如果都有证书的话,并且又不想把别人的机器添加到测试设备中,或者感觉获取UDID麻烦的话,那么就可以采用该方法了. 直接Archive应用程序: 右键显示包内容到product下复制里面的 ...

  10. Python学习二---字符串

    一.字符串 1.1.字符串和转义字符 转义字符需要使用\来表示 1.2.字符串连接 print 字符串1 字符串2,打印出来的字符串直接连接在一起没有空格 print 字符串1,字符串2,打印出来的字 ...