水域大小

在这里插入图片描述

非递归方式

// 快速排序
void QuickSort(int* a, int low, int high){
    
    
   int i=low, j=high, temp;

    if(low<high){
    
    
        temp=a[i];
        while(i<j){
    
    
            while(i<j && a[j]>=temp)
                --j;
            a[i]=a[j];
            while(i<j && a[i]<temp)
                ++i;
            a[j]=a[i];
        }
        a[i]=temp;
        QuickSort(a, low, i-1);
        QuickSort(a, i+1, high);
    }
}

// 非递归方式求解
int* pondSizes(int** land, int landSize, int* landColSize, int* returnSize){
    
    
    struct{
    
    
        int row;
        int col;
    }st[landSize*landColSize[0]];
    int top, i, j, k=0, row, col, p, q, count;
    int visited[landSize][*landColSize];
    int* res = (int*)malloc(landSize*landColSize[0]*sizeof(int));
    
    int tmp[3] = {
    
    -1,0,1};

    for(i=0; i<landSize; ++i)
        for(j=0; j<*landColSize; ++j)
            visited[i][j]=0;
    
    for(i=0; i<landSize; ++i)
        for(j=0; j<*landColSize; ++j){
    
     
            top=-1; 
            if(land[i][j]==0 && visited[i][j]==0){
    
    
                ++top; st[top].row=i, st[top].col=j; count=0; visited[i][j]=1;
                while(top>-1){
    
    
                    row=st[top].row; col=st[top].col; --top; ++count;
                    for(p=0; p<3; ++p)
                        for(q=0; q<3; ++q){
    
    
                            if((row+tmp[p]<landSize) && (col+tmp[q]<*landColSize) && (row+tmp[p]>-1) && 
                                (col+tmp[q]>-1) && (land[row+tmp[p]][col+tmp[q]]==0) && visited[row+tmp[p]][col+tmp[q]]==0){
    
    
                                ++top; st[top].row=row+tmp[p]; st[top].col=col+tmp[q]; visited[row+tmp[p]][col+tmp[q]]=1;
                            }
                        }
                }
                res[k]=count; ++k;   
            }
        }
 
    
    QuickSort(res, 0, k-1);

    *returnSize = k;
    return res;
}

猜你喜欢

转载自blog.csdn.net/m0_46278903/article/details/113349396