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四点后回到起点的最小距离。
 
有点麻烦的一道题,也是照着别人博客写的,每种情况都要分析。
 
用到了镜面定理:

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

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

附上代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; struct Point
{
double x,y;
}h,p[]; double mins(double a,double b)
{
return a<b?a:b;
} double dis(Point a, Point b) ///两点之间的距离
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} double cale()
{
int a=,b=;
double ret=dis(h,p[a])+dis(p[a],p[b])+dis(p[b],h);
double ha,ab,bh;
bool cx=,cy=;
if(h.x*p[a].x<=||h.x*p[b].x<=||p[a].x*p[b].x<=)
cx=;
if(h.y*p[a].y<=||h.y*p[b].y<=||p[a].y*p[b].y<=)
cy=;
if(!cx&&!cy) ///三个点在同一个坐标系(看不懂后面的计算,需要自己画图理解)
{
Point t1,t2;
t1.x=h.x;
t1.y=-h.y;
t2.x=-p[a].x;
t2.y=p[a].y;
ha=ret-dis(h,p[a])+dis(t1,t2); t1.x=p[b].x;
t1.y=-p[b].y;
t2.x=-p[a].x;
t2.y=p[a].y;
ab=ret-dis(p[a],p[b])+dis(t1,t2); t1.x=p[b].x;
t1.y=-p[b].y;
t2.x=-h.x;
t2.y=h.y;
bh=ret-dis(h,p[b])+dis(t1,t2); double ans=mins(ha,mins(ab,bh)); t1.x=p[a].x;
t1.y=-p[a].y;
t2.x=-p[a].x;
t2.y=p[a].y;
ans=mins(ans,ret-dis(p[a],h)+dis(t1,h)-dis(p[a],p[b])+dis(t2,p[b]));
ans=mins(ans,ret-dis(p[a],p[b])+dis(t1,p[b])-dis(p[a],h)+dis(t2,h)); t1.x=h.x;
t1.y=-h.y;
t2.x=-h.x;
t2.y=h.y;
ans=mins(ans,ret-dis(p[a],h)+dis(t1,p[a])-dis(h,p[b])+dis(t2,p[b]));
ans=mins(ans,ret-dis(h,p[b])+dis(t1,p[b])-dis(p[a],h)+dis(t2,p[a])); t1.x=p[b].x;
t1.y=-p[b].y;
t2.x=-p[b].x;
t2.y=p[b].y;
ans=mins(ans,ret-dis(p[a],p[b])+dis(t1,p[a])-dis(h,p[b])+dis(t2,h));
ans=mins(ans,ret-dis(h,p[b])+dis(t1,h)-dis(p[a],p[b])+dis(t2,p[a])); ret=ans;
}
else if(cx==&&!cy)
{
Point tmp;
tmp.x=p[a].x;
tmp.y=-p[a].y;
ha=ret-dis(h,p[a])+dis(h,tmp);
ab=ret-dis(p[a],p[b])+dis(tmp,p[b]); tmp.x=p[b].x;
tmp.y=-p[b].y;
bh=ret-dis(h,p[b])+dis(h,tmp); ret=mins(ha,mins(ab,bh));
}
else if(!cx&&cy==)
{
Point tmp;
tmp.x=-p[a].x;
tmp.y=p[a].y;
ha=ret-dis(h,p[a])+dis(h,tmp);
ab=ret-dis(p[a],p[b])+dis(tmp,p[b]); tmp.x=-p[b].x;
tmp.y=p[b].y;
bh=ret-dis(h,p[b])+dis(h,tmp); ret=mins(ha,mins(ab,bh));
} return ret;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf%lf%lf%lf",&p[].x,&p[].y,&p[].x,&p[].y,&h.x,&h.y);
printf("%.2lf\n",cale());
}
return ;
}

最新文章

  1. java容器的一些存取用法
  2. memset 究竟在做什么?
  3. 01传智_jbpm与OA项目_整体项目架构
  4. ReportNg 测试报告的定制修改【转】
  5. [Bootstrap]7天深入Bootstrap(1)入门准备
  6. Python科学计算&mdash;&mdash;前期准备
  7. MongoDB 学习笔记(python操作)
  8. 第五课,T语言转义字符()(版本5.0)
  9. UVA 539 The Settlers of Catan dfs找最长链
  10. linux 声音大小调整的命令
  11. 电子工程师名片——UFI Command,USB盘符的显示
  12. activity横屏竖屏的切换
  13. 面试题-Java基础-开发环境
  14. SpringCloud的DataRest(一)
  15. SQL语句完整的执行顺序(02)
  16. 搭建web定时任务管理平台
  17. vim中行末去掉^M
  18. 转载:Unity3D游戏对象消失enabled、Destroy与active的区别
  19. jquery接触初级----- 一种新奇的选择器用法
  20. SCP对拷如何连接指定端口远程主机

热门文章

  1. angular4 动态创建组件 vs 动态创建模板
  2. 元素 XXXX 的前缀 &quot;mvc&quot; 未绑定
  3. Docker容器中MySQL最大连接数被限制为214的解决方案
  4. GYM 101350 F. Monkeying Around
  5. Vue--vue中的组件、组件绑定事件和数据、私有组件、子组件,父子组件参数互传
  6. Laravel 精选资源大全
  7. Linux C/C++开发
  8. 2019-8-31-dotnet-core-用值初始化整个数组
  9. BZOJ 1925地精部落题解
  10. vue-cli3 关闭eslint