#Segmentation fault

15 messages · Page 1 of 1 (latest)

wise narwhal
#

I am writing a program to find all unique paths from 0,0 to the bottom right corner of a matrix, while only moving on open spaces (0) and not obstacles(1)
I used recursion to check each path, allowing the program to go up, down, right, left.
Every time I run the program, there is segmentation fault. Can someone help me figure out why? I can't find the solution after a long time.

#include<bits/stdc++.h>
using namespace std;
 
int  UniquePathHelper(int i, int j, int r, int c, vector<vector<int>>& A){
    // boundary condition or constraints
    if(i == r || j == c || i < 0 || j < 0){
      return 0 ;
    }
 
    if(A[i][j] == 1){
      return 0 ;
    }
     
    // base case
    if(i == r-1 && j == c-1){
      return 1 ;
    }
 
    return  UniquePathHelper(i+1, j, r, c, A) +
            UniquePathHelper(i, j+1, r, c, A) + 
            UniquePathHelper(i-1, j, r, c, A) +
            UniquePathHelper(i, j-1, r, c, A) ;
}
 
 
int uniquePathsWithObstacles(vector<vector<int>>& A)
{
     
    int r = A.size(), c = A[0].size();
 
     
    return UniquePathHelper(0, 0, r, c, A) ;
}
 
// Driver code
int main()
{
   vector<vector<int>> A = { { 0, 0, 0 },
                             { 0, 1, 0 },
                             { 0, 0, 0 } };
                              
   cout << uniquePathsWithObstacles(A) << " \n";                                               
}```
empty patioBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

wise narwhal
#

!howto ask

lime condorBOT
#
How to Ask A Programming Question

Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.

What To Post

State your problem clearly and provide all necessary details:
• the relevant portion of your code, or all of it
• the expected output
• the actual output (or the full error)
🏆 Gold Standard: Minimal Reproducible Example

Where To Post

Provide the relevant code in the message, and format it nicely with a code block*. If it's too much for one message, you can upload it:
Compiler Explorer for most C/C++ snippets
OnlineGDB for interaction, debugging
Do not post screenshots, let alone photos of your screen!

late pike
#
#include <vector>
#include <iostream>

int UniquePathHelper(int i, int j, int r, int c, std::vector<std::vector<int>>& A) {
  // boundary condition or constraints
  if (i >= r || j >= c || i < 0 || j < 0) {
    return 0;
  }

  if (A[i][j] == 1) {
    return 0;
  }

  // base case
  if (i == r - 1 && j == c - 1) {
    return 1;
  }

  A[i][j] = 1;

  return UniquePathHelper(i + 1, j, r, c, A) + UniquePathHelper(i, j + 1, r, c, A) + UniquePathHelper(i - 1, j, r, c, A) + UniquePathHelper(i, j - 1, r, c, A);
}

int uniquePathsWithObstacles(std::vector<std::vector<int>>& A) {
  int r = A.size(), c = A[0].size();

  return UniquePathHelper(0, 0, r, c, A);
}

// Driver code
int main() {
  std::vector<std::vector<int>> A = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};

  std::cout << uniquePathsWithObstacles(A) << std::endl;
}
kind blazeBOT
#
Program Output
2
late pike
#

you are forgetting to set visited squares as visited, it will run forever

#

and maybe use >= for the upper bounds just in case instead of ==

wise narwhal
#

oh yeah

#

let me try ur code gimme a sec

#

ok it works

#

i totally forgot the visited

#

thank you

#

!solved