hdu 3272 Mission Impossible

Mission Impossible

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 414    Accepted Submission(s): 178


Problem Description
There are A, B, C, D four kinds of resources, A can be obtained at any point in the X-axis. B can be obtained at any point in the Y-axis. C and D each will only be able to achieve at a certain point. Giving the coordinates of C and D, the coordinates of your current location. Find the shortest path to obtain the four kinds of resources and go back.
 

 

Input
The first line contains the number T of test cases(T<=150). Each of the following T lines consists of six integers cx, cy, dx, dy, hx and hy. (cx, cy) is the coordinates of resource C, (dx, dy) is the coordinates of resource D, (hx, hy) is the coordinates of your current location.
All the numbers of the input are in the range [-1000, 1000].
 

 

Output
Your program should produce a single line for each test case containing the length of the shortest path. The answers should be rounded to two digits after the decimal point.
 

 

Sample Input
3
1 1 2 2 3 3
1 1 -1 -1 -1 -1
1 -1 1 1 -1 1
 

 

Sample Output
8.49
5.66
6.83
 

 

Author
hanshuai
 
 
题意:给你C,D两点的坐标,再给起点H的坐标,A在x轴上任意位置,B在y轴上任意位置,要求从起点出发经过A,B,C,D四点后回到起点的最小距离。
 
有点麻烦的一道题,也是照着别人博客写的,每种情况都要分析。
 
用到了镜面定理:

当坐标在轴同一侧时,对其中一个点的该坐标取反,得到的新点求距离就是镜面反射的距离。

如果在不同侧,就直接求距离

 

附上代码:

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 using namespace std;
  6 
  7 struct Point
  8 {
  9     double x,y;
 10 }h,p[3];
 11 
 12 double mins(double a,double b)
 13 {
 14     return a<b?a:b;
 15 }
 16 
 17 double dis(Point a, Point b)  ///两点之间的距离
 18 {
 19     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
 20 }
 21 
 22 double cale()
 23 {
 24     int a=0,b=1;
 25     double ret=dis(h,p[a])+dis(p[a],p[b])+dis(p[b],h);
 26     double ha,ab,bh;
 27     bool cx=0,cy=0;
 28     if(h.x*p[a].x<=0||h.x*p[b].x<=0||p[a].x*p[b].x<=0)
 29     cx=1;
 30     if(h.y*p[a].y<=0||h.y*p[b].y<=0||p[a].y*p[b].y<=0)
 31     cy=1;
 32     if(!cx&&!cy) ///三个点在同一个坐标系(看不懂后面的计算,需要自己画图理解)
 33     {
 34         Point t1,t2;
 35         t1.x=h.x;
 36         t1.y=-h.y;
 37         t2.x=-p[a].x;
 38         t2.y=p[a].y;
 39         ha=ret-dis(h,p[a])+dis(t1,t2);
 40 
 41         t1.x=p[b].x;
 42         t1.y=-p[b].y;
 43         t2.x=-p[a].x;
 44         t2.y=p[a].y;
 45         ab=ret-dis(p[a],p[b])+dis(t1,t2);
 46 
 47         t1.x=p[b].x;
 48         t1.y=-p[b].y;
 49         t2.x=-h.x;
 50         t2.y=h.y;
 51         bh=ret-dis(h,p[b])+dis(t1,t2);
 52 
 53         double ans=mins(ha,mins(ab,bh));
 54 
 55         t1.x=p[a].x;
 56         t1.y=-p[a].y;
 57         t2.x=-p[a].x;
 58         t2.y=p[a].y;
 59         ans=mins(ans,ret-dis(p[a],h)+dis(t1,h)-dis(p[a],p[b])+dis(t2,p[b]));
 60         ans=mins(ans,ret-dis(p[a],p[b])+dis(t1,p[b])-dis(p[a],h)+dis(t2,h));
 61 
 62         t1.x=h.x;
 63         t1.y=-h.y;
 64         t2.x=-h.x;
 65         t2.y=h.y;
 66         ans=mins(ans,ret-dis(p[a],h)+dis(t1,p[a])-dis(h,p[b])+dis(t2,p[b]));
 67         ans=mins(ans,ret-dis(h,p[b])+dis(t1,p[b])-dis(p[a],h)+dis(t2,p[a]));
 68 
 69         t1.x=p[b].x;
 70         t1.y=-p[b].y;
 71         t2.x=-p[b].x;
 72         t2.y=p[b].y;
 73         ans=mins(ans,ret-dis(p[a],p[b])+dis(t1,p[a])-dis(h,p[b])+dis(t2,h));
 74         ans=mins(ans,ret-dis(h,p[b])+dis(t1,h)-dis(p[a],p[b])+dis(t2,p[a]));
 75 
 76         ret=ans;
 77     }
 78     else if(cx==1&&!cy)
 79     {
 80         Point tmp;
 81         tmp.x=p[a].x;
 82         tmp.y=-p[a].y;
 83         ha=ret-dis(h,p[a])+dis(h,tmp);
 84         ab=ret-dis(p[a],p[b])+dis(tmp,p[b]);
 85 
 86         tmp.x=p[b].x;
 87         tmp.y=-p[b].y;
 88         bh=ret-dis(h,p[b])+dis(h,tmp);
 89 
 90         ret=mins(ha,mins(ab,bh));
 91     }
 92     else if(!cx&&cy==1)
 93     {
 94         Point tmp;
 95         tmp.x=-p[a].x;
 96         tmp.y=p[a].y;
 97         ha=ret-dis(h,p[a])+dis(h,tmp);
 98         ab=ret-dis(p[a],p[b])+dis(tmp,p[b]);
 99 
100         tmp.x=-p[b].x;
101         tmp.y=p[b].y;
102         bh=ret-dis(h,p[b])+dis(h,tmp);
103 
104         ret=mins(ha,mins(ab,bh));
105     }
106 
107     return ret;
108 }
109 
110 int main()
111 {
112     int T;
113     scanf("%d",&T);
114     while(T--)
115     {
116         scanf("%lf%lf%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&h.x,&h.y);
117         printf("%.2lf\n",cale());
118     }
119     return 0;
120 }

 

posted @ 2016-07-29 17:10  lucky_少哖  阅读(311)  评论(1编辑  收藏  举报