酸辣小太阳. 2022-09-29 20:18 采纳率: 71.4%
浏览 35

C语言数据结构栈迷宫问题

我用的是类,问一下这是哪里出了问题

#include<stdio.h>
#include<stdlib.h>
#define OVERFLOW -1
#define SUCCESS 1
typedef struct{
    int x;
    int y;
}Pos;
class Stack{
    typedef struct{
        Pos data;
        struct SNode*next;
    }SNode,*PSNode;
    PSNode head;
    int length;
public:
    Stack(){
        head=NULL;
        length=0;
    }
    int Push(Pos data){
        PSNode p=new SNode;
        if(!p)return OVERFLOW;
        p->data=data;
        p->next=head;
        head=p;
        length++;
        return SUCCESS;
    }
    int  GetTop(Pos&data)    
    {
        if(!head)return OVERFLOW;
        data=head->data;
        return SUCCESS;
    }
    int Pop(Pos&data){
        if(!head)return OVERFLOW;
        PSNode p;
        p=head;
        data=head->data;
        head=p->next;
        delete p;
        length--;
        return SUCCESS;
    }
    bool StackEmpty()
    {
        if(length)return false;
        return true;
    }
    void clear()
    {
        PSNode p=head;
        while(p)
        {
            head=p->next;
            delete p;
            p=head;
        }
    }
    ~Stack()
    {
        clear();
    }
};
int map[10][10]={
    {8,8,8,8,8,8,8,8,8,8},
    {8,0,0,8,0,0,0,8,0,8},
    {8,0,0,8,0,0,0,8,0,8},
    {8,0,0,0,0,8,8,0,0,8},
    {8,0,8,8,8,0,0,0,0,8},
    {8,0,0,0,8,0,0,0,0,8},
    {8,0,8,0,0,0,8,0,0,8},
    {8,0,8,8,8,0,8,8,8,8},
    {8,8,0,0,0,0,0,8,0,8},
    {8,8,8,8,8,8,8,8,8,8}
    }; 
bool GetNext(Pos pos,Pos&nextpos)
{
    if(map[pos.x][pos.y]==0&&map[pos.x+1][pos.y]==0)//判断南边是否有路
    {
        nextpos.x=pos.x+1;nextpos.y=pos.y;map[pos.x][pos.y]=1;return true;
     } 
     if(map[pos.x][pos.y]<=1&&map[pos.x][pos.y+1]==0)//判断东边是否有路 
     {
         nextpos.x=pos.x;nextpos.y=pos.y+1;map[pos.x][pos.y]=2;return true;
     }
     if(map[pos.x][pos.y]<=2&&map[pos.x][pos.y-1]==0)//判断西边是否有路
     {
         nextpos.x=pos.x;nextpos.y=pos.y-1;map[pos.x][pos.y]=3;return true;
      } 
      if(map[pos.x][pos.y]<=3&&map[pos.x-1][pos.y]==0)//判断北边是否有路
      {
          nextpos.x=pos.x-1;nextpos.y=pos.y;map[pos.x][pos.y]=4;return true;
       } 
       return false;
}
int main()
{
    Stack s;
    Pos startpos,curpos,endpos,nextpos;
    startpos.x=1;startpos.y=1;
    endpos.x=8;endpos.y=8;
    s.Push(startpos);
    printf("迷宫:\n");
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<10;j++)
        {
            if(j==9)printf("\n");
            printf("%d",map[i][j]);
        }
    }
    printf("起点坐标:(%d,%d)\n",startpos.x,startpos.y);
    printf("终点坐标:(%d,%d)\n",endpos.x,endpos.y);
    curpos.x=startpos.x;curpos.y=startpos.y;
    bool found=false; 
    while(s.GetTop(curpos)&&!found)
    {
        if(curpos.x==endpos.x&&curpos.y==endpos.y){
            found=true;break;
        }
        if(GetNext(curpos,nextpos))//如果能找到下一步 
        {
           s.Push(nextpos);
        }
        else {
          if(s.StackEmpty())break;//栈是否为空
          s.Pop(curpos); 
        }
    }
    if(found)
    {
        printf("路径逆序为:\n");
        while(!s.StackEmpty())
        {
            s.Pop(curpos);//将栈中元素逆序出栈
            printf("(%d,%d)",curpos.x,curpos.y); 
        }
    }
    else printf("找不到出口\n");
}
 

编译结果总是显示

img

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-30 04:42
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

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

报告相同问题?

问题事件

  • 创建了问题 9月29日

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号