Poj 1556 The Doors 计算几何+最短路

其实本题非常的无脑,无脑拍完1A,写到blog里只因为TM无脑拍也拍了很久啊= =

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
#include <iostream>
#include <map>
#include <cstdlib>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <sstream>

using namespace std;

typedef long long LL;
const int maxn = 2000;
const int maxm = 2000 * 2000;
const double INF = 1e30;
const double eps = 1e-7;
struct Point {
    double x,y;
    Point(double x = 0,double y = 0):x(x),y(y) {}
};

Point str(0,5),end(10,5);
vector<Point> p,pw;
double dist[maxn][maxn],d[maxn];
int N,M;

int dcmp(double x) {
    if(fabs(x) < eps) return 0;
    if(x < 0) return -1;
    return 1;
}

Point operator - (Point a,Point b) {
    return Point(a.x - b.x,a.y - b.y);
}

double Cross(Point a,Point b) {
    return a.x * b.y - a.y * b.x;
}

double Dot(Point a,Point b) {
    return a.x * b.x + a.y * b.y;
}

bool SegmentCross(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 dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
}

bool nonecross(Point a1,Point a2) {
    for(int i = 0;i < pw.size();i += 2) {
        if(SegmentCross(a1,a2,pw[i],pw[i + 1]))
            return false;
    }
    return true;
}

void solve() {
    //构建邻接矩阵
    M = p.size();
    for(int i = 0;i < M;i++) {
        for(int j = i + 1;j < M;j++) {
            if(nonecross(p[i],p[j])) {
                dist[i][j] = dist[j][i] =
                    sqrt(Dot(p[i]-p[j],p[i]-p[j]));
            } else dist[i][j] = INF;
        }
    }
    //bellman-ford
    for(int i = 0;i < M;i++) d[i] = INF;
    d[0] = 0;
    for(int i = 0;i < M;i++) {
        for(int j = 0;j < M;j++) {
            for(int k = 0;k < M;k++) if(dist[j][k] < INF) {
                if(d[j] < INF) {
                    d[k] = min(d[k],d[j] + dist[j][k]);
                }
            }
        }
    }
    printf("%.2f\n",d[M - 1]);
}

int main() {
    while(cin >> N,N != -1) {
        p.clear();
        pw.clear();
        p.push_back(Point(0,5));
        for(int i = 1;i <= N;i++) {
            double posx,posy;
            cin >> posx;
            pw.push_back(Point(posx,0));
            for(int j = 0;j < 4;j++) {
                cin  >> posy;
                p.push_back(Point(posx,posy));
                pw.push_back(Point(posx,posy));
            }
            pw.push_back(Point(posx,10));
        }
        p.push_back(Point(10,5));
        solve();
    }
    return 0;
}

  

Poj 1556 The Doors 计算几何+最短路,布布扣,bubuko.com

时间: 2024-12-30 15:54:57

Poj 1556 The Doors 计算几何+最短路的相关文章

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

POJ 1556 - The Doors题意:    在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少.    分析:        要么直达,要么一定是墙的边缘点之间以及起始点.终点的连线.        所以先枚举墙上每一点到其他点的直线可达距离,就是要判定该线段是否与墙相交(不含端点).        然后最短路. 1 #include <iostream> 2 #include <cstdio> 3 #include &l

POJ 1556 The Doors 线段交 dijkstra

LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个开口的两端点作为一个节点,再枚举点与点间能否直接到达(判相交),以此建图求最短路. /** @Date : 2017-07-11 16:17:31 * @FileName: POJ 1556 线段交+dijkstra 计算几何.cpp * @Platform: Windows * @Author :

POJ 1556 The Doors

计算几何+最短路 枚举线段是否相交建图,然后跑最短路 #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<algorithm> using namespace std; const int maxn=1000+10; const double eps=1e-8; int n; int totP,totL

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

●赘述题目 10*10的房间内,有竖着的一些墙(不超过18个).问从点(0,5)到(10,5)的最短路. 按照输入样例,输入的连续5个数,x,y1,y2,y3,y4,表示(x,0--y1),(x,y2--y3),(x,y4--10)是墙壁. ●题解 方法:建图(用到简单计算几何)+最短路 ○记录下每个端点. ○包含起点,终点,以及每个墙的可以走的端点,如下图: ○然后枚举点,尝试两两组合连(线段)边,若该线不会撞在墙上,即不会与墙壁线段相交,就add_adge(). 效果图如下: 如何判断呢?

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

题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少. 解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短路SPFA,即可得出答案. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <a

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

题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijkstra跑最短路.好题! /************************************************ * Author :Running_Time * Created Time :2015/10/24 星期六 09:48:49 * File Name :POJ_1556.cpp

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

#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <map> #include <vector> #include <set> #include <string> #include <math.h> using namespac

poj 1556 zoj1721 BellmanFord 最短路+判断直线相交

http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6120   Accepted: 2455 Description You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will a

最短路+线段交 POJ 1556 好题

1 // 最短路+线段交 POJ 1556 好题 2 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 3 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终点和之间的点:中间点之间:起点和终点的距离 4 // n最大为18所以直接n^3最短路 5 6 7 #include <cstdio> 8 #include <cstring> 9 #include <iostream> 10 #include <algo