Sunday, October 17, 2010

Pixel Enclosures





In the digital world geometric shapes are drawn using
pixels. A pixel is a square area of unit dimension. For this problem we say 2
pixels are connected if they share an edge or a vertex between them. This is
also called 8-connectivity. Your task is to find the maximum possible area of a
closed loop made up of A pixels (these are boundary pixels of the closed loop).
Area of a closed loop is the number of pixels which are completely inside the
loop or on the loop. Consider the example below:
For A = 4, you can make a close loop as follows -


This has an area of 5 with 4 pixels on the loop and 1 pixel completely inside
the loop.



Input:



First line contains an integer N,
the number of test cases.
Next N lines contain an integer A for that test case.
N <= 100
A <= 1000



Output:



Print N
lines with a single integer stating the maximum area of a closed loop.

Sample Input:
2
4
5

Sample Output:
5
6


Time Limit: 2 seconds
Memory Limit: 32 MB



#include<iostream>



using namespace std;



int main()



{



    int fun(int);



    int N,ans,A;



   
cin>>N;



    while(N>0)



    {



       
cin>>A;



       
ans=fun(A);



       
cout<<ans<<endl;



        N--;



    }



    return 0;



}



int fun(int A)



{



    if(A==0)



    {



        return 0;



    }



    else if(A==1)



    {



        return 1;



    }



    else if(A==2)



    {



        return 2;



    }



    else if(A==3)



    {



        return 3;



    }



    else if(A==4)



    {



        return 5;



    }



    else



    {



        return A + fun(A-4);



    }



}

Friday, October 15, 2010

Code for Chatruka's Parallelogram



#include<iostream>



#include<string>



using namespace std;



int main()



{



    int i,j,k,l,count=0,mov=0,n=1,end=0;



    char A[200],B[100][100],C[100][100],T[10];



    gets(T);



   
count=0;mov=0;n=1;end=0;



    if(T[0]=='E')



    {



       
gets(A);



        while((2*n*n)<strlen(A))



        {



           
n++;



        }



        if(n%2==0)



        {



           
n++;



        }



       
k=0;l=1;end=0;



        for(i=0;i<n;i++)



        {



            for(j=0;j<n;j++)



            {



               
if(k<strlen(A))



                {



                   
B[i][j]=A[k];



                   
k=k+2;



               
}



               
else



               
{



                   
end=1;



                   
break;



               
}



            }



            if(end==1)



            {break;}



        }



        while(i<n)



        {



            while(j<n)



            {



               
B[i][j]='/';



               
j++;



            }



           
i++;



           
j=0;



        }



       
end=0;



        for(i=0;i<n;i++)



        {



            for(j=0;j<n;j++)



            {



               
if(l<strlen(A))



               
{



                   
C[i][j]=A[l];



                   
l=l+2;



               
}



               
else



               
{



                   
end=1;



                   
break;



               
}



            }



            if(end==1)



            {break;}



        }



        while(i<n)



        {



            while(j<n)



            {



               
C[i][j]='/';



               
j++;



            }



           
i++;



           
j=0;



        }



       
cout<<"OUTPUT IS"<<endl;



       
cout<<A<<endl;



       
i=n/2;j=n/2;mov=0;



       
cout<<B[i][j]<<C[i][j];



       
count++;



        while(count<n*n)



        {



           
mov++;



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
j--;



                   
cout<<B[i][j]<<C[i][j];



                   
count++;



               
}



            }



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                    i--;



                   
cout<<B[i][j]<<C[i][j];



                   
count++;



               
}



            }



           
mov++;



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                
   j++;



                   
cout<<B[i][j]<<C[i][j];



                   
count++;



               
}



            }



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
i++;



                   
cout<<B[i][j]<<C[i][j];



                   
count++;



               
}



            }



        }



       
cout<<endl;



    }



    else if(T[0]=='D')



    {



       
gets(A);



        while((2*n*n)<strlen(A))



        {



           
n++;



        }



        if(n%2==0)



        {



           
n++;



        }



       
i=n/2;j=n/2;mov=0;l=0;



       
B[i][j]=A[l];l++;



       
C[i][j]=A[1];l++;



        while(count<n*n)



        {



           
mov++;



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
j--;



                   
B[i][j]=A[l];l++;



                   
C[i][j]=A[l];l++;



                   
count++;



               
}



            }



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
i--;



                   
B[i][j]=A[l];l++;



                   
C[i][j]=A[l];l++;



                   
count++;



               
}



            }



           
mov++;



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
j++;



                   
B[i][j]=A[l];l++;



                   
C[i][j]=A[l];l++;



                   
count++;



               
}



            }



            for(k=0;k<mov;k++)



            {



               
if(count<n*n)



               
{



                   
i++;



                   
B[i][j]=A[l];l++;



                   
C[i][j]=A[l];l++;



                   
count++;



               
}



            }



        }



       
cout<<"OUTPUT IS"<<endl;



        cout<<A<<endl;



        for(i=0;i<n;i++)



        {



            for(j=0;j<n;j++)



            {



               
if(B[i][j]!='/')



               
{cout<<B[i][j];}



               
if(C[i][j]!='/')



               
{cout<<C[i][j];}



            }



        }



       
cout<<endl;



    }    



    return 0;



}

Problem for Chatruka's Parallelogram

Raja Chatruka needs a message to be delivered to his spy. The message is supposed to be encoded so that none other than the spy can decode the message, who knows the process of encoding.


Earlier the Raja and the Spy both decided to adopt the process of “expanding parallelogram code”. The method encodes the message by placing the characters in an odd order parallelogram matrix and then reading it in a clockwise spiral from the center. Each cell of the parallelogram contains two characters; the one below is filled first as well as read first. The length of message determines the order of the matrix. The order needs to be minimum. If the number of characters are less than the number of cells in the matrix the remaining cells are filled with “/”. No message contains the actual character /.


For example: If the message is “this is a problem”



The output should be “a s this iprm/leob”


Your program must be able to encode and decode the message by this process.


INPUT The input will consist of a pair of lines. The first line will contain either of the two words “ENCODE” or “DECODE” in uppercase letters. The second line will contain the message(less than 100 characters) to be encoded or decoded depending upon the first line.


OUTPUT The output will consist of three lines. The first line will print “OUTPUT IS” in uppercase letters. The second line will print the original message, and the third line the resultant message. The character „/‟ should be removed to get the decoded message.


Note: Order of a matrix refers to the number of rows or columns.


SAMPLE INPUT:


ENCODE


this is a problem


SAMPLE OUTPUT:


OUTPUT IS


this is a problem a s this iprm/leob