标题:关于BFS求解迷宫问题,代码未报错,运行无结果,恳请大佬指教
只看楼主
魔云子
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2018-12-2
 问题点数:0 回复次数:12 
关于BFS求解迷宫问题,代码未报错,运行无结果,恳请大佬指教
#include<stdlib.h>
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
#define M 4
#define N 4
//以下定义邻接表类型
typedef struct ANode            //边的结点结构类型
{
    int i, j;                    //该边的终点位置(i,j)
    struct ANode *nextarc;      //指向下一条边的指针
} ArcNode;

typedef struct Vnode            //邻接表头结点的类型
{
    ArcNode *firstarc;          //指向第一条边
} VNode;

typedef struct
{
    VNode adjlist[M + 2][N + 2];    //邻接表头节点数组
} ALGraph;                      //图的邻接表类型

typedef struct
{
    int i;                      //当前方块的行号
    int j;                      //当前方块的列号
} Box;

typedef struct
{
    Box data[MaxSize];
    int length;                 //路径长度
} PathType;                     //定义路径类型

int visited[M + 2][N + 2] = { 0 };
int count = 0;
void CreateList(ALGraph *&G, int mg[][N + 2])
//建立迷宫数组对应的邻接表G
{
    int i, j, i1, j1, di;
    ArcNode *p;
    G = (ALGraph *)malloc(sizeof(ALGraph));
    for (i = 0; i < M + 2; i++)                   //给邻接表中所有头节点的指针域置初值
        for (j = 0; j < N + 2; j++)
            G->adjlist[i][j].firstarc = NULL;
    for (i = 1; i <= M; i++)                    //检查mg中每个元素
        for (j = 1; j <= N; j++)
            if (mg[i][j] == 0)
            {
                di = 0;
                while (di < 4)
                {
                    switch (di)
                    {
                    case 0:
                        i1 = i - 1;
                        j1 = j;
                        break;
                    case 1:
                        i1 = i;
                        j1 = j + 1;
                        break;
                    case 2:
                        i1 = i + 1;
                        j1 = j;
                        break;
                    case 3:
                        i1 = i, j1 = j - 1;
                        break;
                    }
                    if (mg[i1][j1] == 0)                          //(i1,j1)为可走方块
                    {
                        p = (ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p
                        p->i = i1;
                        p->j = j1;
                        p->nextarc = G->adjlist[i][j].firstarc;   //将*p节点链到链表后
                        G->adjlist[i][j].firstarc = p;
                    }
                    di++;
                }
            }
}
//输出邻接表G
void DispAdj(ALGraph *G)
{
    int i, j;
    ArcNode *p;
    for (i = 0; i < M + 2; i++)
        for (j = 0; j < N + 2; j++)
        {
            printf("  [%d,%d]: ", i, j);
            p = G->adjlist[i][j].firstarc;
            while (p != NULL)
            {
                printf("(%d,%d)  ", p->i, p->j);
                p = p->nextarc;
            }
            printf("\n");
        }
}
void FindPath(ALGraph *G, int xi, int yi, int xe, int ye, PathType path)
{
    ArcNode *p;
    visited[xi][yi] = 1;                   //置已访问标记
    path.data[path.length].i = xi;
    path.data[path.length].j = yi;
    path.length++;
    if (xi == xe && yi == ye)
    {
        printf("  迷宫路径%d: ", ++count);
        for (int k = 0; k < path.length; k++)
            printf("(%d,%d) ", path.data[k].i, path.data[k].j);
        printf("\n");
    }
    p = G->adjlist[xi][yi].firstarc;  //p指向顶点v的第一条边顶点
    while (p != NULL)
    {
        if (visited[p->i][p->j] == 0) //若(p->i,p->j)方块未访问,递归访问它
            FindPath(G, p->i, p->j, xe, ye, path);
        p = p->nextarc;               //p指向顶点v的下一条边顶点
    }
    visited[xi][yi] = 0;
}

int main()
{
    ALGraph *G;
    int mg[M + 2][N + 2] =                           //迷宫数组
    {
        {1,1,1,1,1,1},
        {1,0,0,0,1,1},
        {1,0,1,0,0,1},
        {1,0,0,0,1,1},
        {1,1,0,0,0,1},
        {1,1,1,1,1,1}
    };
    CreateList(G, mg);
    printf("迷宫对应的邻接表:\n");
    DispAdj(G); //输出邻接表
    PathType path;
    path.length = 0;
    printf("所有的迷宫路径:\n");
    FindPath(G, 1, 1, M, N, path);
    return 0;
}
其上主要是FindPath函数调用好像没结果,不知道哪里出错了,求指点
搜索更多相关主题的帖子: 迷宫 int path for printf 
2018-12-02 17:00
魔云子
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2018-12-2
得分:0 
求教,大佬快出现
2018-12-02 17:39
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
得分:0 
void CreateList(ALGraph *&G, int mg[][N + 2])
//建立迷宫数组对应的邻接表G
这句是什么意思?我复制过来报一堆错?

saber,别哭.
2018-12-02 17:42
魔云子
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2018-12-2
得分:0 
回复 3楼 幻紫灵心
不应该吧,我用的是VS,没有错误,这一个函数块的作用就是将二维迷宫数组转变为图的邻接表类型
2018-12-02 17:56
魔云子
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2018-12-2
得分:0 
回复 3楼 幻紫灵心
2018-12-02 17:59
魔云子
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2018-12-2
得分:0 
回复 3楼 幻紫灵心
2018-12-02 17:59
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
得分:0 
改的时候忘了写注释了,主要就是你在函数内对函数外的地址申请内存,内存是带不出去的,离开函数就自动释放了。
.c 里面不能在for里面int 声明

程序代码:
#include<stdlib.h>
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
#define M 4
#define N 4
//以下定义邻接表类型
typedef struct ANode            //边的结点结构类型
{
    int i, j;                    //该边的终点位置(i,j)
    struct ANode *nextarc;      //指向下一条边的指针
} ArcNode;

typedef struct Vnode            //邻接表头结点的类型
{
    ArcNode *firstarc;          //指向第一条边
} VNode;

typedef struct
{
    VNode adjlist[M + 2][N + 2];    //邻接表头节点数组
} ALGraph;                      //图的邻接表类型

typedef struct
{
    int i;                      //当前方块的行号
    int j;                      //当前方块的列号
} Box;

typedef struct
{
    Box data[MaxSize];
    int length;                 //路径长度
} PathType;                     //定义路径类型

int visited[M + 2][N + 2] = { 0 };
int count = 0;
void CreateList(ALGraph **G, int mg[][N + 2])
//建立迷宫数组对应的邻接表G
{
    int i, j, i1, j1, di;
    ArcNode *p;
    (*G) = (ALGraph *)malloc(sizeof(ALGraph));
    for (i = 0; i < M + 2; i++)                   //给邻接表中所有头节点的指针域置初值
        for (j = 0; j < N + 2; j++)
            (*G)->adjlist[i][j].firstarc = NULL;
    for (i = 1; i <= M; i++)                    //检查mg中每个元素
        for (j = 1; j <= N; j++)
            if (mg[i][j] == 0)
            {
                di = 0;
                while (di < 4)
                {
                    switch (di)
                    {
                    case 0:
                        i1 = i - 1;
                        j1 = j;
                        break;
                    case 1:
                        i1 = i;
                        j1 = j + 1;
                        break;
                    case 2:
                        i1 = i + 1;
                        j1 = j;
                        break;
                    case 3:
                        i1 = i, j1 = j - 1;
                        break;
                    }
                    if (mg[i1][j1] == 0)                          //(i1,j1)为可走方块
                    {
                        p = (ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p
                        p->i = i1;
                        p->j = j1;
                        p->nextarc = (*G)->adjlist[i][j].firstarc;   //将*p节点链到链表后
                        (*G)->adjlist[i][j].firstarc = p;
                    }
                    di++;
                }
            }
}
//输出邻接表G
void DispAdj(ALGraph *G)
{
    int i, j;
    ArcNode *p;
    for (i = 0; i < M + 2; i++)
        for (j = 0; j < N + 2; j++)
        {
            printf("  [%d,%d]: ", i, j);
            p = G->adjlist[i][j].firstarc;
            while (p != NULL)
            {
                printf("(%d,%d)  ", p->i, p->j);
                p = p->nextarc;
            }
            printf("\n");
        }
}
void FindPath(ALGraph *G, int xi, int yi, int xe, int ye, PathType path)
{
    int k;
    ArcNode *p;
    visited[xi][yi] = 1;                   //置已访问标记
    path.data[path.length].i = xi;
    path.data[path.length].j = yi;
    path.length++;
    if (xi == xe && yi == ye)
    {
        printf("  迷宫路径%d: ", ++count);
        for (k = 0; k < path.length; k++)
            printf("(%d,%d) ", path.data[k].i, path.data[k].j);
        printf("\n");
    }
    p = G->adjlist[xi][yi].firstarc;  //p指向顶点v的第一条边顶点
    while (p != NULL)
    {
        if (visited[p->i][p->j] == 0) //若(p->i,p->j)方块未访问,递归访问它
            FindPath(G, p->i, p->j, xe, ye, path);
        p = p->nextarc;               //p指向顶点v的下一条边顶点
    }
    visited[xi][yi] = 0;
}

int main()
{
    ALGraph *G;
    int mg[M + 2][N + 2] =                           //迷宫数组
    {
        {1,1,1,1,1,1},
        {1,0,0,0,1,1},
        {1,0,1,0,0,1},
        {1,0,0,0,1,1},
        {1,1,0,0,0,1},
        {1,1,1,1,1,1}
    };
    CreateList(&G, mg);
    printf("迷宫对应的邻接表:\n");
    DispAdj(G); //输出邻接表
    PathType path;
    path.length = 0;
    printf("所有的迷宫路径:\n");
    FindPath(G, 1, 1, M, N, path);
    return 0;
}

saber,别哭.
2018-12-02 18:01
魔云子
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2018-12-2
得分:0 
没有路径产生
2018-12-02 18:01
魔云子
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2018-12-2
得分:0 
回复 7楼 幻紫灵心
呃呃,不好意思,刚刚那个是DFS算法,并没有错误,现在这个是BFS
程序代码:
#include<stdlib.h>
#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
#define M 4
#define N 4
//以下定义邻接表类型
typedef struct ANode            //边的结点结构类型
{
    int i, j;                    //该边的终点位置(i,j)
    struct ANode *nextarc;      //指向下一条边的指针
} ArcNode;

typedef struct Vnode            //邻接表头结点的类型
{
    ArcNode *firstarc;          //指向第一条边
} VNode;

typedef struct
{
    VNode adjlist[M + 2][N + 2];    //邻接表头节点数组
} ALGraph;                      //图的邻接表类型

typedef struct
{
    int i;                      //当前方块的行号
    int j;                      //当前方块的列号
} Box;

typedef struct
{
    Box data[MaxSize];
    int length;                 //路径长度
} PathType;                     //定义路径类型

typedef struct
{
    int i;
    int j;
    int parent;
}queue;

int visited[M + 2][N + 2] = { 0 };
int count = 0;

void CreateList(ALGraph *&G, int mg[][N + 2])
//建立迷宫数组对应的邻接表G
{
    int i, j, i1, j1, di;
    ArcNode *p;
    G = (ALGraph *)malloc(sizeof(ALGraph));
    for (i = 0; i < M + 2; i++)                   //给邻接表中所有头节点的指针域置初值
        for (j = 0; j < N + 2; j++)
            G->adjlist[i][j].firstarc = NULL;
    for (i = 1; i <= M; i++)                    //检查mg中每个元素
        for (j = 1; j <= N; j++)
            if (mg[i][j] == 0)
            {
                di = 0;
                while (di < 4)
                {
                    switch (di)
                    {
                    case 0:
                        i1 = i - 1;
                        j1 = j;
                        break;
                    case 1:
                        i1 = i;
                        j1 = j + 1;
                        break;
                    case 2:
                        i1 = i + 1;
                        j1 = j;
                        break;
                    case 3:
                        i1 = i, j1 = j - 1;
                        break;
                    }
                    if (mg[i1][j1] == 0)                          //(i1,j1)为可走方块
                    {
                        p = (ArcNode *)malloc(sizeof(ArcNode));   //创建一个节点*p
                        p->i = i1;
                        p->j = j1;
                        p->nextarc = G->adjlist[i][j].firstarc;   //将*p节点链到链表后
                        G->adjlist[i][j].firstarc = p;
                    }
                    di++;
                }
            }
}
//输出邻接表G
void DispAdj(ALGraph * &G)
{
    int i, j;
    ArcNode *p;
    for (i = 0; i < M + 2; i++)
        for (j = 0; j < N + 2; j++)
        {
            printf("  [%d,%d]: ", i, j);
            p = G->adjlist[i][j].firstarc;
            while (p != NULL)
            {
                printf("(%d,%d)  ", p->i, p->j);
                p = p->nextarc;
            }
            printf("\n");
        }
}
void FindPath(ALGraph * &G, int xi, int yi, int xe, int ye)
{
    ArcNode * p; 
    int ti, tj,i;
    queue qu[MaxSize];
    int front = -1; int rear = -1;
    rear++;
    qu[rear].i = xi;
    qu[rear].j = yi;
    qu[rear].parent = -1;
    visited[xi][yi] = 1;
    while (front != rear)
    {
        front++;
        ti = qu[front].i;
        tj = qu[front].j;
        if (ti == xe && tj == ye)
        {
            i = front;
            while (qu[i].parent != -1)
            {
                printf("迷宫路径:");
                printf("(%d,%d)", qu[i].i, qu[i].j);
                i = qu[i].parent;
            }
            printf("(%d,%d)\n", qu[i].i, qu[i].j);
            return;
        }
        p = G->adjlist[xi][yi].firstarc;  //p指向顶点v的第一个边节点
        while (p != NULL)
        {
            if (visited[p->i][p->j] == 0)
            {
                visited[p->i][p->j] = 1;
                rear++;
                qu[rear].i = p->i;
                qu[rear].j = p->j;
                qu[rear].parent = front;
            }
            p = p->nextarc;
        }
    }

}
int main()
{
    ALGraph *G;
    int mg[M + 2][N + 2] =                           //迷宫数组
    {
        {1,1,1,1,1,1},
        {1,0,0,0,1,1},
        {1,0,1,0,0,1},
        {1,0,0,0,1,1},
        {1,1,0,0,0,1},
        {1,1,1,1,1,1}
    };
    CreateList(G, mg);
    printf("迷宫对应的邻接表:\n");
    DispAdj(G); //输出邻接表
    printf("最短的迷宫路径:\n");
    FindPath(G, 1, 1, 3, 3);
    return 0;
}
2018-12-02 18:07
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
得分:0 
7楼的你看懂为啥了,举一反三都一样。

saber,别哭.
2018-12-02 18:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-491755-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.182860 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved