赞
踩
/** * 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; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。