Run Time Error in the code ?


  • 0
    P

    class Solution
    {

    public :
    int n , m ;
    vector<vector<bool>> visited ;
    
    void solve(vector<vector<char>>& v)
    {   
    
        n = v.size() ;
        if(n != 0)
            m = v[0].size() ;
        else 
            return ;
        
        for(int i = 0 ; i < n ; i++)
        {
            vector<bool>temp(m,false) ;
            visited.push_back(temp) ; 
        }
        
        
        visit_boundary(v) ;
      
        
        for(int i = 0 ; i < n ; i ++)
        {
            for(int j = 0 ; j < m ; j ++)
            {
                if(visited[i][j] == false && v[i][j] == 'O')
                {
                    v[i][j] = 'X' ;
                    visited[i][j] = true ;
                }
            }
        }
        
        return ;
    }
    
    void visit_boundary(vector<vector<char>>&v)
    {
        for(int i = 0 ; i < n ; i++)
        {
            if(visited[i][0] == false && v[i][0] == 'O')
            {
               
                util(i,0,v) ;
            
                
            }
            if(visited[i][m-1] == false && v[i][m-1] == 'O')
            {
                util(i,m-1,v) ;
            }
            
        }
        
        
        
        for(int i = 0 ; i < m ; i ++)
        {
            if(visited[0][i] == false && v[0][i] == 'O')
            {
                util(0,i,v) ;
            }
            if(visited[n-1][i] == false && v[n-1][i] == 'O')
            {
                util(n-1,i,v) ; 
            }
        }
    }
    
    void util(int i , int j , vector<vector<char>>&v)
    {
        cout << "i >> " << i << "  j >> " << j << endl ; 
        if(i >= n || j >= m || i < 0 || j < 0)
        {
            return ;
        }
        else if (v[i][j] != 'O' || visited[i][j] == true)
        {
            return ;
        }
        else 
        {
            visited[i][j] = true ;
            util(i+1,j,v) ; util(i,j+1,v) ; util(i-1,j,v) ; util(i,j-1,v) ;
        }
    }
    

    };


Log in to reply
 

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