Sherlock and Geometry

  • + 0 comments

    this is my code which I wrote in 5-6 hours can someone point out what am i missing because this is really frustrating they have given a testcase and inside that testcase there are 30000 testcases :( instead they should have given 10 seprate testcases :(

    #include <bits/stdc++.h>
    using namespace std;
    
    //int dist();
    
    int distance(int x,int y,int x1,int y1,int x2,int y2)
    {
        float x3=0.0,y3=0.0,temp=0.0,temp1=0.0;
    
        float slope = (y2-y1)/(1.0*(x2-x1));
    
        //cout<<slope<<endl;
    
        temp = y1-y-x1*(slope)-x*(1.0/slope);
        temp1 = -(1.0/slope)-slope;
        float x_cor = temp/(1.0*temp1) ;
        float y_cor = y1+ slope*(x_cor-x1);
    
    
        float dist = 0.0;
    
        dist = (x-x_cor)*(x-x_cor)+(y-y_cor)*(y-y_cor);
    
        return dist;  
    
    }
    
    // Complete the solve function below.
    string solve(int x, int y, int r, vector<int> t1, vector<int> t2, vector<int> t3) {
    
          
        int count1=0,count2=0,count3=0;
        
        if( (t1[0]-x)*(t1[0]-x)+(t1[1]-y)*(t1[1]-y) < r*r ) count1++;
        if( (t2[0]-x)*(t2[0]-x)+(t2[1]-y)*(t2[1]-y) < r*r ) count1++;
        if( (t3[0]-x)*(t3[0]-x)+(t3[1]-y)*(t3[1]-y) < r*r ) count1++;
    
        if( (t1[0]-x)*(t1[0]-x)+(t1[1]-y)*(t1[1]-y) > r*r ) count2++;
        if( (t2[0]-x)*(t2[0]-x)+(t2[1]-y)*(t2[1]-y) > r*r ) count2++;
        if( (t3[0]-x)*(t3[0]-x)+(t3[1]-y)*(t3[1]-y) > r*r ) count2++;
    
        if( (t1[0]-x)*(t1[0]-x)+(t1[1]-y)*(t1[1]-y) == r*r ) count3++;
        if( (t2[0]-x)*(t2[0]-x)+(t2[1]-y)*(t2[1]-y) == r*r ) count3++;
        if( (t3[0]-x)*(t3[0]-x)+(t3[1]-y)*(t3[1]-y) == r*r ) count3++;
    
    
        //cout<<count1<<" "<<count2<<endl;
    
    
        
        float point_x=0.0,point_y=0.0,temp=0.0,dist=0.0;
        
    
        if(count3 != 0 ) return "YES";
    
        if(count1 == 3 )  return "NO";
    
        if(count1 == 1 && count2 == 2) return "YES";
    
        if(count1 == 2 && count2 == 1) return "YES";
        
        else if (count2 == 3)  
        {
            float dist1 = distance(x,y,t1[0],t1[1],t2[0],t2[1]);
            float dist2 = distance(x,y,t1[0],t1[1],t3[0],t3[1]);
            float dist3 = distance(x,y,t2[0],t2[1],t3[0],t3[1]);
    
            //float test4 = dist(4,0,0,0,4,4);
            
            if(dist1 <= r*r*1.0 ) return "YES";
            if(dist2 <= r*r*1.0 ) return "YES";
            if(dist3 <= r*r*1.0)  return "YES";
            else return "NO"; 
                
                
        }
    
        else return "YES";
        
    }
    
    int main()
    {
        int t;
    
        cin>>t;
    
        
    
        
        vector<string> res;
    
        string result;
    
        for(int i=0; i<t; i++)
        {
            int x,y,r;
    
            int x1,y1,x2,y2,x3,y3;
    
            vector<int> t1,t2,t3;
    
            cin>>x>>y>>r;
            cin>>x1>>y1;
            t1.push_back(x1);
            t1.push_back(y1);
            cin>>x2>>y2;
            t2.push_back(x2);
            t2.push_back(y2);
            cin>>x3>>y3;
            t3.push_back(x3);
            t3.push_back(y3);
    
            result = solve(x,y,r,t1,t2,t3);
            res.push_back(result);
            
                
        }    
        
        for(int i=0; i<t; i++)
        {
            cout<<res[i]<<endl;
        }
    return 0;
    }