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

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <cmath>
  11. #include <algorithm>
  12. using namespace std;
  13.  
  14. struct Point {
  15. double x, y;
  16. Point() {}
  17. Point(double x, double y) {
  18. this->x = x;
  19. this->y = y;
  20. }
  21. void read() {
  22. scanf("%lf%lf", &x, &y);
  23. }
  24. };
  25.  
  26. typedef Point Vector;
  27.  
  28. Vector operator - (Vector A, Vector B) {
  29. return Vector(A.x - B.x, A.y - B.y);
  30. }
  31.  
  32. const double eps = 1e-8;
  33.  
  34. int dcmp(double x) {
  35. if (fabs(x) < eps) return 0;
  36. else return x < 0 ? -1 : 1;
  37. }
  38.  
  39. double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积
  40.  
  41. //能够不规范相交
  42. bool SegmentProperIntersection2(Point a1, Point a2, Point b1, Point b2) {
  43. double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
  44. c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
  45. return max(a1.x, a2.x) >= min(b1.x, b2.x) &&
  46. max(b1.x, b2.x) >= min(a1.x, a2.x) &&
  47. max(a1.y, a2.y) >= min(b1.y, b2.y) &&
  48. max(b1.y, b2.y) >= min(a1.y, a2.y) &&
  49. dcmp(c1) * dcmp(c2) <= 0 && dcmp(c3) * dcmp(c4) <= 0;
  50. }
  51.  
  52. const int N = 25;
  53.  
  54. int n;
  55.  
  56. struct Ban {
  57. Point p[4];
  58. void read() {
  59. double a, y[4];
  60. scanf("%lf", &a);
  61. for (int i = 0; i < 4; i++) {
  62. scanf("%lf", &y[i]);
  63. p[i] = Point(a, y[i]);
  64. }
  65. }
  66. } b[N];
  67.  
  68. struct Edge {
  69. int u, v;
  70. double w;
  71. Edge(){}
  72. Edge(int u, int v, double w) {
  73. this->u = u;
  74. this->v = v;
  75. this->w = w;
  76. }
  77. };
  78.  
  79. vector<Edge> g[N * 4];
  80.  
  81. double dist(Point a, Point b) {
  82. double dx = a.x - b.x;
  83. double dy = a.y - b.y;
  84. return sqrt(dx * dx + dy * dy);
  85. }
  86.  
  87. void add_edge(int u, int v, double d) {
  88. g[u].push_back(Edge(u, v, d));
  89. g[v].push_back(Edge(v, u, d));
  90. }
  91.  
  92. bool judge(int l, int r, Point aa, Point bb) {
  93. for (int i = l; i <= r; i++) {
  94. if (!SegmentProperIntersection2(aa, bb, b[i].p[0], b[i].p[1]) && !SegmentProperIntersection2(aa, bb, b[i].p[2], b[i].p[3]))
  95. return false;
  96. }
  97. return true;
  98. }
  99.  
  100. double d[N * 4];
  101. int vis[N * 4];
  102.  
  103. double spfa(int s, int t) {
  104. memset(vis, 0, sizeof(vis));
  105. queue<int> Q;
  106. for (int i = 0; i <= t; i++) d[i] = 1e20;
  107. d[0] = 0;
  108. vis[0] = 1;
  109. Q.push(0);
  110. while (!Q.empty()) {
  111. int u = Q.front();
  112. Q.pop();
  113. vis[u] = 0;
  114. for (int i = 0; i < g[u].size(); i++) {
  115. int v = g[u][i].v;
  116. double w = g[u][i].w;
  117. if (d[u] + w < d[v]) {
  118. d[v] = d[u] + w;
  119. if (!vis[v]) {
  120. vis[v] = 1;
  121. Q.push(v);
  122. }
  123. }
  124. }
  125. }
  126. return d[t];
  127. }
  128.  
  129. int main() {
  130. while (~scanf("%d", &n) && n != -1) {
  131. for (int i = 0; i <= n * 4 + 1; i++) g[i].clear();
  132. for (int i = 0; i < n; i++)
  133. b[i].read();
  134. if (judge(0, n - 1, Point(0, 5), Point(10, 5)))
  135. add_edge(0, n * 4 + 1, 10);
  136. for (int i = 0; i < n; i++) {
  137. for (int j = 0; j < 4; j++) {
  138. if (judge(0, i - 1, Point(0, 5), b[i].p[j]))
  139. add_edge(0, i * 4 + j + 1, dist(Point(0, 5), b[i].p[j]));
  140. if (judge(i + 1, n - 1, b[i].p[j], Point(10, 5)))
  141. add_edge(n * 4 + 1, i * 4 + j + 1, dist(Point(10, 5), b[i].p[j]));
  142. for (int k = i + 1; k < n; k++) {
  143. for (int x = 0; x < 4; x++) {
  144. if (judge(i + 1, k - 1, b[i].p[j], b[k].p[x]))
  145. add_edge(i * 4 + j + 1, k * 4 + x + 1, dist(b[i].p[j], b[k].p[x]));
  146. }
  147. }
  148. }
  149. }
  150. printf("%.2f\n", spfa(0, n * 4 + 1));
  151. }
  152. return 0;
  153. }

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. Google Chrome浏览器的使用方法

    Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.html ] 在Google Chr ...

  2. linux win7双系统

    真恨我自己啊,刚在linux下写了这个博客,因为没有分类,添加了个linux分类.按了F5刷没了.靠,哪里有心情复述啊 一直想装直接装linux系统,现在实现他,以后也要跟上linux的笔记,不然都对 ...

  3. 使用WinSetupFromUSB来U盘安装windowsXP(不使用win PE系统)

    目前用U盘安装XP的多数方法都要借助WINPE,比较麻烦.使用WinSetupFromUSB只需要下载一个6.5MB的绿色软件就可以制作好windows xp的安装U盘,方便简捷. WinSetupF ...

  4. dom元素和方法总结

    主要是参考<精通javascript>. 全局变量有: document.这个变量包含浏览器的html dom文档的引用. HTMElement 这个变量是所要html dom 元素的超类 ...

  5. 关于iOS7越狱的整理

    目前越狱非常的不稳定,已经白苹果第三次了.中途遇见了不少问题,去各大论坛找了下解决办法,算是搬运工. iOS7越狱过程中打开手机上的“evasi0n7”闪退,怎么办?1. 请先尝试卸载手机“evasi ...

  6. MyEclipse配色字体等配置的解决方案

    Myeclipse黑色配色方案(精心修改版) http://download.csdn.net/detail/rehongchen/6579945 如何改变Myeclipse编辑区背景色 http:/ ...

  7. Android开发实例之闹钟提醒

    本实例通过TimePickerDialog时间选择对话框让用户设置闹钟.并通过AlarmManager全局定时器在指定的时间启动闹钟Activity . 程序执行效果图: 实例代码: package ...

  8. jeecms 2012 源码分析(2) 前台栏目页静态化分析

    还是要说到web.xml文件 <welcome-file-list> <welcome-file>index.html</welcome-file> <wel ...

  9. Android Navigation Drawer(导航抽屉)

    Google I/O 2013 Android 更新了Support库,新版本的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建  Navigation ...

  10. android 抽屉式滑动demo

    下载地址:https://github.com/asijack/AndroidDrawerDemo 直接上效果图如下: 是不是还不错的样子. 先看看布局文件吧 <android.support. ...