Hi Abhishek!
Two things:
{string a = rec(a, abs(x), abs(y));}
This line here is causing a Segmentation Fault. In C++, when we declare
variables in any way that does not use the default constructor (in this
case, the copy constructor), the lifetime of a variable does not begin
until initialization ends. In other words, string a does not exist until
after this line completes, so when the program looks for a so that it can
assign ans, it can't find a! Now, when we look at the rec(str,int,int)
function, it appears that ans is only ever equal to the empty string (""),
so let's just get rid of it wherever it appears. The new line looks like
this:
{string a = rec(abs(x), abs(y));}
The function declaration only has 2 parameters (x and y), and all of the
return statements in rec(int,int) are exactly as they were before, just
without ans.
Second, there are some inconsistencies with the checks to xx and yy where
the program looks to invert directions. I'll leave it to you to determine
which ones need adjustment.
Best,
Matt
On Tuesday, April 21, 2020 at 12:21:24 PM UTC-4, Abhishek Mohanty wrote:
>
> #include<bits/stdc++.h>
>
> using namespace std;
>
> string rec(string ans, int x, int y){
> if(x + y == 0){
> return "";
> }
> if(x == 1 && y == 0) return ans + 'E';
> if(x == -1 && y == 0) return ans + 'W';
> if(x == 0 && y == 1) return ans + 'N';
> if(x == 0 && y == -1) return ans + 'S';
> if(x % 2 == 0){
> if((((y + 1) + x) / 2) % 2 == 1) return ans + 'S' + rec(ans, x/2, (y +
> 1)/2);
>
> if((((y - 1) + x) / 2) % 2 == 1) return ans + 'N' + rec(ans, x/2, (y -
> 1)/2);
> }
> if(y % 2 == 0){
> if((((x + 1) + y) / 2) % 2 == 1) return ans + 'W' + rec(ans, (x + 1)/2,
> y/2);
> if((((x - 1) + y) / 2) % 2 == 1) return ans + 'E' + rec(ans, (x - 1)/2,
> y/2);
> }
> }
>
> void solve(){
> int xx, yy;
> cin>>xx>>yy;
> int x = abs(xx);
> int y = abs(yy);
> int total = x + y;
> if(total % 2 == 0){cout<<"IMPOSSIBLE"<<endl;return;}
> string a = rec(a, abs(x), abs(y));
> if(xx < 0 && yy < 0){
> string opp = "";
> for(char c: a){
> if(c == 'N') opp += 'N';
> if(c == 'S') opp += 'S';
> if(c == 'E') opp += 'E';
> if(c == 'W') opp += 'W';
> }
> a = opp;
> }
> if(xx < 0 && yy < 0){
> string opp = "";
> for(char c: a){
> if(c == 'N') opp += 'S';
> if(c == 'S') opp += 'N';
> if(c == 'E') opp += 'W';
> if(c == 'W') opp += 'E';
> }
> a = opp;
> }
> if(xx < 0 && yy > 0){
> string opp = "";
> for(char c: a){
> if(c == 'N') opp += 'N';
> if(c == 'S') opp += 'S';
> if(c == 'E') opp += 'W';
> if(c == 'W') opp += 'E';
> }
> a = opp;
> }
> if(xx > 0 && yy < 0){
> string opp = "";
> for(char c: a){
> if(c == 'N') opp += 'S';
> if(c == 'S') opp += 'N';
> if(c == 'E') opp += 'E';
> if(c == 'W') opp += 'W';
> }
> a = opp;
> }
> cout<<a<<endl;
> }
> int main(){
> int t;
> cin>>t;
> for(int tt = 1; tt<=t; tt++){
> cout<<"Case #"<<tt<<": ";
> solve();
> }
> return 0;
> }
>
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/fc97654f-24c2-4bec-97dc-2ac19bbea111%40googlegroups.com.