hdu 4454 Stealing a Cake

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4454


题目大意:

给一起点,一个圆,一个平行于坐标轴的矩形,在二维坐标系中,求从起点经过圆(接触边缘也可,也可以穿过圆)再到矩形(碰到边缘即可)的最短距离。


题目思路:

先说这题暴力枚举角度即可,精度0.01就能A了,但是显然这有点。。。。。。

不过也不难想到三分角度得到圆上的点然后求距离。

证明:

设起点(xs,ys), 圆(x0,y0) r,  矩形(x1,y1)->(x2,y2) 且x1<x2 ,y1<y2 , 走的距离为dis, 当前角度为a ,

则圆上的点为 (xx=x0+r*cos(a), yy=y0+r*sin(a))

(xx,yy)到(xs,ys)的距离dd=sqrt( (xs-xx)*(xs-xx)+(ys-yy)*(ys-yy) )

当xx<x1 , yy<y1 , dis=sqrt( (x1-xx)*(x1-xx)+(y1-yy)*(y1-yy) )+dd

当xx<x1 , yy>y2 , dis=sqrt( (x1-xx)*(x1-xx)+(y2-yy)*(y2-yy) )+dd

当xx<x1 , y1<=yy<=y2, dis=x1-xx+dd

当xx>x2 , yy<y1 , dis=sqrt( (x2-xx)*(x2-xx)+(y1-yy)*(y1-yy) )+dd

当xx>x2 , yy>y1 , dis=sqrt( (x2-xx)*(x2-xx)+(y2-yy)*(y2-yy) )+dd

当xx>x2 , y1<=yy<=y2 , dis=xx-x2+dd

当x1<=xx<=x2 , yy<y1 , dis=y1-yy+dd

当x1<=xx<=x2 , yy>y2 , dis=yy-y2+dd

好吧。。。写到这里完全没心情解下去了。。。看到这种题凭经验吧。

代码(暴力 500ms):

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-8)
#define type int
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define _max(x,y) (((x)>(y))? (x):(y))
#define _min(x,y) (((x)<(y))? (x):(y))
#define _abs(x) ((x)<0? (-(x)):(x))
#define getmin(x,y) (x= (x<0 || (y)<x)? (y):x)
#define getmax(x,y) (x= ((y)>x)? (y):x)
template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
int TS,cas=1;
const int M=1000000+5;

double x,y,R;
struct pot{
    double x,y;
	pot(double _x=0,double _y=0){x=_x,y=_y;}
}p0;
struct rec{
    pot p1,p2;
    void read(){
        scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y);
        if(p1.x>p2.x) _swap(p1.x,p2.x);
        if(p1.y>p2.y) _swap(p1.y,p2.y);
    }
}r0;
double p2p(pot p1,pot p2){
    double dx=p1.x-p2.x;
    double dy=p1.y-p2.y;
    return sqrt(dx*dx+dy*dy);
}
double p2rec(pot p,rec r){
    double x=0,y=0;
    if(p.x<r.p1.x) x=r.p1.x-p.x;
    else if(p.x>r.p2.x) x=p.x-r.p2.x;
    if(p.y<r.p1.y) y=r.p1.y-p.y;
    else if(p.y>r.p2.y) y=p.y-r.p2.y;
    return sqrt(x*x+y*y);
}

void run(){
    int i,j;
    scanf("%lf%lf%lf",&x,&y,&R);
	r0.read();
	pot p;
	double ans=-1,tmp;
	for(double a=0;a<=360;a+=0.01){
		p=pot(x+R*cos(a/180*pi),y+R*sin(a/180*pi));
		tmp=p2p(p,p0)+p2rec(p,r0);
		getmin(ans,tmp);
	}
	printf("%.2lf\n",ans);
}

void preSof(){
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    preSof();
    //run();
    while(~scanf("%lf%lf",&p0.x,&p0.y) && (p0.x || p0.y)) run();
    //for(scanf("%d",&TS);cas<=TS;cas++) run();
    return 0;
}


代码(三分,0ms, ps:AC了但不会证明= =):

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-8)
#define type int
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define _max(x,y) (((x)>(y))? (x):(y))
#define _min(x,y) (((x)<(y))? (x):(y))
#define _abs(x) ((x)<0? (-(x)):(x))
#define getmin(x,y) (x= (x<0 || (y)<x)? (y):x)
#define getmax(x,y) (x= ((y)>x)? (y):x)
template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
int TS,cas=1;
const int M=1000000+5;

double x,y,R;
struct pot{
    double x,y;
    pot(double _x=0,double _y=0){x=_x,y=_y;}
}p0;
struct rec{
    pot p1,p2;
    void read(){
        scanf("%lf%lf%lf%lf",&p1.x,&p1.y,&p2.x,&p2.y);
        if(p1.x>p2.x) _swap(p1.x,p2.x);
        if(p1.y>p2.y) _swap(p1.y,p2.y);
    }
}r0;
double p2p(pot p1,pot p2){
    double dx=p1.x-p2.x;
    double dy=p1.y-p2.y;
    return sqrt(dx*dx+dy*dy);
}
double p2rec(pot p,rec r){
    double x=0,y=0;
    if(p.x<r.p1.x) x=r.p1.x-p.x;
    else if(p.x>r.p2.x) x=p.x-r.p2.x;
    if(p.y<r.p1.y) y=r.p1.y-p.y;
    else if(p.y>r.p2.y) y=p.y-r.p2.y;
    return sqrt(x*x+y*y);
}

void run(){
    int i,j;
    p0=pot(x,y);
    scanf("%lf%lf%lf",&x,&y,&R);
    r0.read();
    pot p1,p2;
    double ans=-1,t1,t2;
    double l,r,mid,midmid;
    for(l=0,r=pi;r-l>=eps;){
        mid=(r+l)/2;
        midmid=(r+mid)/2;
        p1=pot(x+R*cos(mid),y+R*sin(mid));
        p2=pot(x+R*cos(midmid),y+R*sin(midmid));
        t1=p2p(p1,p0)+p2rec(p1,r0);
        t2=p2p(p2,p0)+p2rec(p2,r0);
        if(t1>t2) l=mid,getmin(ans,t2);
        else r=midmid,getmin(ans,t1);
    }
    for(l=pi,r=2*pi;r-l>=eps;){
        mid=(r+l)/2;
        midmid=(r+mid)/2;
        p1=pot(x+R*cos(mid),y+R*sin(mid));
        p2=pot(x+R*cos(midmid),y+R*sin(midmid));
        t1=p2p(p1,p0)+p2rec(p1,r0);
        t2=p2p(p2,p0)+p2rec(p2,r0);
        if(t1>t2) l=mid,getmin(ans,t2);
        else r=midmid,getmin(ans,t1);
    }
    printf("%.2lf\n",ans);
}

void preSof(){
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    preSof();
    //run();
    while(~scanf("%lf%lf",&x,&y) && (x||y)) run();
    //for(scanf("%d",&TS);cas<=TS;cas++) run();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值