hdu 4454 Stealing a Cake (三分)
Stealing a Cake
The big cake can be considered as a circle on a 2D plane. The ant’s home can be considered as a rectangle. The ant can walk through the cake. Please find out the shortest path for the poor ant.
The first line of each test case contains x,y, representing the coordinate of the starting point. The second line contains x, y, r. The center of the cake is point (x,y) and the radius of the cake is r. The third line contains x1,y1,x2,y2, representing the coordinates of two opposite vertices of the rectangle --- the ant's home.
All numbers in the input are real numbers range from -10000 to 10000. It is guaranteed that the cake and the ant's home don't overlap or contact, and the ant's starting point also is not inside the cake or his home, and doesn't contact with the cake or his home.
If the ant touches any part of home, then he is at home.
Input ends with a line of 0 0. There may be a blank line between two test cases.
-1 1 1
0 -1 1 0
0 2
-1 1 1
0 -1 1 0
0 0
2.00
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 105
#define MAXN 10005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 0.00000001
using namespace std; int n,m;
double x,y,xr,yr,r,x1,Y1,x2,y2,ans;
double le,ri,mid,mmid; double caldist(double xx1,double YY1,double xx2,double yy2)
{
return sqrt((xx1-xx2)*(xx1-xx2)+(YY1-yy2)*(YY1-yy2));
}
double getdist(double k)
{
int i,j;
double xx,yy,d1,d2;
xx=xr+r*cos(k);
yy=yr+r*sin(k);
d1=caldist(xx,yy,x,y);
d2=INF;
if(xx>=x1&&xx<=x2||yy>=Y1&&yy<=y2)
{
if(xx>=x1&&xx<=x2)
{
d2=min(d2,fabs(yy-Y1));
d2=min(d2,fabs(yy-y2));
}
else
{
d2=min(d2,fabs(xx-x1));
d2=min(d2,fabs(xx-x2));
}
}
else
{
d2=min(d2,caldist(xx,yy,x1,Y1));
d2=min(d2,caldist(xx,yy,x2,y2));
d2=min(d2,caldist(xx,yy,x1,y2));
d2=min(d2,caldist(xx,yy,x2,Y1));
}
return d1+d2;
}
void solve()
{
int i,j;
double d1,d2,tle=le,tri=ri;
while(ri-le>eps)
{
mid=(le+ri)/2.0;
mmid=(mid+ri)/2.0;
d1=getdist(mid);
d2=getdist(mmid);
if(d1<d2) ri=mmid;
else le=mid;
}
ans=d1;
le=tri;
ri=tle+2*pi;
while(ri-le>eps)
{
mid=(le+ri)/2.0;
mmid=(mid+ri)/2.0;
d1=getdist(mid);
d2=getdist(mmid);
if(d1<d2) ri=mmid;
else le=mid;
}
ans=min(ans,d1);
}
void presolve()
{
int i,j;
double xx1=x1,YY1=Y1,xx2=x2,yy2=y2,tmp;
x1=min(xx1,xx2);
Y1=min(YY1,yy2);
x2=max(xx1,xx2);
y2=max(YY1,yy2);
xx1=xr-x;
YY1=yr-y;
tmp=2*xx1/(2*sqrt(xx1*xx1+YY1*YY1));
le=pi/2+acos(tmp);
ri=le+pi;
}
int main()
{
int i,j;
while(scanf("%lf%lf",&x,&y))
{
if(x==0&&y==0) break ;
scanf("%lf%lf%lf%lf%lf%lf%lf",&xr,&yr,&r,&x1,&Y1,&x2,&y2);
presolve();
solve();
printf("%.2f\n",ans);
}
return 0;
}
hdu 4454 Stealing a Cake (三分)的更多相关文章
- hdu 4454 Stealing a Cake(三分之二)
pid=4454" target="_blank" style="">题目链接:hdu 4454 Stealing a Cake 题目大意:给定 ...
- HDU 4454 - Stealing a Cake(三分)
我比较快速的想到了三分,但是我是从0到2*pi区间进行三分,并且漏了一种点到边距离的情况,一直WA了好几次 后来画了下图才发现,0到2*pi区间内是有两个极值的,每个半圆存在一个极值 以下是代码 #i ...
- hdu 4454 Stealing a Cake(计算几何:最短距离、枚举/三分)
题意:已知起点.圆.矩形,要求计算从起点开始,经过圆(和圆上任一点接触即可),到达矩形的路径的最短距离.(可以穿过园). 分析:没什么好的方法,凭感觉圆上的每个点对应最短距离,应该是一个凸函数,用三分 ...
- hdu 4454 Stealing a Cake 三分法
很容易想到三分法求解,不过要分别在0-pi,pi-2pi进行三分. 另外也可以直接暴力枚举…… 代码如下: #include<iostream> #include<stdio.h&g ...
- hdu 4454 Stealing a Cake
简单的计算几何: 可以把0-2*pi分成几千份,然后找出最小的: 也可以用三分: #include<cstdio> #include<cmath> #include<al ...
- hdu 4454 Stealing a Cake(三分法)
给定一个起始点,一个矩形,一个圆,三者互不相交.求从起始点->圆->矩形的最短距离. 自己画一画就知道距离和会是凹函数,不过不是一个凹函数.按与水平向量夹角为圆心角求圆上某点坐标,[0, ...
- HDU 4454 Stealing a Cake(枚举角度)
题目链接 去年杭州现场赛的神题..枚举角度..精度也不用注意.. #include <iostream> #include <cstdio> #include <cstr ...
- HDU 4454 Stealing a Cake --枚举
题意: 给一个点,一个圆,一个矩形, 求一条折线,从点出发,到圆,再到矩形的最短距离. 解法: 因为答案要求输出两位小数即可,精确度要求不是很高,于是可以试着爆一发,暴力角度得到圆上的点,然后求个距离 ...
- hdu 4771 Stealing Harry Potter's Precious(bfs)
题目链接:hdu 4771 Stealing Harry Potter's Precious 题目大意:在一个N*M的银行里,贼的位置在'@',如今给出n个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
随机推荐
- java对象的内存布局(二):利用sun.misc.Unsafe获取类字段的偏移地址和读取字段的值
在上一篇文章中.我们列出了计算java对象大小的几个结论以及jol工具的使用,jol工具的源代码有兴趣的能够去看下.如今我们利用JDK中的sun.misc.Unsafe来计算下字段的偏移地址,一则验证 ...
- 服务器:RAID、AHCI、IDE
RAID 独立磁盘冗余阵列(RAID,redundant array of independent disks)是把相同的数据存储在多个硬盘的不同的地方(因此,冗余地)的方法.通过把数据放在多个硬盘上 ...
- web开发 - 从零开始 - 01 - 常见样式
1.width & height 2.background : a.background-color b.background-image:url() c.background-repea ...
- Spring 4.0 中的 WebSocket 架构
两年前,客户端与服务器端的全双工双向通信作为一个很重要的功能被纳入到WebSocket RFC 6455协议中.在HTML5中,WebSocket已经成为一个流行词,大家对这个功能赋予很多构想,很多时 ...
- iOS 任意类型数据转换字符串
//转换数据类型: NSError *parseError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:resp ...
- C#:判断一个String是否为数字
方案一:Try...Catch(执行效率不高)private bool IsNumberic(string oText){ try { ...
- doGet与doPost的区别
转自:http://blog.csdn.net/luoweifu/article/details/7865243 目录(?)[-] 不同点一 不同点二 输入表单inputhtml Serlvlet ...
- src 和 href 的区别
因为理解不深,到写外部加载Javascript文件或者css文件的时候总是需要去找个例子,这样可不好.现在总结下 href 属性规定被链接文档的位置(URL). href是hyperrefresh的缩 ...
- shell命令实战详解
1.解析路径获取文件名和目录名. 获取文件名 #awk解法:用“/”做分隔符,然后打印出最后的那一部分. resFile=`echo /tmp/csdn/zhengyi/test/adb.l ...
- codeforces 659E . New Reform 强连通
题目链接 对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0. 否则对答案贡献是1. #include <iostream> #include <vecto ...