Page 1 of 4

10310 - Dog and Gopher

Posted: Sun Aug 04, 2002 1:02 am
by JWizard
I don't know why I keep getting wrong answer... works fine with all the data I tried....

[c]#include <stdio.h>
#include <math.h>

int main()
int n;
double gx, gy, dx, dy;
while(5 == scanf("%d %lf %lf %lf %lf", &n, &gx, &gy, &dx, &dy))
int i, j, s = 0;
for(i = 0 ; i < n ; i++)
double x, y, dg, dd;
scanf("%lf %lf", &x, &y);
dg = hypot(gx-x, gy-y);
dd = hypot(dx-x, dy-y);
if(dg*2.0 < dd)
printf("The gopher can escape through the hole at (%.3f,%.3f).\n", x, y);
s = 1;
if(!s) printf("The gopher cannot escape.\n");
return 0;
} [/c]

While we are on the topic, does anyone have any tips for finding "tricky" input ? :)


Posted: Tue Aug 13, 2002 7:41 pm
by Hauser
I've used long longs, not doubles, and I've got AC.
(You can simply operate on numbers multiplied by 1000).
Also, you don't have to use sqrt function, or something. You can simply
compare squares of numbers.


Posted: Sat Aug 17, 2002 9:35 am
by seolv
I tried to solve this problem too but I got WA.

I think,, the solution of this problem is obvious and the reason why i got WA is something related floating number arithmatics.

I changed double to long datatype but it doesn't work. --;

Do you know any good sites that explain floating number errors?

plz answer me

Posted: Sat Aug 17, 2002 7:54 pm
by Hauser
I haven't check if 'long' is enough, but I've used instead 'long long'
(it is 64bit integer, 2x bigger than normal long which is 32bit), and I got AC.

Some parts of my program:

long long dog_x, dog_y;
long long gopher_x, gopher_y;

int can_escape( long long x, long long y)

long long dog_l, gopher_l;

dog_l = (x-dog_x)*(x-dog_x) + (y-dog_y)*(y-dog_y);
gopher_l = (x-gopher_x)*(x-gopher_x) + (y-gopher_y)*(y-gopher_y);

if( 4*gopher_l <= dog_l )
return 1;
return 0;


When reading numbers from input, you should multiply them by 1000.
To read a number I've used this function:

[c]int get_spaces(void)
int c;
while( isspace(c=getchar()) );
ungetc( c, stdin );
return c;

long long get_number(void)
int c;
int minus=1;
int i;
long long result = 0;

if( c=='-' ) {
minus = -1;

while( (c=getchar())!=' ' && c!='.') {
result *= 10;
result += c-'0';

if( c==' ' )
return minus*result*1000;

for( i=0; i<3; i++ ) {
if( c==' ' )

result *= 10;
result += c-'0';

while( i<3 ) {
result *= 10;

return minus*result;

I think it can be done much easier, but thats the way i've done it.
You should also use some yours printing method.


Why the possible Wrong Answer

Posted: Fri Sep 27, 2002 2:32 am
by Johnny_T
Apparently if the gopher reaches the hole at the same time as the dog it is considered that he escapes...


Posted: Tue Oct 08, 2002 5:07 pm
by scythe
This is easier:
scanf("%d.%d", &a, &b);
x = (long long) (a * 1000 + b);


Posted: Wed Oct 09, 2002 5:06 pm
by scythe
Sorry, that wouldnt work for negative numbers
Still, i got ac using double data type and an 1e-7 epsilon.


Posted: Sun Apr 13, 2003 6:34 am
by SilVer DirectXer
i have tested many cases and i cant see any errors..
#include <iostream.h>
#include <stdio.h>
#include <math.h>
class co{
double x;
double y;
co dog, g,holes[1003];
int pts;
int i,j,k;
double dd,dg;
int a;
int gx,gy,hx,hy,dx,dy;
void main(void)
while (1)
if (cin.eof())
gy=g.y *1000;

for (i=0;i<pts;i++)
cin>>holes.x >>holes.y ;
for (i=0;i<pts;i++)
hx=holes.x *1000;
hy=holes.y *1000;

dd=(dx-hx)*(dx-hx)+(dy-hy)*(dy-hy) ;

dg=(gx-hx)*(gx-hx )+(gy-hy )*(gy-hy );

if (dd>dg)
printf("The gopher can escape through the hole at (%.3f,%.3f).\n",holes.x ,holes.y );

if (a==0)
cout<<"The gopher cannot escape."<<endl;


Posted: Sun Apr 20, 2003 6:18 pm
by deddy one
have you try

if (dd>=dg)

notice the '=' there.

I dunno...

Posted: Tue Apr 22, 2003 9:41 pm
by beska
I think I'm having the same problem, though I'm using Java. I'm getting WA, even though the algorithm seems obvious. I am checking for >= rather than strictly >, so that's not the issue... Any bizzare test cases people have would help.[/java]

Posted: Wed Apr 23, 2003 4:28 pm
by deddy one
when I first submit I use '>'
and get Wa, when I changed it
to '>=' then I get Ac

I use double to solve this using C

Posted: Wed Apr 23, 2003 10:36 pm
by FlareCDE
A few things I noticed. You need to check for equals, as posted above. Doubles seem to help, my floats gave me WA. Another big thing is to watch for integer division in c++. dogDist/2 gives a WA, but dogDist/2.0 does not :). Be careful there, this one is simple but a few minor programming nuances can really mess it up.

Posted: Wed Apr 23, 2003 11:19 pm
by turuthok
For problems like this, logically, it's straightforward to solve using floating-points operations (i.e.: using doubles). But, if you're skeptical about it because of precision stuff ... use integer operation. I think this problem is very doable using integer operation ... (e.g.: multiply the input numbers with 1000 ... or something like that).

My AC-ed solution is using double, though ...


math is always important...

Posted: Mon May 19, 2003 11:28 am
by stcheung
People, perhaps this may be the most useful tip on getting your 10310 program to AC. First of all, you can use doubles (no need to use long long and multiply input by 1000 blahblah). Secondly, when the dog & gopher reach the hole at same time, it's still considered a successful escape. Lastly, and definitely NOT the least, if you are lazy like me and didn't take sqrt when calculating distances, then when you compare the dog & gopher distances, remember to multiply gopher distance by 4 (or divide dog distance by 4). Multiplying/dividing by 2 doesn't work if you HAVE NOT taken square root (and that's how I keep getting WA at first).


10310 - Dog and Gopher

Posted: Sat Sep 20, 2003 4:06 am
by bugzpodder
I have:
1) made the output precision to 3 decimal places
2) output the first available hole and ignored all the ones after inputting them
3) if dog and gopher reaches a hole at the same time, gopher escapes
4) used long long to avoid any precision error (including not taking sqrt and using 4 to replace 2 upon squaring)

but i still cannot get the problem. please help!!


using namespace std;

long long dist(long long a, long long b){
return a*a+b*b;

int main(){
long long d1,d2,gx,gy,dx,dy,hx,hy;
int N,i;
double dgx,dgy,ddx,ddy,dhx,dhy;
bool flag;

gx=(long long)(dgx*1000ULL); gy=(long long)(dgy*1000ULL);
dx=(long long)(ddx*1000ULL); dy=(long long)(ddy*1000ULL);
for (i=0;i<N;i++){
if (flag) continue;
hx=(long long)(dhx*1000ULL); hy=(long long)(dhy*1000ULL);
if (d1>=d2){
cout<<"The gopher can escape through the hole at (";

if (!flag) cout<<"The gopher cannot escape."<<endl;
return 0;