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个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
随机推荐
- PHP和JAVASCRIPT判断访客终端是电脑还是手机
当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点.本文将介绍分别使用PHP和JAVASCRIPT代码判断用户终端类型. PHP ...
- bootstrap-js(3)滚动监听
导航条实例 ScrollSpy插件根据滚动的位置自动更新导航条中相应的导航项. 拖动下面区域的滚动条,使其低于导航条的位置,注意观察active类的变化.下拉菜单中的子项也会跟着变为高亮状态. 1.调 ...
- 根据老赵轻量级Actor进行修改的Actor模型
学习了老赵轻量级Actor模型,并在实际中使用,效果不错. 老赵轻量级Actor模型: ActorLite:一个轻量级Actor模型实现(上) ActorLite:一个轻量级Actor模型实现(中) ...
- spring与hibernate整合事务管理的理解
在谈Spring事务管理之前我们想一下在我们不用Spring的时候,在Hibernate中我们是怎么进行数据操作的.在Hibernate中我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据 ...
- 基于nginx+lua简单的灰度发布系统
upstream.conf upstream grey_1 { keepalive 1000; server localhost:8020; } upstream grey_2 { keepalive ...
- Android IT资讯网络阅读器应用源码
这个是Android IT资讯网络阅读器应用,也是一款通过jsoup解析Html获取内容的网络阅读器,和前面的其实是类似的,也是大学时期闲暇完成,对照CSDN的Web页面元素设计进行解析提取内容,核心 ...
- listbox横向排列
在Listbox中横向显示CheckBox 前台代码 <ListBox Height=" > <StackPanel x:Name="sp" Orien ...
- 'gbk' codec can't encode character
做爬虫抓取网页,print(html)进行调试,遇到UnicodeEncodeError: 'gbk' codec can't encode character XX in position XX问题 ...
- 走进C标准库(7)——"string.h"中函数的实现memcmp,memcpy,memmove,memset
我的memcmp: int memcmp(void *buf1, void *buf2, unsigned int count){ int reval; while(count && ...
- cocos2d-x -------之笔记篇 3D动作说明
CCShaky3D::create(时间,晃动网格大小,晃动范围,Z轴是否晃动); //创建一个3D晃动的效果 CCShakyTiles3D::create(时间,晃动网格大小,晃动范围,Z轴是 ...