这题就是,处理出没两个点。假设能够到达,就连一条边,推断可不能够到达,利用线段相交去推断就可以。最后求个最短路就可以

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; struct Point {
double x, y;
Point() {}
Point(double x, double y) {
this->x = x;
this->y = y;
}
void read() {
scanf("%lf%lf", &x, &y);
}
}; typedef Point Vector; Vector operator - (Vector A, Vector B) {
return Vector(A.x - B.x, A.y - B.y);
} const double eps = 1e-8; int dcmp(double x) {
if (fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
} double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积 //能够不规范相交
bool SegmentProperIntersection2(Point a1, Point a2, Point b1, Point b2) {
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return max(a1.x, a2.x) >= min(b1.x, b2.x) &&
max(b1.x, b2.x) >= min(a1.x, a2.x) &&
max(a1.y, a2.y) >= min(b1.y, b2.y) &&
max(b1.y, b2.y) >= min(a1.y, a2.y) &&
dcmp(c1) * dcmp(c2) <= 0 && dcmp(c3) * dcmp(c4) <= 0;
} const int N = 25; int n; struct Ban {
Point p[4];
void read() {
double a, y[4];
scanf("%lf", &a);
for (int i = 0; i < 4; i++) {
scanf("%lf", &y[i]);
p[i] = Point(a, y[i]);
}
}
} b[N]; struct Edge {
int u, v;
double w;
Edge(){}
Edge(int u, int v, double w) {
this->u = u;
this->v = v;
this->w = w;
}
}; vector<Edge> g[N * 4]; double dist(Point a, Point b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
} void add_edge(int u, int v, double d) {
g[u].push_back(Edge(u, v, d));
g[v].push_back(Edge(v, u, d));
} bool judge(int l, int r, Point aa, Point bb) {
for (int i = l; i <= r; i++) {
if (!SegmentProperIntersection2(aa, bb, b[i].p[0], b[i].p[1]) && !SegmentProperIntersection2(aa, bb, b[i].p[2], b[i].p[3]))
return false;
}
return true;
} double d[N * 4];
int vis[N * 4]; double spfa(int s, int t) {
memset(vis, 0, sizeof(vis));
queue<int> Q;
for (int i = 0; i <= t; i++) d[i] = 1e20;
d[0] = 0;
vis[0] = 1;
Q.push(0);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
vis[u] = 0;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i].v;
double w = g[u][i].w;
if (d[u] + w < d[v]) {
d[v] = d[u] + w;
if (!vis[v]) {
vis[v] = 1;
Q.push(v);
}
}
}
}
return d[t];
} int main() {
while (~scanf("%d", &n) && n != -1) {
for (int i = 0; i <= n * 4 + 1; i++) g[i].clear();
for (int i = 0; i < n; i++)
b[i].read();
if (judge(0, n - 1, Point(0, 5), Point(10, 5)))
add_edge(0, n * 4 + 1, 10);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
if (judge(0, i - 1, Point(0, 5), b[i].p[j]))
add_edge(0, i * 4 + j + 1, dist(Point(0, 5), b[i].p[j]));
if (judge(i + 1, n - 1, b[i].p[j], Point(10, 5)))
add_edge(n * 4 + 1, i * 4 + j + 1, dist(Point(10, 5), b[i].p[j]));
for (int k = i + 1; k < n; k++) {
for (int x = 0; x < 4; x++) {
if (judge(i + 1, k - 1, b[i].p[j], b[k].p[x]))
add_edge(i * 4 + j + 1, k * 4 + x + 1, dist(b[i].p[j], b[k].p[x]));
}
}
}
}
printf("%.2f\n", spfa(0, n * 4 + 1));
}
return 0;
}

POJ 1556 The Doors(计算几何+最短路)的更多相关文章

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

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

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

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

  3. POJ 1556 The Doors 线段交 dijkstra

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

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

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

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

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

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

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

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

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

  8. POJ 1556 The Doors --几何,最短路

    题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少. 解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短 ...

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

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

随机推荐

  1. Microsoft.AlphaImageLoader滤镜讲--透明处理<转>

    Microsoft.AlphaImageLoader是IE滤镜的一种,其主要作用就是对图片进行透明处理.虽然FireFox和IE7以上的IE浏览器已经支持透明的PNG图片,但是就IE5-IE6而言还是 ...

  2. ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句

    ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句 存储过程 1.找到正在执行的存储过程的 sid ,serial# select   b.sid,b.SERIAL#,a.OBJEC ...

  3. ubuntu14.04 Markdown编辑器推荐之Remarkable

    如今已经习惯了用Markdown编辑器写博文的习惯,那么ubuntu以下有什么好用的呢?搜索中发现了这个叫Remarkable的免费Markdown编辑器.为什么推荐这个呢?说说它的特点: 实时预览 ...

  4. 简单C#文字转语音

    跟着微软走妥妥的,C#文字转语音有很多参数我就不说了,毕竟我也是初学者.跟大家分享最简单的方法,要好的效果得自己琢磨喽: 先添加引用System.Speech程序集: using System; us ...

  5. JavaScript之<noscript>标签简介

    早期浏览器都面临一个特殊的问题,即当浏览器不支持JavaScript时如何让页面平稳的退化.对这个问题的终极方案就是创造一个<noscript>元素,用以在不支持或支持但禁用了JavaSc ...

  6. UnicodeEncodeError: 'latin-1' codec can't encode character 解决sae flask 中文问题

    #encoding=utf-8 #中文编码支持 import MySQLdb from flask import Flask, g, request app = Flask(__name__) app ...

  7. js 数字

    var text = $("#iptNum").val(); if(isNaN(text)){ alert("不是数字"); } else{ alert(&qu ...

  8. SQL 语句优化—— (一) 操作符优化

    1.IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格.但是用IN的SQL性能总是比较低的,从Oracle执行的步骤来分析用IN的SQL与不用IN的SQL有以 ...

  9. 数据持久化------Archiving(归档,解档)

    其中TRPerson为自定义的继承自NSObject的类的子类  其中有两个属性,name 和 age .h文件 #import @interface TRPerson : NSObject<& ...

  10. 加深理解UIView,UIResponder,UIController

    转载出处:http://www.th7.cn/Program/IOS/201503/406514.shtml 原文地址==>自定义控件:http://objccn.io/issue-3-4/ 读 ...