hdu4454Stealing a Cake+计算几何

Problem Description
There is a big round cake on the ground. A small ant plans to steal a small piece of cake. He starts from a certain point, reaches the cake, and then carry the piece back home. He does not want to be detected, so he is going to design a shortest path to achieve his goal.

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.

Input
The input consists of several test cases.
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.

Output
For each test case, print the shortest distance to achieve his goal. Please round the result to 2 digits after decimal point.

Sample Input

1 1
-1 1 1
0 -1 1 0
0 2
-1 1 1
0 -1 1 0
0 0

Sample Output

1.75
2.00

Source
2012 Asia Hangzhou Regional Contest
题意:一只老鼠从一个点出发,到一个圆形的蛋糕偷一块蛋糕回到矩形的家里,问最短路径。可以穿过蛋糕。
解法:直接暴力枚举老鼠到达圆上的点,计算点到圆上点+圆到矩形最小的距离。点到矩形的最小距离就是点到矩形4条边的线段的最小距离。

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=100;
const double eps=1e-8;
const double pi=acos(-1.0);
inline double sqr(double x){
    return x*x;
}
struct point{
    double x,y;
    point(){};
    point (double x,double y):x(x),y(y){};
    friend point operator - (const point &a,const point &b){
        return point(a.x-b.x,a.y-b.y);
    }
    double norm(){
        return sqrt(sqr(x)+sqr(y));
    }
};
int cmp(double x){
    if(fabs(x)<eps) return 0;
    if(x>0) return 1;
    return -1;
}
double det(const point &a,const point &b){
    return a.x*b.y-a.y*b.x;
}
double dot(const point &a,const point &b){
    return a.x*b.x+a.y*b.y;
}
double dist(point a,point b){
    return (a-b).norm();
}
double dis_point_segment(const point p,const point s,const point t){
    if(cmp(dot(p-s,t-s))<0) return (p-s).norm();
    if(cmp(dot(p-t,s-t))<0) return (p-t).norm();
    return fabs(det(s-p,t-p)/dist(s,t));
}
int main(void) {
    point start;
    point o;
    double r;
    point j1,j2,j3,j4;
    while(scanf("%lf %lf",&start.x,&start.y)!=EOF){
        if(start.x==0&&start.y==0) break;
        scanf("%lf %lf %lf",&o.x,&o.y,&r);
        double a,b,c,d;
        scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
        if(a>c) swap(a,c);
        if(b>d) swap(b,d);
        j1=point(a,b);  //矩形的四个点
        j2=point(a,d);
        j3=point(c,b);
        j4=point(c,d);
        double ans=1e100;
        double temp=pi/10000;
        for(double i=0;i<=2*pi;i=i+temp){
            point oncircle=point(o.x+r*cos(i),o.y+r*sin(i));//园上的点
            //对应到矩形四条边的最小距离
            ans=min(ans,dist(oncircle,start)+dis_point_segment(oncircle,j1,j2));
            ans=min(ans,dist(oncircle,start)+dis_point_segment(oncircle,j1,j3));
            ans=min(ans,dist(oncircle,start)+dis_point_segment(oncircle,j2,j4));
            ans=min(ans,dist(oncircle,start)+dis_point_segment(oncircle,j3,j4));
        }
        printf("%.2f\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值