Page 4 of 4

Re: 10310 - Dog and Gopher

Posted: Sun Oct 31, 2010 1:34 am
by crystaltanvir
I got TLE with my code.... Can someone help????

Code: Select all

#include<cmath>
#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int N, i, c = 0;
    while(1)
    {
        if(c != 0) cout << endl;
        cin >> N;
        c++;
        int res = 0;
        float dogx, dogy, gopx, gopy, escapex, escapey;
        float holex[N], holey[N];
        cin >> gopx >> gopy >> dogx >> dogy;
        for(i = 0; i < N; i++)
        {
            cin >> holex[i] >> holey[i];
            if(res != 1)
            {
                float dogdis, gopdis;
                dogdis = sqrt(pow((dogx - holex[i]), 2) + pow((dogy - holey[i]), 2));
                gopdis = sqrt(pow((gopx - holex[i]), 2) + pow((gopy - holey[i]), 2));
                if(dogdis >= (2 * gopdis))
                {
                    res = 1;
                    escapex = holex[i];
                    escapey = holey[i];
                }
            }
        }
        if(res == 0) cout << "The gopher cannot escape." << endl;
        else printf("The gopher can escape through the hole at (%.3f,%.3f).\n", escapex, escapey);
    }
    return 0;
}


Re: 10310 - Dog and Gopher

Posted: Fri Apr 29, 2011 7:35 am
by ShaunOfTheLive
crystaltanvir wrote:I got TLE with my code.... Can someone help????

Code: Select all

#include<cmath>
#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int N, i, c = 0;
    while(1)
    {
        if(c != 0) cout << endl;
        cin >> N;
        c++;
        int res = 0;
        float dogx, dogy, gopx, gopy, escapex, escapey;
        float holex[N], holey[N];
        cin >> gopx >> gopy >> dogx >> dogy;
        for(i = 0; i < N; i++)
        {
            cin >> holex[i] >> holey[i];
            if(res != 1)
            {
                float dogdis, gopdis;
                dogdis = sqrt(pow((dogx - holex[i]), 2) + pow((dogy - holey[i]), 2));
                gopdis = sqrt(pow((gopx - holex[i]), 2) + pow((gopy - holey[i]), 2));
                if(dogdis >= (2 * gopdis))
                {
                    res = 1;
                    escapex = holex[i];
                    escapey = holey[i];
                }
            }
        }
        if(res == 0) cout << "The gopher cannot escape." << endl;
        else printf("The gopher can escape through the hole at (%.3f,%.3f).\n", escapex, escapey);
    }
    return 0;
}

A little too late to reply to this, I guess, but I will anyway.

If you run your code on the command line and then press Ctrl+Z after you're done entering input, you'll see what the problem is. Your code doesn't properly handle EOF (end-of-file), and in fact it has no way to exit. It keeps running indefinitely after end-of-file. To handle this, instead of while(1) you'll want to do something like while(!cin.fail()).

Re: 10310 - Dog and Gopher

Posted: Fri Sep 16, 2011 7:13 pm
by Pro.metal
using scanf is a better option since scanf is said to faster than cin !

and reading until EOF is a factor...while(scanf("%d",&N)!=EOF) can help :wink: