hdu 4454 Stealing a Cake(三分,4级)

Stealing a Cake

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1013    Accepted Submission(s): 288


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
 
思路:三分蚂蚁圆上所在点。其实就是三分圆心角。


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const double eps=1e-8;
const double pi=acos(-1.0);

class Point
{
public:
  double x,y,r;
  double dis(Point z)
  {
    return sqrt((z.x-x)*(z.x-x)+(z.y-y)*(z.y-y));
  }
}s,cir,hei,col;

double fabs(double x)
{
  if(x<0)return -x;
  return x;
}
class Line
{
public:
  Point l,r;
  double xmult(Point p1,Point p2,Point p0)
  {
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
  }
  double dis_P_seg(Point p)///点到线段最短距离
  { Point t=p;
    t.x+=l.y-r.y;
    t.y+=r.x-l.x;
    if(xmult(l,t,p)*xmult(r,t,p)>eps)
      return p.dis(l)<p.dis(r)?p.dis(l):p.dis(r);
    return fabs(xmult(p,l,r))/l.dis(r);
  }
};
 double p_dis(Point p,Point l,Point r)///点到矩形的最短距离
  {
    double d=1e28;
    Line t;
    ///1
    t.l=l;t.r=r;
    t.r.x=t.l.x; d=min(d,t.dis_P_seg(p));
    ///2
    t.l=l;t.r=r;
    t.r.y=t.l.y; d=min(d,t.dis_P_seg(p));
    ///3
    t.l=l;t.r=r;
    t.l.x=t.r.x; d=min(d,t.dis_P_seg(p));
    ///4
    t.l=l;t.r=r;
    t.l.y=t.r.y; d=min(d,t.dis_P_seg(p));
    return d;
  }
double judge(double x)
{
  Point t;
  t.x=cir.x+cir.r*cos(x);
  t.y=cir.y+cir.r*sin(x);
  return t.dis(s)+p_dis(t,hei,col); //t.dis(hei,col);
}
int main()
{
  while(~scanf("%lf%lf",&s.x,&s.y))
  {
    if(s.x==0&&s.y==0)break;
    scanf("%lf%lf%lf",&cir.x,&cir.y,&cir.r);
    scanf("%lf%lf%lf%lf",&hei.x,&hei.y,&col.x,&col.y);
    double l=0,r=2*pi,ll,rr;
    while(r-l>eps)
    {
      ll=(l+l+r)/3;
      rr=(l+r+r)/3;
      if(judge(ll)>judge(rr))
      {
        l=ll;
      }
      else r=rr;
    }
    double ans=judge( (l+r)/2);
    printf("%.2lf\n",ans);
    //cout<<ans<<endl;
  }
}




posted @ 2013-10-04 14:48  剑不飞  阅读(193)  评论(0编辑  收藏  举报