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

梦里梦外; 2022-05-18 06:07 234阅读 0赞

题目链接

题目大意:有一个10*10的正方形房间中间用墙隔开,每个墙上有两个门给出门的两个端点的坐标求从左边中点走到右边中点所需要的最短路程。

分析:计算每个墙的端点和其他墙的端点的距离(包括起点和终点),如果中间没有墙挡住。则它们的距离就是它们的直线距离,如果中间有墙挡住,则它们之间的距离,设置为无法到达,然后用最短路跑一遍求得左边中点到右边中点的距离。

  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<cstdio>
  7. #include<vector>
  8. #include<iomanip>
  9. #include<utility>
  10. #include<cctype>
  11. #include<cstring>
  12. #include<cstdlib>
  13. #include<iostream>
  14. #include<algorithm>
  15. #define inf 0x3f3f3f3f
  16. #define Clear(x) memset(x,0,sizeof(x))
  17. #define fup(i,a,b) for(int i=a;i<b;i++)
  18. #define rfup(i,a,b) for(int i=a;i<=b;i++)
  19. #define fdn(i,a,b) for(int i=a;i>b;i--)
  20. #define rfdn(i,a,b) for(int i=a;i>=b;i--)
  21. typedef long long ll;
  22. using namespace std;
  23. const double pi=acos(-1.0);
  24. const int maxn = 1e2+7;
  25. const double eps = 1e-8;
  26. int vis[maxn];
  27. double dis[maxn],mp[maxn][maxn];
  28. int pcnt,lcnt;
  29. int sgn(double x)
  30. {
  31. if(fabs(x)<eps) return 0;
  32. else return x<0?-1:1;
  33. }
  34. struct Point{
  35. double x,y;
  36. Point(){}
  37. Point (double _x,double _y){x=_x;y=_y;}
  38. }p[maxn];
  39. struct Line{
  40. Point s,e;
  41. Line(){}
  42. Line(Point _s,Point _e){s=_s;e=_e;}
  43. }line[maxn];
  44. double cross(Point a,Point b,Point c)
  45. {
  46. a.x-=c.x;a.y-=c.y;
  47. b.x-=c.x;b.y-=c.y;
  48. return a.x*b.y-a.y*b.x;
  49. }
  50. double dist(Point a,Point b)
  51. {
  52. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  53. }
  54. bool intersect(Point a,Point b,Line l1)
  55. {
  56. int d1=sgn(cross(l1.e,b,a));
  57. int d2=sgn(cross(l1.s,b,a));
  58. int d3=sgn(cross(b,l1.s,l1.e));
  59. int d4=sgn(cross(a,l1.s,l1.e));
  60. if(d1*d2<0&&d3*d4<0)
  61. return true;
  62. return false;
  63. }
  64. bool check(int x,int y)
  65. {
  66. for(int i=0;i<lcnt;i++)
  67. {
  68. if(intersect(p[x],p[y],line[i]))
  69. return false;
  70. }
  71. return true;
  72. }
  73. double dijkstra()
  74. {
  75. Clear(vis);
  76. for(int i=0;i<=pcnt;i++)
  77. dis[i]=mp[0][i];
  78. vis[0]=1;
  79. int u;
  80. for(int i=0;i<=pcnt-1;i++)
  81. {
  82. double mmin = inf;
  83. for(int j=0;j<=pcnt;j++)
  84. {
  85. if(!vis[j]&&dis[j]<mmin)
  86. {
  87. mmin=dis[j];
  88. u=j;
  89. }
  90. }
  91. vis[u]=1;
  92. for(int j=0;j<=pcnt;j++)
  93. {
  94. if(dis[j]>dis[u]+mp[u][j])
  95. dis[j]=dis[u]+mp[u][j];
  96. }
  97. }
  98. return dis[pcnt];
  99. }
  100. int main()
  101. {
  102. int n;
  103. double x,y1,y2,y3,y4;
  104. while(scanf("%d",&n)&&n!=-1)
  105. {
  106. Clear(mp);
  107. pcnt=0,lcnt=0;
  108. p[pcnt++]=Point(0,5);
  109. for(int i=0;i<n;i++)
  110. {
  111. scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
  112. p[pcnt++]=Point(x,y1),p[pcnt++]=Point(x,y2),p[pcnt++]=Point(x,y3),p[pcnt++]=Point(x,y4);
  113. line[lcnt++]=Line(Point(x,0),p[pcnt-4]);
  114. line[lcnt++]=Line(p[pcnt-3],p[pcnt-2]);
  115. line[lcnt++]=Line(p[pcnt-1],Point(x,10));
  116. }
  117. p[pcnt]=Point(10,5);
  118. for(int i=0;i<=pcnt;i++)
  119. {
  120. for(int j=0;j<=pcnt;j++)
  121. {
  122. if(i==j) mp[i][j]=0;
  123. else if(check(i,j)) mp[i][j]=dist(p[i],p[j]);
  124. else mp[i][j]=inf;
  125. }
  126. }
  127. printf("%.2f\n",dijkstra());
  128. }
  129. return 0;
  130. }

发表评论

表情:
评论列表 (有 0 条评论,234人围观)

还没有评论,来说两句吧...

相关阅读