不知道为啥路径长度为0

路径长度总是为零不知道是哪里出了问题,用的是队列想通过队列求最短路径

#include<stdio.h>
#include<stdlib.h>
#define MAX 10000

typedef struct{
    int x; 
    int y;
    int l;
} DataType;

typedef struct {
    DataType data[MAX];
    int begin;
    int end;
} Queue;

Queue* createEmptyQueue(void) {
    Queue* q;
    q = (Queue*)malloc(sizeof(Queue));
    if (q == NULL)
        printf("malloc error!!\n");
    else {
        q->begin = 0;
        q->end = 0;
    }
    return q;
}

int full(Queue* q)
{
    if ((q->end + 1) % MAX == q->begin)
        return 1;
    else
        return 0;
}

int empty(Queue* q)
{
    if (q->begin == q->end)
        return 1;
    else
        return 0;
}

int enter(Queue* q, DataType t)
{
    if (full(q))
        return 0;
    q->data[q->end] = t;
    q->end = (q->end + 1) % MAX;
    return 1;
}

int exitQueue(Queue* q, DataType *t1)
{
    if (empty(q))
        return 0;
    *t1 = q->data[q->begin];
    q->begin = (q->begin + 1) % MAX;
}

int getl(Queue* q)
{
    return (q->end - q->begin + MAX) % MAX;
}

int main()
{
    int direction[][2] = { -1,0,1,0,0,-1,0,1 };
    int x1, x2, y1, y2;
    int i, j, m, n, a[50][50], b[50][50], flag = 0;
    DataType t, t1;
    
    scanf("%d %d", &m, &n);
    
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
            scanf("%d", &a[i][j]);
    }
    
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
            b[i][j] = a[i][j];
    }
    
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    t.x = x1;
    t.y = y1;
    t.l = 0 ;
    b[0][0]=1;
    Queue* q = createEmptyQueue();
    enter(q, t);
    printf("%d %d %d\n",q->begin,q->end,q->data[q->begin].l);
    while (!empty(q))
    {
        exitQueue(q, &t1);
        for (i = 0; i < 4; i++)
        {
            
            t.x = t1.x + direction[i][0];
            t.y = t1.y + direction[i][1];
            
            if (t.x >= 0 && t.x < m && t.y >= 0 && t.y < n && a[t.x][t.y] == 0 && b[t.x][t.y] ==0)
            {
                b[t.x][t.y] = 2;
                enter(q, t);
                q->data[q->end].l = q->data[q->begin].l+1;
                printf("%d %d %d\n",q->begin,q->end,q->data[q->begin].l);
            }
            
            if (t.x == x2 && t.y == y2)
            {
                flag = 1;
                break;
            }
        }
        
        if (flag)
            break;
    }
    
    if (flag)
        printf("%d", q->data[(q->end - 1 + MAX) % MAX].l);
    else
        printf("no path!\n");
    
    return 0;
}

在给定的代码中,最短路径输出为0的原因可能是在进入队列时没有正确更新t.l的值。

可以看到,在将元素t加入队列后,更新了t.l的值:

q->data[q->end].l = q->data[q->begin].l+1;

然而,在创建空队列并将第一个元素t加入队列之前,t.l的初始值没有被设置。因此,当第一个元素从队列中取出并进行遍历时,它的初始值仍然为0。

为了解决这个问题,可以在创建空队列并将第一个元素t加入队列之前,先将t.l的初始值设置为0:

t.l = 0;

修改后的代码如下所示:

...

t.x = x1;
t.y = y1;
t.l = 0;

b[0][0] = 1;
Queue* q = createEmptyQueue();
enter(q, t);
printf("%d %d %d\n", q->begin, q->end, q->data[q->begin].l);

while (!empty(q))
{
    exitQueue(q, &t1);
    for (i = 0; i < 4; i++)
    {
        t.x = t1.x + direction[i][0];
        t.y = t1.y + direction[i][1];

        if (t.x >= 0 && t.x < m && t.y >= 0 && t.y < n && a[t.x][t.y] == 0 && b[t.x][t.y] == 0)
        {
            b[t.x][t.y] = 2;
            enter(q, t);
            q->data[q->end].l = q->data[q->begin].l + 1;
            printf("%d %d %d\n", q->begin, q->end, q->data[q->begin].l);
        }

        if (t.x == x2 && t.y == y2)
        {
            flag = 1;
            break;
        }
    }

    if (flag)
        break;
}

...

通过这个修改,每个加入队列的元素都会在t.l上正确地增加1,从而确保输出的最短路径长度是正确的。

q->data是个数组,你一直在给q->data[q->end].l赋值,q->data[(q->end - 1 + MAX) % MAX].l没有赋值呀
既然你只需要一个最短的长度,那你直接定义一个全局变量l,修改它即可,不要搞个数组来放l,要那么多个l干啥呢