简体   繁体   中英

Segmentation fault: 11 of Maze problem in c++

The maze is represented by a 2x2 vector where a 0 represents a blocked and 1 represents a free cell. The goal is to find if there is a path between start and end, so I tried to recursive the function after judge whether the end can trace back to it last point. And if I visited each point, I change the point value to 2, and if I visited the start, I change the point value to 3.Finally, I check the start point, if it is 3,return true, else return false. But It shows the Segmentation fault: 11

and if I Commented out code:

if(input[e.first+1][e.second] == 1 && e.first+1 < input[0].size()) {
      if (e.first+1 == s.first && e.second == s.second){
        input[e.first+1][e.second] = 3;
      }else{
        e.first += 1;
        input[e.first][e.second] = 2;
        maze(input, s, e);
      }
    }

The answer is 0 1 1 0 0 0 1 1 1 1 1 0 1 0 0 1 2 0 0 0 0 2 2 2 2 2 1 3 1 0 0 0 2 0 1 1 1 2 2 0 0 1 2 2 0 0 1 2 2 2 2 2

The function:

bool Solution::maze(std::vector<std::vector<int>>& input, std::pair<int,int> s, std::pair<int,int> e){
  input[.first][e.second] = 2;
  if(e.first>=0 && e.second>=0 && e.first<input[0].size() && e.second<input.size()){
    if(input[e.first][e.second-1] == 1 && e.second-1 >= 0){
      if (e.first == s.first && e.second-1 == s.second){
        input[e.first][e.second-1] = 3;
      }else{
        e.second -= 1;
        input[e.first][e.second] = 2;
        maze(input, s, e);
      }
    }

    if(input[e.first+1][e.second] == 1 && e.first+1 < input[0].size()) {
      if (e.first+1 == s.first && e.second == s.second){
        input[e.first+1][e.second] = 3;
      }else{
        e.first += 1;
        input[e.first][e.second] = 2;
        maze(input, s, e);
      }
    }
    if(input[e.first-1][e.second] == 1 && e.first-1 >= 0){
      if (e.first-1 == s.first && s.second == e.second){
        input[e.first-1][e.second] = 3;
      }else{
        e.first -= 1;
        input[e.first][e.second] = 2;
        maze(input, s, e);
      }
    }
    if (input[e.first][e.second+1] == 1 && e.second+1 < input.size()){
       if(e.first == s.first && e.second+1 == s.second){
        input[e.first][e.second+1] = 3;
      }else{
        e.second += 1;
        input[e.first][e.second] = 2;
        maze(input, s, e);
      }
    }


  }
  if(input[s.first][s.second] == 3){
    return true;
  }else{
    return false;
  }

}

Main function:

int main() {
 Solution solution;
  std::vector<std::vector<int>> input = {{1, 1, 0, 0, 0},
                                         {1, 1, 1, 1, 1},
                                         {0, 1, 0, 0, 1},
                                         {1, 0, 0, 0, 0},
                                         {1, 1, 1, 1, 1}};
  std::pair<int,int> s(1,2);
  std::pair<int,int> e(4,4);

  std::cout << solution.maze(input, s, e) << std::endl;
  for(auto it = input.begin(); it != input.end(); ++it){
    std::vector<int> n = *it;
    for(auto it1 = n.begin(); it1 != n.end(); ++it1){
      int n1 = *it1;
      std::cout<< n1<< " ";
    }
    std::cout <<  std::endl;
  }

  std::vector<std::vector<int>> input1 = {{1, 1, 0, 0, 0},
                                          {1, 0, 1, 1, 1},
                                          {1, 1, 0, 0, 1},
                                          {1, 1, 0, 0, 1},
                                          {1, 1, 1, 1, 1}};
  std::pair<int,int> s1(0,0);
  std::pair<int,int> e1(4,4);

  std::cout << solution.maze(input1, s1, e1) << std::endl;
  for(auto it = input1.begin(); it != input1.end(); ++it){
    std::vector<int> n = *it;
    for(auto it1 = n.begin(); it1 != n.end(); ++it1){
      int n1 = *it1;
      std::cout<< n1<< " ";
    }
    std::cout <<  std::endl;
  }
}

The fault is probably due to one of your array loops calling an index out of range. When you're trying to access input[first][second] and one of those is being incremented or decremented like input[first+1] it could be out of bounds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM