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个宝物的位置.如今贼要将全部的宝物拿到手.问最 ...
随机推荐
- ASP.NET MVC 学习之路-2
本文在于巩固基础 为了方便理解MVC框架,我们先创建空的ASP.NET MVC模板 下面是创建后的项目结构 每个文件或者文件夹的作用 App_Data 应用程序数据--- 顾名思义是放置文件或者数据库 ...
- .net通用权限框架B/S (四)--DAL数据层以及数据接口
数据层以及数据接口设计如下图(以g_orga组织机构和g_role角色)为例,这几个类可以通过.tt模版生成 设计参考学习http://www.cnblogs.com/hanyinglong/arch ...
- SQL Server 备份维护计划
1. 创建维护计划:SSMS -> 管理 -> 维护计划 -> 新建维护计划 2. 添加子计划(备份计划) a) 每30分钟:事务日志备份 每天:差异备份 每周:完整备份 b) ...
- Guestinfo.hbm(1)The markup declarations contained or pointed to by the document type declaration must be well-formed
今天启动ssh项目是居然报错了,还提示要联网启动,看了看错误信息发现,肯定是Hibernate映射文件的声明头出错了,仔细一下: hbm.xml中的dtd头直接是连接www.hibernate.org ...
- tomcat 配置项目
tomcat\conf\Catalina\localhost 下面存在项目配置文件信息,emas.xml 表示你eclipse中有个emas项目, 还有其他的项目xml.
- 20160115--Hibernate
package com.hanqi.dao; import static org.junit.Assert.*; import java.util.*; import org.hibernate.se ...
- leetcode Binary Tree Postorder Traversal python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- php数据类型有哪些?
php数据类型有哪些?有三大类1.基本数据类型 1.1整型 $a = 0123; // 八进制数(是以0开头) 83 $a = 0x1A; // 十六进制数 26 1.2小数 ...
- mini-httpd源码分析-port.h
针对不同系统的宏定义,对于Linux而言 /* port.h - portability defines */ #elif defined(linux) # define OS_Linux # def ...
- Cocos2d-x 安装教程for mac(Xcode)
cocos2d v3.x 版本出来后,从配置安装到创建项目都是命令行,下面简单说一下. 1. 下载地址 http://cn.cocos2d-x.org/download/ (虽然没有标明 for ...