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

解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短路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. ASP.NET Core1.0 带来的新特性

    1.采用新的文件系统,不再通过工程文件(.sln和.csproj)来定义项目文件清单. 解决方案文件还是*.sln,但项目文件变成*.xproj了.在项目文件夹下新增的文件会被自动添加到项目中,不用再 ...

  2. .NET Core 和 ASP.NET 5 RC1 发布

    昨天微软发布了 .NET Core 和 ASP.NET 5 候选版本,支持 Windows,Linux 和 OS X 平台,版本 License 为 "Go Live",,也就是说 ...

  3. [CLK Framework] CLK.Threading.PortableTimer - 跨平台的Timer类别

    [CLK Framework] CLK.Threading.PortableTimer - 跨平台的Timer类别 问题情景 开发应用程式的时候,免不了需要加入一些定时执行的设计,例如说:定时更新画面 ...

  4. Force.com微信开发系列(六)客服接口

    当用户主动发消息给微信公众账号的时候(包括发送信息.点击自定义菜单click事件.订阅事件.扫描二维码事件.支付成功事件.用户维权),微信将会把消息数据推送给开发者,开发者在一段时间内(目前为48小时 ...

  5. R语言学习笔记:分析学生的考试成绩

    孩子上初中时拿到过全年级一次考试所有科目的考试成绩表,正好可以用于R语言的统计分析学习.为了不泄漏孩子的姓名,就用学号代替了,感兴趣可以下载测试数据进行练习. num class chn math e ...

  6. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q57-Q59)

    Question 57You update a solution validator.You need to ensure that all SharePoint solutions are vali ...

  7. 详解Paint的setShader(Shader shader)

    一.概述 setShader(Shader shader)中传入的自然是shader对象了,shader类是Android在图形变换中非常重要的一个类.Shader在三维软件中我们称之为着色器,其作用 ...

  8. Cent OS 6.4安装mysql

    Cent OS6.4 RPM安装mysql 一.卸载掉原有mysql 因为目前主流Linux系统版本基本上都集成了mysql数据库在里面 如下命令来查看我们的操作系统上是否已经安装了mysql数据库 ...

  9. mybatis实战教程(mybatis in action),mybatis入门到精通(转)

    转自:http://www.yihaomen.com/article/java/302.htm (读者注:其实这个应该叫做很基础的入门一下下,如果你看过Hibernate了那这个就非常的简单) (再加 ...

  10. Objective-C之优雅的命名

    There are only two hard things in Computer Science: cache invalidation and naming things.在计算机科学中只有两件 ...