Codeforces Gym 100338B Geometry Problem 计算几何
Problem B. Geometry Problem
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88926#problem/B
Description
Peter is studying in the third grade of elementary school. His teacher of geometry often gives him difficult home tasks. At the last lesson the students were studying circles. They learned how to draw circles with compasses. Peter has completed most of his homework and now he needs to solve the following problem. He is given two segments. He needs to draw a circle which intersects interior of each segment exactly once. The circle must intersect the interior of each segment, just touching or passing through the end of the segment is not satisfactory. Help Peter to complete his homework.
Input
Output
For each test case output three real numbers — the coordinates of the center and the radius of the circle. All numbers in the output file must not exceed 1010 by their absolute values. The jury makes all comparisons of real numbers with the precision of 10−4 .
Sample Input
0 0 0 4
1 0 1 4
0 0 0 0
0 0 0 0
Sample Output
0.5 0 2
HINT
题意
给你两个线段,让你构造一个圆,与每个线段都只相交一次
题解:
首先如何判断这个线段和圆相交了一次:一个端点在圆内,一个在圆外
然后我们枚举四个点的中点,半径就取中点到端点的最小值,然后再随便加上一个0.005就好了
就AC了……
加0.05会WA8
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 200051 #define mod 10007 #define eps 1e-9 int Num; //const int inf=0x7fffffff; //нчоч╢С const int inf=0x3f3f3f3f; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** struct node { double x,y; }; node kiss1[4]; node kiss2[4]; double dis(node a,node b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int check1(double r,double x,double y) { int flag=0; int flag1=0; node ttt; ttt.x=x,ttt.y=y; for(int i=0;i<2;i++) { if(dis(ttt,kiss1[i])<r) flag++; if(dis(ttt,kiss2[i])<r) flag1++; } if(flag==1&&flag1==1) return 1; return 0; } int check(double x,double y) { double ans=inf; node a; a.x=x,a.y=y; for(int i=0;i<2;i++) { ans = min(ans,dis(a,kiss1[i])); ans = min(ans,dis(a,kiss2[i])); } ans+=0.005; if(check1(ans,x,y)) { printf("%.10f %.10f %.10f\n",x,y,ans); return 1; } return 0; } int main() { srand((unsigned)time(NULL)); freopen("geometry.in","r",stdin); freopen("geometry.out","w",stdout); while(1) { for(int i=0;i<2;i++) cin>>kiss1[i].x>>kiss1[i].y; for(int i=0;i<2;i++) cin>>kiss2[i].x>>kiss2[i].y; if(kiss1[0].x==0&&kiss1[0].y==0&&kiss2[0].x==0&&kiss2[0].y==0&&kiss1[1].x==0&&kiss1[1].y==0&&kiss2[1].x==0&&kiss2[1].y==0) break; node a,b; double ans=inf; int flag = 1; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { a = kiss1[i],b = kiss2[j]; double ansx=(a.x+b.x)/2.0; double ansy=(a.y+b.y)/2.0; if(check(ansx,ansy)==1) { flag = 0; break; } } if(!flag) break; } if(!flag) continue; } }