The Doors

Time Limit: 1000 MS Memory Limit: 10000 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

Description

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length. 

Input

The input data for the illustrated chamber would appear as follows.
2  4 2 7 8 9  7 3 4.5 6 7
The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.

Output

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input

  1. 1
  2. 5 4 6 7 8
  3. 2
  4. 4 2 7 8 9
  5. 7 3 4.5 6 7
  6. -1

Sample Output

  1. 10.00
  2. 10.06
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7. #define maxx 100
  8. #define INF 10000000
  9.  
  10. struct Node
  11. {
  12. double x;
  13. double y;
  14. } p[maxx]; ///每扇门的终点 起点 和门的两个端点的平面坐标
  15.  
  16. struct EDGE
  17. {
  18. int u;
  19. int v;
  20. } Edge[maxx*maxx]; ///存构造的边 因为之前是孤立的点
  21.  
  22. int n; ///n个墙
  23. double wX[]; ///输入每堵墙的横坐标
  24. double py[][]; ///每堵墙横坐标对应的纵坐标 0 1 2 3
  25.  
  26. double g[maxx][maxx]; ///存邻接矩阵 配合dis[]的
  27. double dis[maxx]; ///beg到其他点的最短距离
  28.  
  29. int Psize; ///边的数量
  30. int Esize; ///点的数量
  31.  
  32. double Dis(Node a,Node b) ///计算亮点之间的距离
    {
  33. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  34. }
  35.  
  36. double cross(double x1,double y1,double x2,double y2,double x3,double y3) ///判断(x3,y3)与(x1,y1)(x2,y2)是否交叉
  37. {
  38. return (x2-x1)*(y3-y1)-(x3-x1)*(y2-y1);
  39. }
  40.  
  41. bool IsOk(Node a,Node b) ///判断两点之间是否可以连线
  42. {
  43. if(a.x>=b.x)
  44. return false;
  45. bool falg=true;
  46. int i=;
  47. while(wX[i]<=a.x&&i<n)
  48. i++;
  49. while(wX[i]<b.x&&i<n)
  50. {
  51. if(cross(a.x,a.y,b.x,b.y,wX[i],)*cross(a.x,a.y,b.x,b.y,wX[i],py[i][])<
  52. ||cross(a.x,a.y,b.x,b.y,wX[i],py[i][])*cross(a.x,a.y,b.x,b.y,wX[i],py[i][])<
  53. ||cross(a.x,a.y,b.x,b.y,wX[i],py[i][])*(cross(a.x,a.y,b.x,b.y,wX[i],))<)
  54. {
  55. falg=false;
  56. break;
  57. }
  58. i++;
  59. }
  60. return falg;
  61. }
  62.  
  63. double Bellman(int beg,int end)
  64. {
  65. for(int i=;i<maxx;i++)
  66. dis[i]=INF;
  67. dis[beg]=;
  68. bool EX=true;
  69. for(int i=;i<=Psize&&EX;i++)
  70. {
  71. EX=false;
  72. for(int j=;j<Esize;j++)
  73. {
  74. if(dis[Edge[j].u]<INF&&dis[Edge[j].v]>(dis[Edge[j].u]+g[Edge[j].u][Edge[j].v]))
  75. {
  76. dis[Edge[j].v]=(dis[Edge[j].u]+g[Edge[j].u][Edge[j].v]);
  77. EX=true;
  78. }
  79. }
  80. }
  81. return dis[end];
  82. }
  83.  
  84. int main()
  85. {
  86. while(scanf("%d",&n)!=EOF)
  87. {
  88. if(n==-)
  89. break;
  90. p[].x=;
  91. p[].y=;
  92. Psize=;
  93. for(int i=; i<n; i++)
  94. {
  95. cin>>wX[i];
  96. for(int j=; j<; j++)
  97. {
  98. p[Psize].x=wX[i];
  99. cin>>p[Psize].y;
  100. py[i][j]=p[Psize].y;
  101. Psize++;
  102. }
  103. }
  104. p[Psize].x=;
  105. p[Psize].y=;
  106. for(int i=; i<=Psize; i++)
  107. {
  108. for(int j=; j<=Psize; j++)
  109. {
  110. g[i][j]==INF;
  111. }
  112. }
  113. Esize=;
  114. for(int i=; i<=Psize; i++)
  115. {
  116. for(int j=i+; j<=Psize; j++)
  117. {
  118. if(IsOk(p[i],p[j]))
  119. {
  120. g[i][j]=Dis(p[i],p[j]);
  121. Edge[Esize].u=i;
  122. Edge[Esize].v=j;
  123. Esize++;
  124. }
  125. }
  126. }
  127. printf("%.2lf\n",Bellman(,Psize));
  128. }
  129. return ;
  130. }

poj 1556 The Doors的更多相关文章

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

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

  2. POJ 1556 The Doors 线段交 dijkstra

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

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

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

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

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

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

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

  6. POJ 1556 The Doors 线段判交+Dijkstra

    The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6734   Accepted: 2670 Descrip ...

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

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

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

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

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

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

随机推荐

  1. [winserver]设置Server2008R2远程桌面允许每个用户运行多个会话

    首先打开"服务器管理器",选择"角色"-在对话框右边部分选择"添加角色" 根据提示一步步安装即可. 然后在角色中找到"远程桌面服务 ...

  2. UVa 10300 - Ecological Premium

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  3. VC++ MFC子对话框建立与关闭

    主窗体 void CMoshiwindowDlg::OnButton1() { // TODO: Add your control notification handler code here CDi ...

  4. bs4的学习

    soup = BeautifulSoup(html,'html.parser') #'html.parser'是html解析器必须有soup.find_all("a")  #等价于 ...

  5. asp.net GridView控件的列属性

    BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...

  6. Linux/Unix双机建立信任教程

    Linux/Unix双机建立信任教程 一 需要建立信任关系的2台主机都执行生成密钥输入ssh-keygen -t rsa之后全部默认回车,这样就会在/root/.ssh下生成密钥文件 [root@pl ...

  7. PHP 判断是否为Get/Post/Ajax提交

    <?php PHP 判断是否为Get/Post/Ajax提交 /** * 是否是AJAx提交的 * @return bool */ function isAjax(){ if(isset($_S ...

  8. [转] 关于linux下通过shell命令(自动)修改用户密码

    关于linux下通过shell命令(自动)修改用户密码 2012-04-23 18:47:39 分类: 原文地址:关于linux下(自动)修改用户密码 作者:ubuntuer 本文章总结了如何手动.自 ...

  9. quick-cocos2d-x之testlua之mainMenu.lua

    require "helper" require "testResource" require "ActionsTest.ActionsTest&qu ...

  10. Odoo启动过程

    [本文基于odoo9源码编写] odoo包含的服务有 db object report workflow web[wsgi] Odoo以wsgi 规范提供Web及Web服务db/object/repo ...