当前位置:   article > 正文

力扣------01 矩阵_return an array of arrays of size *returnsize. * t

return an array of arrays of size *returnsize. * the sizes of the arrays are

在这里插入图片描述

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
 int min(int a,int b,int c,int d){
     int e;
     e=a;
     if(e>b){
         e=b;
     }
     if(e>c){
         e=c;
     }
     if(e>d){
         e=d;
     }
     return e;
 }
int** updateMatrix(int** mat, int matSize, int* matColSize, int* returnSize, int** returnColumnSizes){
    *returnSize=matSize;
    *returnColumnSizes=matColSize;
    int **dis = (int**)malloc(sizeof(int*) * matSize);  //sizeof(int*),不能少*,一个指针的内存大小,每个元素是一个指针。
    for (int i = 0;i < matSize;i++)
    {
        dis[i] = (int*)malloc(sizeof(int) * matColSize[0]);
    }
    for(int a=0;a<matSize;a++){
        for(int b=0;b<matColSize[0];b++){
            dis[a][b]=10000;
        }
    }   
    for(int a=0;a<matSize;a++){
        for(int b=0;b<matColSize[0];b++){
            if(mat[a][b]==0){
                dis[a][b]=0;
            }
            else{
                if(a-1==-1&&a+1==matSize&&b-1==-1){
                    dis[a][b]=min(10000,10000,10000,dis[a][b+1])+1;
                }
                else if(a-1==-1&&a+1==matSize&&b+1==matColSize[0]){
                    dis[a][b]=min(10000,10000,dis[a][b-1],10000)+1;
                }           
                else if(a-1==-1&&a+1==matSize){
                    dis[a][b]=min(10000,10000,dis[a][b-1],dis[a][b+1])+1;
                }  
                else if(b-1==-1&&b+1==matColSize[0]&&a+1==matSize){
                    dis[a][b]=min(dis[a-1][b],10000,10000,10000)+1;
                }
                else if(b-1==-1&&b+1==matColSize[0]&&a-1==-1){
                    dis[a][b]=min(10000,dis[a+1][b],10000,10000)+1;
                }
                else if(b-1==-1&&b+1==matColSize[0]){
                    dis[a][b]=min(dis[a-1][b],dis[a+1][b],10000,10000)+1;
                }
                else if(a-1==-1&&b-1==-1){
                    dis[a][b]=min(10000,dis[a+1][b],10000,dis[a][b+1])+1;
                }
                else if(a-1==-1&&b+1==matColSize[0]){
                    dis[a][b]=min(10000,dis[a+1][b],dis[a][b-1],10000)+1;
                }
                else if(a+1==matSize&&b-1==-1){
                    dis[a][b]=min(dis[a-1][b],10000,10000,dis[a][b+1])+1;
                }
                else if(a+1==matSize&&b+1==matColSize[0]){
                    dis[a][b]=min(dis[a-1][b],10000,dis[a][b-1],10000)+1;
                }
                else if(a-1==-1){
                    dis[a][b]=min(10000,dis[a+1][b],dis[a][b-1],dis[a][b+1])+1;
                }
                else if(a+1==matSize){
                    dis[a][b]=min(dis[a-1][b],10000,dis[a][b-1],dis[a][b+1])+1;
                }
                else if(b-1==-1){
                    dis[a][b]=min(dis[a-1][b],dis[a+1][b],10000,dis[a][b+1])+1;
                }
                else if(b+1==matColSize[0]){
                    dis[a][b]=min(dis[a-1][b],dis[a+1][b],dis[a][b-1],10000)+1;
                }
                else{
                    dis[a][b]=min(dis[a-1][b],dis[a+1][b],dis[a][b-1],dis[a][b+1])+1;
                }
            }
        }
    }
    for(int a=matSize-1;a>=0;a--){
        for(int b=matColSize[0]-1;b>=0;b--){
            if(mat[a][b]==0){
                dis[a][b]=0;
            }
            else{
                if(a-1==-1&&a+1==matSize&&b-1==-1){
                    dis[a][b]=min(10000,10000,10000,dis[a][b+1])+1;
                }
                else if(a-1==-1&&a+1==matSize&&b+1==matColSize[0]){
                    dis[a][b]=min(10000,10000,dis[a][b-1],10000)+1;
                }           
                else if(a-1==-1&&a+1==matSize){
                    dis[a][b]=min(10000,10000,dis[a][b-1],dis[a][b+1])+1;
                }  
                else if(b-1==-1&&b+1==matColSize[0]&&a+1==matSize){
                    dis[a][b]=min(dis[a-1][b],10000,10000,10000)+1;
                }
                else if(b-1==-1&&b+1==matColSize[0]&&a-1==-1){
                    dis[a][b]=min(10000,dis[a+1][b],10000,10000)+1;
                }
                else if(b-1==-1&&b+1==matColSize[0]){
                    dis[a][b]=min(dis[a-1][b],dis[a+1][b],10000,10000)+1;
                }
                else if(a-1==-1&&b-1==-1){
                    dis[a][b]=min(10000,dis[a+1][b],10000,dis[a][b+1])+1;
                }
                else if(a-1==-1&&b+1==matColSize[0]){
                    dis[a][b]=min(10000,dis[a+1][b],dis[a][b-1],10000)+1;
                }
                else if(a+1==matSize&&b-1==-1){
                    dis[a][b]=min(dis[a-1][b],10000,10000,dis[a][b+1])+1;
                }
                else if(a+1==matSize&&b+1==matColSize[0]){
                    dis[a][b]=min(dis[a-1][b],10000,dis[a][b-1],10000)+1;
                }
                else if(a-1==-1){
                    dis[a][b]=min(10000,dis[a+1][b],dis[a][b-1],dis[a][b+1])+1;
                }
                else if(a+1==matSize){
                    dis[a][b]=min(dis[a-1][b],10000,dis[a][b-1],dis[a][b+1])+1;
                }
                else if(b-1==-1){
                    dis[a][b]=min(dis[a-1][b],dis[a+1][b],10000,dis[a][b+1])+1;
                }
                else if(b+1==matColSize[0]){
                    dis[a][b]=min(dis[a-1][b],dis[a+1][b],dis[a][b-1],10000)+1;
                }
                else{
                    dis[a][b]=min(dis[a-1][b],dis[a+1][b],dis[a][b-1],dis[a][b+1])+1;
                }
            }
        }
    }
    return dis;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号