Run time error Please Help (c++)


  • 0
    C
    void flip(int i, int j, vector<vector<char>> &map, bool & isflapable){
        if(i>=0 && i<map.size() && j>=0 && j<map[i].size()&& map[i][j] =='O' ){
            if(i==0 || i==map.size()-1 || j==0 || j==map[i].size()-1) isflapable = false;
            map[i][j] = 'X';
            flip(i+1,j,map,isflapable);
            flip(i-1,j,map,isflapable);
            flip(i,j+1,map,isflapable);
            flip(i,j-1,map,isflapable);
        }
    }
    
    void solve(vector<vector<char>>& board) {
        vector<vector<char>> map = board;
        for(int i = 0; i<map.size(); ++i){
            for(int j = 0; j<map[i].size();++j){
                if(map[i][j] == 'O'){
                    bool isflapable = true;
                    flip(i,j,map,isflapable);
                    if(isflapable){
                        bool dump;
                        flip(i,j,board,dump);
                    }
                }
            }
        }
    }

  • 1
    B

    I was also getting the same error. Due to stack overflow the problem is occurring. Instead of recursive calls use queue.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.