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

bool rec(int i,int j, int x, int y, int p, int str[])
{
    if(i==x && j==y)
    {
        str[0] = p;
        return true;
    }
    
    if(x>0 && (i>x||i<(x*-1)))
        return false;
    if(x<0 && (i<x||i>(x*-1)))
        return false;
    if(y>0 && (j>y||j<(y*-1)))
        return false;
    if(y<0 && (j<y||j>(y*-1)))
        return false;

    int j_len = pow(2,p);
    p++;
    
    if(rec(i+j_len, j, x, y, p, str))
    {
        // cout<<'E';
        str[p] = 'E';
        return true;
    }
    if(rec(i, j+j_len, x, y, p, str))
    {
        // cout<<'N';
        str[p] = 'N';
        return true;
    }
    if(rec(i-j_len, j, x, y, p, str))
    {
        // cout<<'W';
        str[p] = 'W';
        return true;
    }
    if(rec(i, j-j_len, x, y, p, str))
    {
        // cout<<'S';
        str[p] = 'S';
        return true;
    }
    return false;
}

void solve()
{
    int x,y;
    cin>>x>>y;
    
    int str[1000000000] = {0};
    if(rec(0,0,x,y,0,str)==false)
    { 
        cout<<"IMPOSSIBLE"<<endl;
        return;
    }
    
    for(int i=1;str[i] != 0;i++)
        printf("%c",str[i]);
    cout<<endl;
}

main()
{
    int t_case;
    cin>>t_case;
    
    for(int i=0;i<t_case;i++)
    {
        cout<<"Case #"<<i+1<<": ";
        solve();
    }
}

Usually Codes written on c++14 works on C++17 compiler but not always and I can't understand the difference. Can someone help.

I tried searching online but that didn't helped much.

Also Can someone who regularly Code or is a professional coder can tell me that does my code looks good and written nicely or its messy.

Code's purpose is to give a string as output consisting N,S,E,W representing four directions. a person can jump 2^(the number of jump starting from 0). (Our starting position is 0,0). Suppose the inputs are 2,3 than Output will be SEN .

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Mayank_pawar
  • 54
  • 10
  • 3
    What's the error you have got? – prehistoricpenguin Apr 24 '21 at 09:29
  • 1
    _that does my code looks good and written nicely or its messy_ That's easy: With the lack of proper indentation - rather the latter. Furthermore `#include` is bad habit. ([Why should I not #include ?](https://stackoverflow.com/q/31816095/7478597)) – Scheff's Cat Apr 24 '21 at 09:30
  • 5
    `int str[1000000000] = {0};` will - under most modern operating systems - probably exceed stack quotas for your process. You're lucky it ever worked at all, with any C++ version, since few host operating systems will allow a process to have a 4GB stack. – Peter Apr 24 '21 at 09:30
  • 2
    `int j_len = pow(2,p);` -> For integral powers of 2, shift operator is the better (and more accurate) choice. – Scheff's Cat Apr 24 '21 at 09:31
  • 2
    `#include` -- Get rid of this line and include the proper headers. – PaulMcKenzie Apr 24 '21 at 09:32
  • @Scheff thank you. Also i recently started use to bits/stdc++.h . I saw others using it and it had all the functions as in iostream so I used it. I will keep it in my mind from the next time. – Mayank_pawar Apr 24 '21 at 10:40
  • @Mr.HITMAN Please don't remove the `c++` tag. `c++??` tags can be used in addition to it, not instead of it. – HolyBlackCat Jun 01 '22 at 17:53
  • 1
    `main() {` without a return type isn't valid – Mat Jun 01 '22 at 17:59

0 Answers0