11800 - Determine the Shape

All about problems in Volume 118. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

xnorax
New poster
Posts: 9
Joined: Thu Jun 26, 2014 9:31 pm

Re: 11800 - Determine the Shape

Post by xnorax » Sun Feb 22, 2015 8:49 pm

I used this algorithm to solve the problem
1- if same x axis for 1st & 3rd sides
Parallelogram
OR
square
OR
rectangular
OR
Trapezium
2- if same y axis
Trapezium
3- test if equal sides and not a square
Rhombus
4- else
Ordinal

Thank you in advance

Code: Select all

#include <iostream>
using namespace std;

int main(){
    long long int tc,arr[8];
    cin>>tc;
    for (int i=1; i<=tc; i++) {
        for(int j=0;j<8; j++){
            cin>>arr[j];
        }
        if(arr[1]==arr[3] && arr[5]==arr[7]){//same x axis for 1st & 2nd side
            
            //1******
            if((arr[4]>arr[2] && arr[6]>arr[0])||(arr[4]<arr[2] && arr[6]<arr[0]))
                printf("Case %i: Parallelogram\n",i);
            //2****** equal sides
            else if(arr[5]-arr[3] == arr[2]-arr[0])
                      printf("Case %i: Square\n",i);
            //3***** same y
            else if(arr[2] == arr[4]&&arr[6]==arr[0])
                printf("Case %i: Rectangle\n",i);

            else
                printf("Case %i: Trapezium\n",i);

        }
        else if(arr[2] == arr[4]&&arr[6]==arr[0])
            printf("Case %i: Trapezium\n",i);
        else if((arr[3]==arr[7] && arr[1]==arr[5])&&(arr[2]-arr[6] == arr[4]-arr[0])){
                printf("Case %i: Rhombus\n",i);
        }
        else
            printf("Case %i: Ordinary Quadrilateral\n",i);
        
    }

    return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11800 - Determine the Shape

Post by brianfry713 » Wed Feb 25, 2015 1:43 am

Try the I/O in this thread.
Check input and AC output for thousands of problems on uDebug!

garbage
New poster
Posts: 19
Joined: Thu Feb 21, 2013 5:46 am

Re: 11800 - Determine the Shape

Post by garbage » Sun Jun 28, 2015 6:17 pm

I can't find what's wrong with this code. Getting repeatedly WA. Please Help someone...

Code: Select all

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<string>
using namespace std;

struct Point{
    long long x;
    long long y;
}pnt;

class Comparator
{
    public:
        bool operator()(Point& P1, Point& P2)
        {
            if(P1.x > P2.x)
                return true;
                
            return false;
        }
};

long long gcd(long long a, long long b)
{
    if(a%b == 0)
        return b;
        
    else
        return gcd(b, a%b);
}

string convertToString(long long n)
{
    string S = "";
    
    while(n!=0)
    {
        long long r = n%10;
        n = n/10;
        S.push_back(r+'0');
    }
    
    return S;
}

string lineSlope(Point P1, Point P2)
{
    if(P1.x == P2.x)
        return "inf";
        
    else
        if(P1.y == P2.y)
            return "zero";
            
        else
        {
            string minus = "";
            long long n1 = P2.y - P1.y;
            long long n2 = P2.x - P1.x;
            long long n = gcd(n1, n2);
            
            n1 = n1 / n;
            n2 = n2 / n;
            
            if(n1 < 0 && n2 < 0)
            {
                n1 = (-1) * n1;
                n2 = (-1) * n2;
            }
            
            else
                if((n1 < 0 && n2 >= 0))
                {
                    n1 = (-1) * n1;
                    minus = "-";
                }
            
                else
                    if(n1 >= 0 && n2 < 0)
                    {
                        n2 = (-1) * n2;
                        minus = "-";
                    }
            
            return minus+(convertToString(n1)+convertToString(n2));
        }
}

int main()
{
    long long test, d1, d2, d3, d4, d5, d6;
    
    cin>>test;
    
    for(long long i=1;i<=test;i++)
    {
        Point P1, P2, P3, P4;
        priority_queue<Point, vector<Point>, Comparator>pq;
        
        for(long long j=1;j<=4;j++)
        {
            cin>>pnt.x>>pnt.y;
            pq.push(pnt);
        }
        
        P1 = pq.top();
        pq.pop();
        
        P2 = pq.top();
        pq.pop();
        
        P3 = pq.top();
        pq.pop();
        
        P4 = pq.top();
        pq.pop();
        
        //cout<<P3.x<<"  "<<P3.y<<"  "<<P4.x<<"  "<<P4.y<<endl;
        
        if(P1.y > P2.y)
        {
            Point tmp = P1;
            P1 = P2;
            P2 = tmp;
        }
        
        if(P3.y < P4.y)
        {
            Point tmp = P3;
            P3 = P4;
            P4 = tmp;
        }

        d1 = (P1.x - P2.x)*(P1.x - P2.x) + (P1.y - P2.y)*(P1.y - P2.y);
        d2 = (P3.x - P4.x)*(P3.x - P4.x) + (P3.y - P4.y)*(P3.y - P4.y);
        d3 = (P1.x - P4.x)*(P1.x - P4.x) + (P1.y - P4.y)*(P1.y - P4.y);
        d4 = (P2.x - P3.x)*(P2.x - P3.x) + (P2.y - P3.y)*(P2.y - P3.y);
        
        d5 = (P1.x - P3.x)*(P1.x - P3.x) + (P1.y - P3.y)*(P1.y - P3.y);
        d6 = (P2.x - P4.x)*(P2.x - P4.x) + (P2.y - P4.y)*(P2.y - P4.y);
        
        cout<<"Case "<<i<<": ";
        
        if(d1 == d2 && d2 == d3 && d3 == d4 && d5 == d6)
            cout<<"Square"<<endl;
            
        else
            if(d1 == d2 && d2 == d3 && d3 == d4 && d5 != d6)
                cout<<"Rhombus"<<endl;
            
            else
                if(d1 == d2 && d3 == d4 && d5 == d6)
                    cout<<"Rectangle"<<endl;
                    
                else
                    if(d1 == d2 && d3 == d4 && d5 != d6)
                        cout<<"Parallelogram"<<endl;
                        
                    else
                    {
                        string slp1 = lineSlope(P1, P2);
                        string slp2 = lineSlope(P3, P4);
                        string slp3 = lineSlope(P1, P4);
                        string slp4 = lineSlope(P2, P3);
                        
                        //cout<<slp1<<"  "<<slp2<<"  "<<slp3<<"   "<<slp4<<endl;
                           
                        if((slp1 == slp2 && slp3 != slp4) || (slp3 == slp4 && slp1 != slp2))
                            cout<<"Trapezium"<<endl;
                            
                        else
                            cout<<"Ordinary Quadrilateral"<<endl;
                    }
    }
    return 0;
}


Post Reply

Return to “Volume 118 (11800-11899)”