What's wrong with my flood_fill solution ??? help pls


  • 0
    R
    class Solution {
     public:
    int m, n ;
    
    void flood(vector<vector<char> >& a, int x, int y, char old , char neW){
        if(x < 0|| x >= m || y < 0 || y >= n)
            return ;
            
        if(a[x][y] != old)
            return ;
            
        a[x][y] = neW;
        
        flood(a,x+1,y,old,neW);
        flood(a,x-1,y,old,neW);
        flood(a,x,y+1,old,neW);
        flood(a,x,y-1,old,neW);
    }
    
    
    void solve(vector<vector<char>>& a) {
        if(a.empty()) return ;
        
         m = a.size();
         n = a[0].size();
        
        
        for(int i = 0 ; i < m; i++){
            for(int j = 0; j < n ; j++){
                if(a[i][j] == 'O'){
                    a[i][j] = '-';
                }
            }
        }
        
        for(int i = 0; i < m; i++){
            if(a[i][0] == '-'){
                flood(a,i,0,'-','O');
            }
        }
        for(int i = 0; i < m; i++){
            if(a[i][n-1] == '-'){
                flood(a,i,n-1,'-','O');
            }
        }
        
        for(int i = 0; i < n; i++){
            if(a[0][i] == '-'){
                flood(a,0,i,'-','O');
            }
        }
        
        for(int i = 0; i < n; i++){
            if(a[m-1][i] == '-'){
                flood(a,m-1,i,'-','O');
            }
        }
        
        for(int i = 0; i < m ; i++){
            for(int j = 0; j < n ;j++){
                if(a[i][j] == '-')
                    a[i][j] = 'X';
            }
        }
        
    }
    

    };


  • 0
    D

    I didnt see whole solution, but this approach might be problem w.g. ["000", "000", "000"] So you end up having X in middle even if its NOT surrounded by X


  • 0
    D

    I am also facing the same problem and unable to get why its not working.


Log in to reply
 

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