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

代码:

#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. Visual Studio 2008中控制台程序一闪而过的解决方法

    VS2008中编写C/C++的程序时,调试运行,控制台窗口会在执行完毕后立即关闭,这样就无法看到运行的结果.为了解决这个问题,可以使用①system("pause"); ②getc ...

  2. JAVA实现字符串反转,借助字符数组实现

    public static String reverseStr(String str) { int len = str.length(); char ch[] = str.toCharArray(); ...

  3. #include <string>

    1 append(string T&);字符串拼接 2 c_str string.c_str是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址. 3 empty() ...

  4. try,catch捕获错误的用法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <script&g ...

  5. twisted的一些代码

    之前用swoole(1.7.19)写的一段程序在数据量大的时候存在内存泄漏,改为twisted(15.4)实现,自测无误,记录如下(两者cpu占用率90%时吞吐rps能从120提升到1000). #! ...

  6. hibernate环境配置和使用

    一.hibernate简单介绍                Hibernate是一个开放源码的对象关系映射框架,它对JDBC进行了很轻量级的对象封装,使得Java程序猿能够随心所欲的使用对象编程思维 ...

  7. hdu3280Equal Sum Partitions (区间DP)

    Problem Description An equal sum partition of a sequence of numbers is a grouping of the numbers (in ...

  8. sqlserver字符串拆分(split)方法汇总

    --方法0:动态SQL法declare @s varchar(100),@sql varchar(1000)set @s='1,2,3,4,5,6,7,8,9,10'set @sql='select ...

  9. GWT RPC机制

    GWT RPC GWT RPCRemote Procedure Calls GWT: Google Web Toolkit的缩写,有了 GWT可以使用 Java 编程语言编写 AJAX 前端,然后 G ...

  10. hibernate sql查询后对象转换成实体类

    在多表查询的时候使用hibernate的sql查询的时候,一般返回的是object[]数组,或者可以使用  session.createSQLQuery(sql).setResultTransform ...