-1

I am using the below code to solve the rat maze problem from geeksforgeeks.However I am getting the segmentation error and I am unable to debug it.Can someone guide me with the debugging? Here's the code:

class Solution{
    public:
    string x="";
    void rat(vector<vector<int>>&m,int n,vector<string>&ans,int i,int j)
    {
        cout<<"cool";
        if(i==n-1&&j==n-1)
        {ans.push_back(x);
        return;}
        
        if(m[i][j]==0)
        return;
       
        
        if(i<0||j<0||i==n||j==n)
        return ;
    
        if(i<n-1)
        {
            x+="D";
            rat(m,n,ans,i+1,j);
        }
        x.pop_back();
      
        if(j!=n-1)
        {
            x+="R";
            rat(m,n,ans,i,j+1);
        }
        x.pop_back();
     
        if(i>0)
        {
            x+="U";
            rat(m,n,ans,i-1,j);
        }
        x.pop_back();
      
        if(j>0)
        {
            x+="L";
            rat(m,n,ans,i,j-1);
        }
        x.pop_back();
        
    */
       
    }
    vector<string> findPath(vector<vector<int>> &m, int n) {
     
     vector<string>ans;
     
     rat(m,n,ans,0,0);
     
     if(ans.size()==0)
     return {"-1"};
     return ans;
    }
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    First of all create a [mre] which you can build and debug locally. Make sure that it replicates the problem you want to ask about, and *only* the problem you want to ask about. The code you doesn't, as it won't even build. Once you have a program which replicates your problem, use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to catch the crash "in action", to locate when and where in your code it happens, and examine variables and their values to try to figure out what the problem might be. – Some programmer dude Jul 11 '22 at 07:17
  • As for a couple possible things to look out for: Vector indexes out of bounds; Too deep recursion. – Some programmer dude Jul 11 '22 at 07:18
  • And finally a little pet peeve of mine: Don't use so-called "competition" or "judge" sites to learn programming languages, or programming in general. That's not their purpose. If you want to learn C++ invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take classes. – Some programmer dude Jul 11 '22 at 07:20
  • 2
    *I am using the below code to solve the rat maze problem from geeksforgeeks* -- First, that site is not one you should use to learn C++. It has bad coding samples, bad advice, and bad explanations on many topics. Second: *However I am getting the segmentation error and I am unable to debug it* -- If you wrote the code yourself, there is absolutely no excuse why you can't debug your own code. You wrote it with a plan in mind, so use the debugger to see where the program goes against what you planned. – PaulMcKenzie Jul 11 '22 at 07:27
  • Also: What do you want this lonely `*/` to do? It lacks an opening `/*`. – Ted Lyngmo Jul 11 '22 at 07:33

1 Answers1

2

Seem very likely to me that this code

    if(i>0)
    {
        x+="U";
        rat(m,n,ans,i-1,j);
    }
    x.pop_back();

should be

    if(i>0)
    {
        x+="U";
        rat(m,n,ans,i-1,j);
        x.pop_back();
    }

Same error several times.

The way you have written it, you will remove characters from x that were never put there in the first place.

john
  • 85,011
  • 4
  • 57
  • 81