Saturday, 2 June 2012

UVa 10310 Dog and Gopher Solution

#include<stdio.h>
#include<math.h>
int main()
{
long int n,kase,i,j,k;
double gx,gy,dx,dy,hx[1001],hy[1001],gdis,ddis;
while(scanf("%ld%lf%lf%lf%lf",&n,&gx,&gy,&dx,&dy)==5)
{
int goph=0,x;
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&hx[i],&hy[i]);
}
for(i=1;i<=n;i++)
{
gdis=4*(pow((hx[i]-gx),2)+pow((hy[i]-gy),2));
ddis=pow((hx[i]-dx),2)+pow((hy[i]-dy),2);
if(gdis<=ddis)
{
goph++,x=i;
break;
}
}
if(goph==0)printf("The gopher cannot escape.\n");
else printf("The gopher can escape through the hole at (%.3lf,%.3lf).\n",hx[x],hy[x]);
}
return 0;
}

3 comments:

  1. gdis=4*(pow((hx[i]-gx),2)+pow((hy[i]-gy),2));
    why was 4 multiplied here instead of 2 ?

    ReplyDelete
    Replies
    1. because the formula is: sqrt(pow((hx[i]-gx),2)+pow((hy[i]-gy),2));
      here sqrt is ignored that's why 2 is squared into 4.

      thank you :)

      Delete
  2. for(i=1;i<=n;i++)
    {
    gdis=4*(pow((hx[i]-gx),2)+pow((hy[i]-gy),2));
    ddis=pow((hx[i]-dx),2)+pow((hy[i]-dy),2);
    if(gdis<=ddis)
    {
    goph++,x=i;
    break;
    }
    }
    pls, can you give some comments into it?

    ReplyDelete