< Back to forum

my code is not passing few test cases , i am not getting which special cases i am missing of this questn... https://www.hackerrank.com/challenges/queens-attack-2/problem

#include<bits/stdc++.h>
using namespace std;

#define ll long long int

ll a[1000000]={0};
ll b[1000000]={0};


int main()
{
ll n,k,p,xq,yq,ans,x,y,xd,yd,i;

cin>>n>>k;

cin>>yq>>xq;

p=0;

 ll d[8];


 d[0]=xq;
 d[1]=n-xq+1;
 d[2]=xq;
 d[3]=n-yq+1;
 d[4]=xq;
 d[5]=n-yq+1;
 d[6]=yq;
 d[7]=n-yq+1;

while(k--)
{
 cin>>y>>x;
 xd=xq-x;
 yd=yq-y;

 if((abs(xd)==abs(yd))||(abs(xd)==0)||(abs(yd)==0))
 {

   if((yd==0)&&(xd>0))
    {
     d[0]=min(xd,d[0]);
    }


    else if((yd==0)&&(xd<0))
    {
     d[1]=min(abs(xd),d[1]);


    }


   else if((xd==yd)&&(xd>0))
    {
     d[2]=min(xd,d[2]);

    }

    else if((xd==yd)&&(xd<0))
    {
      d[3]=min(abs(yd),d[3]);

    }


    else if((xd==-yd)&&(xd>0))
    {

     d[4]=min(xd,d[4]);

    }


    else if((xd==-yd)&&(xd<0))
    {
     d[5]=min(abs(yd),d[5]);
    }

    else if((xd==0)&&(yd>0))
    {
     d[6]=min(yd,d[6]);
    }

    else if((xd==0)&&(yd<0))
    {
     d[7]=min(abs(yd),d[7]);
    }


 }

}



/* for(i=0;i<8;i++)
{
     d[i]=d[i]-1;
     cout<<d[i]<<" ";
}
*/


for(i=0;i<8;i++)
{
     d[i]=d[i]-1;
 //    cout<<d[i]<<" before ";
}


if((xq==n)&&(yq==n))
        d[4]=0;

if((xq==1)&&(yq==n))
        d[2]=0;


if((xq==n)&&(yq==1))
        d[5]=0;

if((xq==1)&&(yq==1))
        d[2]=0;



/*for(i=0;i<8;i++)
{

     cout<<d[i]<<" ";
}*/

ans=accumulate(d,d+8,0);
cout<<ans;


return 0;
}

 

Asked by: Manish_Kumar_Savita on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar

in this code, your diagonal calculation is not correct. any diagonal have the minimum of nearest vertical space and nearest horizontal space.

like :)

horizontal :-
    d1 = x-1;
    d2 = n-x;
vertical :-
    d3 = y-1;
    d4 = n-y;
diagonal :-
    d5 = min(x-1,y-1)
    d6 = min(x-1,n-y)
    d7 = min(n-x,y-1)
    d8 = min(n-x,n-y)
where x and y are cordinates of queen location in 1-based indexing.

 

rishup132 last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.