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

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

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath>
  4 #include <cstring>
  5 using namespace std;
  6 const double INF = 1e10;
  7 const double eps = 1e-10;
  8 int dcmp(double x)
  9 {
 10     return fabs(x) < eps ? 0 : (x < 0 ? -1 : 1);
 11 }
 12 struct Point
 13 {
 14     double x,y;
 15     Point(double x1 = 0,double y1 = 0) : x(x1), y(y1) {}
 16 };
 17 Point operator - (Point a,Point b)
 18 {
 19     return Point(a.x - b.x, a.y - b.y);
 20 }
 21 double Det(Point a,Point b)
 22 {
 23     return a.x * b.y - a.y * b.x;
 24 }
 25 double Dot(Point a,Point b)
 26 {
 27     return a.x * b.x + a.y * b.y;
 28 }
 29 double Length(Point a)
 30 {
 31     return sqrt(Dot(a, a));
 32 }
 33 bool OnSegment(Point p, Point a1, Point a2)
 34 {
 35     return dcmp(Det(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p) ) <= 0;
 36 }
 37 struct Line
 38 {
 39     Point s,e;
 40     Line() {}
 41     Line(Point s1, Point e1) : s(s1), e(e1) {}
 42 };
 43 bool SegCross(Point a1, Point a2, Point b1, Point b2)
 44 {
 45     double c1 = Det(a2 - a1, b1 - a1);
 46     double c2 = Det(a2 - a1, b2 - a1);
 47     double c3 = Det(b2 - b1, a1 - b1);
 48     double c4 = Det(b2 - b1, a2 - b1);
 49     if(dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0) return 1;
 50     else return 0;
 51 }
 52
 53 int n;
 54 Line l[100];
 55 Point p[100];
 56 double map[100][100];
 57 int main()
 58 {
 59     while (~scanf("%d", &n) && n != -1)
 60     {
 61         p[0] = Point(0, 5);
 62         p[1] = Point(10, 5);
 63         int t = 2, m = 0;
 64         for (int i = 1; i <= n; i++)
 65         {
 66             double x; Point p1,p2,p3,p4;
 67             scanf("%lf%lf%lf%lf%lf",&x, &p1.y, &p2.y, &p3.y, &p4.y);
 68             p1.x = p2.x = p3.x = p4.x = x;
 69             l[m++] = Line(Point(x,0),p1);
 70             l[m++] = Line(p2, p3);
 71             l[m++] = Line(p4, Point(x,10));
 72             p[t++] = p1; p[t++] = p2; p[t++] = p3; p[t++] = p4;
 73         }
 74         for (int i = 0; i < t; i++)
 75             for (int j = 0; j < t; j++)
 76                 map[i][j] = INF;
 77         for (int i = 0; i < t; i++)
 78         {
 79             for (int j = i+1; j < t; j++)
 80             {
 81                 if (p[i].x == p[j].x) continue;
 82                 bool flag = 1;
 83                 for (int k = 0; k < m; k++)//枚举墙
 84                 {
 85                     if(SegCross(p[i], p[j], l[k].e, l[k].s))
 86                     {
 87                         flag = 0; break;
 88                     }
 89                 }
 90                 if (flag)
 91                 {
 92                     map[i][j] = map[j][i] = Length(p[i]-p[j]);
 93                 }
 94
 95             }
 96         }
 97         for(int i = 0; i < t; i++) map[i][i] = 0;
 98         for (int k = 0; k < t; k++)
 99             for (int i = 0; i < t;i++)
100                 for (int j = 0; j < t; j++)
101                     map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
102         printf("%.2f\n",map[0][1]);
103     }
104 }

时间: 2024-12-20 13:07:04

POJ 1556 - The Doors 线段相交不含端点的相关文章

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 <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 3304 直线与线段相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12161   Accepted: 3847 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

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

其实本题非常的无脑,无脑拍完1A,写到blog里只因为TM无脑拍也拍了很久啊= = #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdl

POJ 1410 Intersection(线段相交&amp;&amp;判断点在矩形内&amp;&amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段完全在矩形内部算相交:线段与矩形任意一条边不规范相交算相交. 思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑. 1 struct Point 2 { 3 double x, y; 4 } A, B, C, D; 5 struct Line 6 { 7 Point a, b; 8 } L; 9 10 int n; 11

POJ 1410 Intersection(线段相交&amp;amp;&amp;amp;推断点在矩形内&amp;amp;&amp;amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段全然在矩形内部算相交:线段与矩形随意一条边不规范相交算相交. 思路:知道详细的相交规则之后题事实上是不难的,可是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,须要又一次推断一下...真坑. struct Point { double x, y; } A, B, C, D; struct Line { Point a, b; } L; int n; double xmult(Point p1

简单几何(线段相交+最短路) 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 1127 Jack Straws ( 求直线交点, 判断线段是否相交(含端点) )

题目:传送门 题意: 给你 n 条线段的两个端点, 然后有多次询问, 每次询问, 问你线段 x 和 线段 y 是否相交. 若线段 A 和线段 B 相交且线段 A 和线段 C 相交,那么线段 B 和线段 C 相交.     1 < n < 13 题解: 暴力求线段是否相交, 然后再跑个 Floyd 或者并查集都可以的. #include <iostream> #include <stdio.h> #include <string.h> #include <

POJ 2653 Pick-up sticks [线段相交 迷之暴力]

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12861   Accepted: 4847 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin