螺旋矩阵问题不知道哪里有问题?

程序执行不起来。
题目

img


1 <= n <= 20

#include 
#include 

int** generateMatrix(int n, int* returnSize, int** returnColumnSizes) 
{
    if (n <= 0) 
    {
        *returnSize = 0;
        *returnColumnSizes = NULL;
        return NULL;
    }

    int** res = (int**)malloc(n * sizeof(int*));
    if (res == NULL)
    {
        *returnSize = 0;
        *returnColumnSizes = NULL;
        return NULL;
    }

    for (int i = 0; i < n; i++) 
    {
        res[i] = (int*)malloc(n * sizeof(int));
        if (res[i] == NULL)
        {
            free(res[i]);
        }
        else
        {
            for (int j = 0; j < n; j++)
            {
                res[i][j] = (int)malloc(sizeof(int));
            }
        }
    }

    int a1 = n;
    int a2 = n - 1;
    int f = 1;
    int c1 = 0, c2 = 0;
    int w = 1;

    while (a1 > 0 || a2 > 0)
    {
        for (int i = 0; i < a2; i++)
        {
            res[c1][c2] = w;
            c2 = c2 + f;
            w++;
        }
        for (int i = 0; i < a1; i++) 
        {
            res[c1][c2] = w;
            c1 = c1 + f;
            w++;
        }
        a2--;
        a1--;
        f = -f;
    }

    returnSize =&n;
    for (int i = 0; i < n; i++) 
    {
        (*returnColumnSizes)[i] = n;
    }

    return res;
}

int main() {
    int n;
    printf("Please enter a number for the size of the matrix:\n");
    scanf_s("%d", &n);

    int returnSize;
    int* returnColumnSizes;
    int** res = generateMatrix(n, &returnSize, &returnColumnSizes);
    if (res == NULL)
    {
        printf("ERROR: Failed to allocate memory.\n");
        return -1;
    }

    for (int i = 0; i < returnSize; i++)
    {
        for (int j = 0; j < returnColumnSizes[i]; j++)
        {
            printf("%d ", res[i][j]);
        }
        printf("\n");
    }

    for (int i = 0; i < returnSize; i++)
    {
        free(res[i]);
    }
    free(res);
    free(returnColumnSizes);
    return 0;
}

img

img


想问怎么解决这个问题?

Do not let me think!
仅供参考:

#include <stdio.h>
#define MAXN 100
int m[MAXN+2][MAXN+2];
char d;
int x,y,k,n,w;
char str[10];
void main() {
    while (1) {
        printf("Input n(1..%d):",MAXN);
        fflush(stdout);
        rewind(stdin);
        if (1==scanf("%d",&n)) {
            if (1<=n && n<=MAXN) break;
        }
    }
    y=0  ;for (x=0;x<=n+1;x++) m[y][x]=1;
    y=n+1;for (x=0;x<=n+1;x++) m[y][x]=1;
    x=0  ;for (y=0;y<=n+1;y++) m[y][x]=1;
    x=n+1;for (y=0;y<=n+1;y++) m[y][x]=1;
    for (y=1;y<=n;y++) {
        for (x=1;x<=n;x++) {
            m[y][x]=0;
        }
    }
    x=1;
    y=1;
    k=0;
    d='D';
    while (1) {
        k++;
        if (k>n*n) break;
        m[y][x]=k;
        switch (d) {
            case 'D':
                if (0==m[y+1][x])  y++;
                else              {x++;d='R';}
            break;
            case 'R':
                if (0==m[y][x+1])  x++;
                else              {y--;d='U';}
            break;
            case 'U':
                if (0==m[y-1][x])  y--;
                else              {x--;d='L';}
            break;
            case 'L':
                if (0==m[y][x-1])  x--;
                else              {y++;d='D';}
            break;
        }
    }
    w=sprintf(str,"%d",n*n);
    for (y=1;y<=n;y++) {
        for (x=1;x<=n;x++) {
            printf(" %0*d",w,m[y][x]);
        }
        printf("\n");
    }
}



不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^