c code for number of island


  • 0
    T
    int label(char** grid,int row,int col,int gridRowSize, int gridColSize );
    int numIslands(char** grid, int gridRowSize, int gridColSize) {
        int row,col;
        int newlabel;
        newlabel=0;
        for(row=0;row<=gridRowSize-1;row++)
        {
           for(col=0;col<=gridColSize-1;col++) 
           {
               newlabel=newlabel+label(grid,row,col,gridRowSize,gridColSize);
           }
            
        }
        
        return newlabel;
    }
    
    int label(char** grid,int row,int col,int gridRowSize, int gridColSize )
    {
        if (*(*(grid+row)+col)=='1')
        {
            *(*(grid+row)+col)='0';
            if (row!=0)
                label(grid,row-1,col,gridRowSize,gridColSize );
            if (row!=gridRowSize-1)    
                label(grid,row+1,col,gridRowSize,gridColSize );
            if (col!=0)
                label(grid,row,col-1,gridRowSize,gridColSize );
            if (col!=gridColSize-1)    
                label(grid,row,col+1,gridRowSize,gridColSize ); 
            
            return 1;
        }
        else    
            return 0;
      
    }
    

Log in to reply
 

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