ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1348. Goat in the Garden 2

WA#13
Posted by B&B 9 May 2013 18:26

#include <cstdio>
#include <cmath>
#include <algorithm>

struct point
{
int x, y;
};

double distance(const point P1, const point P2)
{
return sqrt((double)(P1.x - P2.x)*(P1.x - P2.x) + (double)(P1.y - P2.y)*(P1.y - P2.y));
}

bool operator ==(const point P1, const point P2)
{
if (P1.x == P2.x && P1.y == P2.y) return 1;
return 0;
}

int main()
{
point A, B, C;
unsigned int L;
scanf("%d%d%d%d%d%d%d", &A.x, &A.y, &B.x, &B.y, &C.x, &C.y, &L);

const double pi = 3.14159265;
const double epsilon = 0.00000001;
double alpha, beta, gamma;
double a, b, c;
a = distance(B, C);
b = distance(A, C);
c = distance(A, B);
alpha = acos((b*b + c*c - a*a)/(2*b*c));
beta = acos((a*a + c*c - b*b)/(2*a*c));
gamma = acos((a*a + b*b - c*c)/(2*a*b));
if (A == B)
{
printf("1 is working\n");
printf("a is %.2lf\nb is %.2lf\nc is %.2lf\n", a , b , c);
printf("alpha is %.2lf\nbeta is %.2lf\ngamma is %.2lf\n",  alpha, beta, gamma);
if (L >= a) printf("0.00\n0.00\n");
else printf("%.2lf\n%.2lf\n", a - L, b - L);
return 0;
}

if(abs(alpha + beta - pi) < epsilon)
{
printf("2 is working\n");
printf("a is %.2lf\nb is %.2lf\nc is %.2lf\n", a , b , c);
printf("alpha is %.2lf\nbeta is %.2lf\ngamma is %.2lf\n",  alpha, beta, gamma);
double nl1 = std::min(a, b);
double nl2 = std::max(a, b);
if (L >= nl1) printf("0.00\n");
else printf("%.2lf\n", nl1 - L);
if (L >= nl2) printf("0.00\n");
else printf("%.2lf\n", nl2 - L);
return 0;
}
if (abs(alpha - pi/2) < epsilon)
{
printf("3 is working\n");
printf("a is %.2lf\nb is %.2lf\nc is %.2lf\n", a , b , c);
printf("alpha is %.2lf\nbeta is %.2lf\ngamma is %.2lf\n",  alpha, beta, gamma);
double nl1 = b;
double nl2 = std::max(a, b);
if (L >= nl1) printf("0.00\n");
else printf("%.2lf\n", nl1 - L);
if (L >= nl2) printf("0.00\n");
else printf("%.2lf\n", nl2 - L);
return 0;
}
if (abs(beta - pi/2) < epsilon)
{
printf("4 is working\n");
printf("a is %.2lf\nb is %.2lf\nc is %.2lf\n", a , b , c);
printf("alpha is %.2lf\nbeta is %.2lf\ngamma is %.2lf\n",  alpha, beta, gamma);
double nl1 = a;
double nl2 = std::max(a, b);
if (L >= nl1) printf("0.00\n");
else printf("%.2lf\n", nl1 - L);
if (L >= nl2) printf("0.00\n");
else printf("%.2lf\n", nl2 - L);
return 0;
}
if(alpha > pi/2)
{
printf("5 is working\n");
printf("a is %.2lf\nb is %.2lf\nc is %.2lf\n", a , b , c);
printf("alpha is %.2lf\nbeta is %.2lf\ngamma is %.2lf\n",  alpha, beta, gamma);
double nl1 = b;
double nl2 = a;
if (L >= nl1) printf("0.00\n");
else printf("%.2lf\n", nl1 - L);
if (L >= nl2) printf("0.00\n");
else printf("%.2lf\n", nl2 - L);
return 0;
}
if(beta > pi/2)
{
printf("6 is working\n");
printf("a is %.2lf\nb is %.2lf\nc is %.2lf\n", a , b , c);
printf("alpha is %.2lf\nbeta is %.2lf\ngamma is %.2lf\n",  alpha, beta, gamma);
double nl1 = a;
double nl2 = b;
if (L >= nl1) printf("0.00\n");
else printf("%.2lf\n", nl1 - L);
if (L >= nl2) printf("0.00\n");
else printf("%.2lf\n", nl2 - L);
return 0;
}

printf("7 is working\n");
printf("a is %.2lf\nb is %.2lf\nc is %.2lf\n", a , b , c);
printf("alpha is %.2lf\nbeta is %.2lf\ngamma is %.2lf\n",  alpha, beta, gamma);
double nl1 = b * sin(alpha);
double nl2 = std::max(a, b);
if (L >= nl1) printf("0.00\n");
else printf("%.2lf\n", nl1 - L);
if (L >= nl2) printf("0.00\n");
else printf("%.2lf\n", nl2 - L);
return 0;
}
Re: WA#13
Posted by Dron-elektron 9 Nov 2013 13:41
Check this situations: A = B, B = C, A = C
Because, if you are calculating angle between vectors with formula: cos a = (scalar mult) / (|len1| * |len2|), so if one of distance is 0 - you will get "nan" (Not A Number) = something / 0 ...

Edited by author 09.11.2013 13:43