虚心吴 2022-04-06 23:13 采纳率: 100%
浏览 85
已结题

c语言用栈解决迷宫问题,求各位帮助帮助

问题遇到的现象和发生背景

上机课敲了两个小时,求指点!

问题相关代码,请勿粘贴截图
#include<stdio.h>
#include<malloc.h>
#define m 4
#define n 4
#define max 100
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}
};
typedef struct
{
    int i,j;//行,列
    int di ;
} box;
typedef struct
{
    box data[max];
    int top;
} st;
void initst(st *& s)//初始化 
{
    s=(st*)malloc(sizeof(st));
    s->top=-1;
}
bool push(st *&s,box e)//进栈 
{
    if(s->top==max-1)
        return false;
    s->top++;
    s->data[s->top]=e;
    return true;
}
bool stempty(st *s)//检查空栈 
{
    if(s->top==-1)
        return false;
    return true;    
}
bool gettop(st *s,box &e)//取栈顶 
{
    if(s->top==-1)
        return false;
    e=s->data[s->top];
    return true;
}
bool pop(st *&s,box &e)//出栈 
{
    if(s->top==-1)
        return false;
    e=s->data[s->top];
    s->top--;
    return true;
}
void destroyst(st *&s)//销毁栈 
{
    free(s);
}
bool mgpath(int xi,int yi,int xe,int ye)
{    
    box path[max],e;
    int i,j,di,il,jl,k;
    bool find;
    st *s;
    initst(s);
    e.i=xi;e.j=yi;e.di=-1;
    mg[xi][yi]=-1;
    push(s,e);
    while(!stempty(s))
    {
        gettop(s,e);
        i=e.i;j=e.j,di=e.di;
        if(i==xe&&j==ye)
        {
            printf("迷宫的一条路径如下: \n");
            k=0;
            while(!stempty(s))
            {
                pop(s,e);
                path[k++]=e;
            }
            while(k>=1)
            {
                k--;
                printf("(%d,%d)",path[k].i,path[k].j);
            }
            printf("\n");
            destroyst(s);
            return true;
        }
        find=false;
        while(di<4&&!find)
        {
            di++;
            switch(di)
            {
                case 0:il=i-1;jl=j;break;
                case 1:il=i;jl=j+1;break;
                case 2:il=i+1;jl=j;break;
                case 3:il=i;jl=j-1;break;
            }        
            if(mg[il][jl]==0)
                find=true;
        }
        if(find)
        {
            s->data[s->top].di=di;
            e.i=i;
            e.j=j;
            e.di=-1;
            push(s,e);
            mg[il][jl]=-1;
        }
        else
        {
            pop(s,e);
            mg[e.i][e.j]=0;
        }
        destroyst(s);
        return false;
        }        
}
int main() 
{    
    if(!mgpath(1,1,m,n))
        printf("无解"); 
    return 1;
}
运行结果及报错内容

img

我的解答思路和尝试过的方法

看了十几遍了,一直找不出错误的原因,求各位帮助帮助

我想要达到的结果
  • 写回答

3条回答 默认 最新

  • 关注

    1, stempty()检查空栈, 应该是空栈 return true;不是空栈 return false; 你true false写反了
    2, if(find) 中是 是e.i=il 和e.j=jl, 不是e.i=i e.j=j
    3, mgpath函数中最后的 destroyst(s); 和 return false; 要放在while外面

    你题目的解答代码如下:

    #include<stdio.h>
    #include<malloc.h>
    #define m 4
    #define n 4
    #define max 100
    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}
    };
    typedef struct
    {
        int i,j;//行,列
        int di ;
    } box;
    typedef struct
    {
        box data[max];
        int top;
    } st;
    void initst(st *& s)//初始化
    {
        s=(st*)malloc(sizeof(st));
        s->top=-1;
    }
    bool push(st *&s,box e)//进栈
    {
        if(s->top==max-1)
            return false;
        s->top++;
        s->data[s->top]=e;
        return true;
    }
    bool stempty(st *s)//检查空栈
    {
        if(s->top==-1)
            return true; //空栈 return true;
        return false;  //不是空栈 return false;
    }
    bool gettop(st *s,box &e)//取栈顶
    {
        if(s->top==-1)
            return false;
        e=s->data[s->top];
        return true;
    }
    bool pop(st *&s,box &e)//出栈
    {
        if(s->top==-1)
            return false;
        e=s->data[s->top];
        s->top--;
        return true;
    }
    void destroyst(st *&s)//销毁栈
    {
        free(s);
    }
    bool mgpath(int xi,int yi,int xe,int ye)
    {
        box path[max],e;
        int i,j,di,il,jl,k;
        bool find;
        st *s;
        initst(s);
        e.i=xi;e.j=yi;e.di=-1;
        mg[xi][yi]=-1;
        push(s,e);
        while(!stempty(s))
        {
            gettop(s,e);
            i=e.i;j=e.j,di=e.di;
            if(i==xe&&j==ye)
            {
                printf("迷宫的一条路径如下: \n");
                k=0;
                while(!stempty(s))
                {
                    pop(s,e);
                    path[k++]=e;
                }
                while(k>=1)
                {
                    k--;
                    printf("(%d,%d)",path[k].i,path[k].j);
                }
                printf("\n");
                destroyst(s);
                return true;
            }
            find=false;
            while(di<4&&!find)
            {
                di++;
                switch(di)
                {
                    case 0:il=i-1;jl=j;break;
                    case 1:il=i;jl=j+1;break;
                    case 2:il=i+1;jl=j;break;
                    case 3:il=i;jl=j-1;break;
                }
                if(mg[il][jl]==0)
                    find=true;
            }
            if(find)
            {
                s->data[s->top].di=di;
                e.i=il;   // 是e.i=il 不是e.i=i
                e.j=jl;   // 是e.j=jl 不是e.j=j
                e.di=-1;
                push(s,e);
                mg[il][jl]=-1;
            }
            else
            {
                pop(s,e);
                mg[e.i][e.j]=0;
            }
        }
        //放在while外面
        destroyst(s);
        return false;
    }
    int main()
    {
        if(!mgpath(1,1,m,n))
            printf("无解");
        return 1;
    }
    

    img

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月15日
  • 已采纳回答 4月7日
  • 创建了问题 4月6日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装