2013
12-04

Rectangle and Circle

Given a rectangle and a circle in the coordinate system(two edges of the rectangle are parallel with the X-axis, and the other two are parallel with the Y-axis), you have to tell if their borders intersect.

Note: we call them intersect even if they are just tangent. The circle is located by its centre and radius, and the rectangle is located by one of its diagonal.

The first line of input is a positive integer P which indicates the number of test cases. Then P test cases follow. Each test cases consists of seven real numbers, they are X,Y,R,X1,Y1,X2,Y2. That means the centre of a circle is (X,Y) and the radius of the circle is R, and one of the rectangle’s diagonal is (X1,Y1)-(X2,Y2).

For each test case, if the rectangle and the circle intersects, just output "YES" in a single line, or you should output "NO" in a single line.

2
1 1 1 1 2 4 3
1 1 1 1 3 4 4.5

YES
NO

WA了好几次。。。

#include"stdio.h"
#include"string.h"
#include"math.h"
struct node
{
double x,y;
}aa[5];
int main()
{
int T;
int n,t;
double x,y,r;
double x1,x2,y1,y2;
scanf("%d",&T);
while(T--)
{
scanf("%lf%lf%lf",&x,&y,&r);
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
aa[0].x=x;aa[0].y=y;
if(x1>x2){t=x1;x1=x2;x2=t;}
if(y1>y2){t=y1;y1=y2;y2=t;}
double t1,t2;
int f1,f2,f3,f4;
f1=f2=f3=f4=0;
if((r*r-(x1-x)*(x1-x))>=0)
{
t1=sqrt((r*r-(x1-x)*(x1-x)))+y;
t2=-sqrt((r*r-(x1-x)*(x1-x)))+y;
if(t1>=y1&&t1<=y2||t2>=y1&&t2<=y2)f1=1;
}
if((r*r-(x2-x)*(x2-x))>=0)
{
t1=sqrt((r*r-(x2-x)*(x2-x)))+y;
t2=-sqrt(r*r-(x2-x)*(x2-x))+y;
if(t1>=y1&&t1<=y2||t2>=y1&&t2<=y2)f2=1;
}
if((r*r-(y1-y)*(y1-y))>=0)
{
t1=sqrt(r*r-(y1-y)*(y1-y))+x;
t2=-sqrt(r*r-(y1-y)*(y1-y))+x;
if(t1>=x1&&t1<=x2||t2>=x1&&t2<=x2)f3=1;
}
if((r*r-(y2-y)*(y2-y))>=0)
{
t1=sqrt(r*r-(y2-y)*(y2-y))+x;
t2=-sqrt(r*r-(y2-y)*(y2-y))+x;
if(t1>=x1&&t1<=x2||t2>=x1&&t2<=x2)f4=1;
}
if(f1||f2||f3||f4)printf("YES\n");
else printf("NO\n");
}
return 0;
}

1. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
O(n) = O(n-1) + O(n-2) + ….
O(n-1) = O(n-2) + O(n-3)+ …
O(n) – O(n-1) = O(n-1)
O(n) = 2O(n-1)

2. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.