递归函数调用时出现分段错误

时间:2021-07-10 17:52:10

标签: c++ recursion vector stl segmentation-fault

我正在尝试解决 this 个问题。

这涉及递归回溯。我将已经访问过的 i 和 j 对存储在一个名为visited 的向量中,并每次都检查它,以便在路径中不重复(以避免循环)。

当 i = 2 且 j = 0 时,我递归调用 findPath(m,n) 时会出现问题。您可以使用调试器来理解我在说什么。调试器显示,分割错误发生在名为 stl_vector.h 的文件中,位于以下部分:

  reference
  operator[](size_type __n) _GLIBCXX_NOEXCEPT
  { return *(this->_M_impl._M_start + __n);//debugger highlights this line while showing the fault }

我注意到,如果我在每个 if 块中的 findPath 调用之前删除 visited.push_back(make_pair(i,j));,则不会发生分段错误。

这是代码。我已经发表了一些评论来确定错误的部分。有人可以告诉什么是错的。谢谢。

#include <iostream>
#include <vector>
#include <utility>
#include <vector>
#include <string>
#include <set>
using namespace std;
    
    class Solution
    {
        public:
        int i=0,j=0;
        set <string> paths;
        string path;
        vector<string> allpaths;
        vector <pair<int,int>> visited;
    
        bool alreadyVisited(int a, int b)
        {
            for(auto el: visited)
            {
                if(el.first == a  &&  el.second == b)
                return true;
            }
            return false;
        }
        vector<string> findPath(vector<vector<int>> &m, int n) {
            
            if(i==n-1 && j==n-1)
            {
                paths.insert(path);
                return allpaths; 
            }
            if(m.at(i).at(j)==0)
            return allpaths;
    
            if(m.at(i).at(j)==1)
            {
                if(i+1<=n-1  &&  j<=n-1  &&  i>=0  &&  j>=0  &&  m.at(i+1).at(j)==1  &&  alreadyVisited(i+1,j)==false)
                {
                    i++;// debugger stops here without any reason
                    path=path+"D";
                    visited.push_back(make_pair(i,j));// if i remove this sigsegv error doesnt happen
                    findPath(m,n);// on debugging problem is happening here
                    visited.resize(visited.size()-1);
                    i--;
                    path.resize(path.size()-1);
                }
                if(i<=n-1  &&  j+1<=n-1 && i>=0 && j>=0 && m.at(i).at(j+1)==1 && alreadyVisited(i,j+1)==false)
                {
                    j++;
                    path=path+"R";
                    visited.push_back(make_pair(i,j));
                    findPath(m,n);
                    visited.resize(visited.size()-1);
                    j--;
                    path.resize(path.size()-1);
                }
                if(i<n-1 && j<n-1 && i>=0 && j-1>=0 && m.at(i).at(j-1)==1 && alreadyVisited(i,j-1)==false)
                {
                    j--;
                    path=path+"L";
                    visited.push_back(make_pair(i,j));
                    findPath(m,n);
                    visited.resize(visited.size()-1);
                    j++;
                    path.resize(path.size()-1);
                }
                if(i<n-1 && j<n-1 && i-1>=0 && j>=0 && m.at(i-1).at(j)==1 && alreadyVisited(i-1,j)==false)
                {
                    i--;
                    path=path+"U";
                    visited.push_back(make_pair(i,j));
                    findPath(m,n);
                    visited.resize(visited.size()-1);
                    i++;
                    path.resize(path.size()-1);
                }
            }
            for(string str : paths)
            {
                allpaths.push_back(str);
            }
            return allpaths;
        }
    };
    
    int main() {
        int t;
        t=1;
        while (t--) {
            int n=4;
            vector<vector<int>> m(n, vector<int> (n,0)) ;
            m = {{1, 0, 0, 0},{1, 1, 0, 1},{1, 1, 0, 0},{0, 1, 1, 1}};     
            Solution obj;
            vector<string> result = obj.findPath(m, n);
            if (result.size() == 0)
                cout << -1;
            else
                for (int i = 0; i < result.size(); i++) cout << result.at(i) << " ";
            cout << endl;
        }
        return 0;
    } 

0 个答案:

没有答案