传送门:Gym - 102021B
Battle Royale games are the current trend in video games and Gamers Concealed Punching
Circles (GCPC) is the most popular game of them all. The game takes place in an area that, for
the sake of simplicity, can be thought of as a two-dimensional plane. Movement and positioning
are a substantial part of the gameplay, but getting to a desired location can be dangerous. You
are confident in your ability to handle the other players, however, while you are walking to your
destination, there are two hazards posed by the game itself:
- The game zone is bounded by a blue circle. Outside of this circle, there is a deadly force
field that would instantly take you out of the game. - Inside the game zone, there is a red circle where you are exposed to artillery strikes. This
circle is also too risky to enter.
You want to move from one spot on the map to another, but the direct path to your destination is
blocked by the red circle, so you need to find a way around it. Can you find the shortest path that
avoids all hazards by never leaving the blue or entering the red circle? Touching the boundaries
of the circles is fine, as long as you do not cross them.
Input
The input consists of:
- one line with two integers xc, yc specifying your current location;
- one line with two integers xd, yd specifying your destination;
- one line with three integers xb, yb, rb specifying the center and radius of the blue circle;
- one line with three integers xr, yr, rr specifying the center and radius of the red circle.
All coordinates have an absolute value of at most 1 000, and 1 ≤ rb, rr ≤ 1 000. The red circle
is strictly inside the blue circle. Your current location and destination are strictly inside the blue
circle and strictly outside of the red circle, and the direct path between them is blocked by the
red circle.
Output
Output the length of the shortest path that does not leave the blue or enter the red circle. The
output must be accurate up to a relative or absolute error (whichever is lower) of 10−7
Sample Input 1
0 0
10 0
0 0 1000
5 0 2
.
Sample Output 1
10.8112187742
题意:
平面内有一个大圆,大圆里面有两个点(我假设C和D)和一个小圆。你不能穿过小圆的区域,也不能走出大圆,问从C到D的最短距离。
题解:
如果线段CD没有穿过小圆,那么最小距离就是CD的距离,所有我们要讨论的就是如果CD穿过小圆,我们怎么求最短距离。
应该过C,D两点做小圆的切线,l1为切点到C的距离,l2为切点到D的距离,l3为在小圆上两切点的弧长。
那么最小距离就应该是
l
1
+
l
2
+
l
3
l1+l2+l3
l1+l2+l3 。
计算弧长时,有弧长公式:
S
=
a
∗
r
S=a*r
S=a∗r 。(
a
a
a为扇形的角度(弧度制),
r
r
r为扇形半径)
计算角度可以用
a
c
o
s
(
)
acos()
acos() 这个函数(即arccos),该函数返回的就是弧度。
一开始我认为最小距离应该是CD的长度减去穿过圆的部分,加上圆弧的长度,但是由两点之间直线最短我们可以得到:由C,D点到切点的距离更小, l 3 l3 l3 的长度的一样的。(可以结合上面的图)
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
struct Node
{
int x,y;
};
double getd(Node a,Node b,Node c)//计算点到直线距离,a,b为直线上两点,c为直线外的点
{
double d=(fabs((b.y-a.y)*c.x+(a.x-b.x)*c.y+((b.x*a.y)-(a.x*b.y))))/(sqrt(pow(b.y-a.y,2)+pow(a.x-b.x,2)));
return d;
}
double getl(Node a,Node b)
{
double d=sqrt(pow(b.x-a.x,2)+pow(b.y-a.y,2));
return d;
}
int main()
{
Node c,d,b,r;
int rr,rb;
scanf("%d%d",&c.x,&c.y);
scanf("%d%d",&d.x,&d.y);
scanf("%d%d%d",&b.x,&b.y,&rb);
scanf("%d%d%d",&r.x,&r.y,&rr);
double D=getd(c,d,r);
if(D>=rr)
{
double len=sqrt(pow(d.x-c.x,2)+pow(d.y-c.y,2));
printf("%.10lf\n",len);
}
else
{
double x1=getl(c,r),x2=getl(d,r);
double l1=sqrt(x1*x1-rr*rr),l2=sqrt(x2*x2-rr*rr);
double a=acos(D/x1)+acos(D/x2)-acos(rr*1.0/x1)-acos(rr*1.0/x2);
double l3=a*rr;
double l=l1+l2+l3;
printf("%.10lf\n",l);
}
return 0;
}