YXTS122 2016-06-14 23:36 采纳率: 100%
浏览 1051
已采纳

为什么输出路径前面没显示出来?

 #include<iostream>
using namespace std;
#include<iomanip>
class Position//位置类(栈数据元素类)
{
public:
    int x;//存X坐标
    int y;//存Y坐标
};

class Stacknode//栈结点类
{
public:
    Position data;
    Stacknode *next;
};

class Stack//栈类
{
public:
Stacknode *top;//栈顶指针
    Stacknode *base;//栈底指针 
    unsigned height;//栈高
    Stack()//无参构造函数
    {
        top=NULL;
        height=0;
        base=NULL;
    }
   ~Stack()//析构函数
    {
        Clear();
    }
  void Clear()
    {
        Position po;
        Pop(po);
    }//清空当前栈中元素
    void Push(Position 
    &);//进栈函数
    void Pop(Position &);//出栈函数
};

void Stack::Push(Position &po)
{
    Stacknode *p;
    if(top)//若栈非空
    {
        p=new Stacknode;
        p->data=po;
        p->next=top;//将结点插入链式栈前端,成为栈顶元素
        top=p; //修改栈顶指针
    }
    else//若为空栈
    {
        top=new Stacknode; //为栈顶元素分配内存
        //将x赋给栈顶数据元素
        top->data=po;
        top->next=NULL;
    }
    height++;
}

 void Stack::Pop(Position &po)
{
    Stacknode *p;
    if(height)//若栈中有元素
        {
            po=top->data;
            cout<<"("<<po.x<<","<<po.y<<") ";
            p=top;//将栈顶指针赋给p
            top=top->next;//修改栈顶指针,下移一个位置
            delete p;//删除原栈顶结点
            height--;//栈高减1
        }
    else
         return;
}

class Maze
{
public:
    int m,n,maze[8][8];
    void Getnum()//获取迷宫的行数和列数
    {
        m=8;
        n=8;
    }
    void Getmaze();//构造和输入迷宫,为迷宫四周加上围栏
    void Simplemaze();//简化迷宫
    void Putmaze();//输出迷宫
    void compare(Stack &s,int i,int j);
};

void Maze::Getmaze()//构造和输入迷宫,为迷宫四周加上围栏
{
    int i,j;
        for (j=0;j<8;j++)
        {
            maze[0][j]=1;
        }
        for (j=0;j<8;j++)
        {
            maze[7][j]=1;
        }
        for (i=0;i<8;i++)
        {
            maze[i][0]=1;
        }
        for (i=0;i<8;i++)
        {
            maze[i][7]=1;
        }

cout<<"请输入迷宫的形状:"<<endl; 
for(i=1;i<7;i++)
        {
            for(j=1;j<7;j++)
            {
                cin>>maze[i][j];
            }
        }
}

void Maze::Simplemaze()//简化迷宫
{
        for (int t=1;t<7;t++)
        {
            for (int r=1;r<7;r++)
            {
                if (maze[t-1][r]+maze[t+1][r]>=3)
                   maze[t][r]=1;
            }
        }
       maze[1][1]=0;
   maze[6][7]=4;
}

void Maze::Putmaze()//输出迷宫
{
    for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
               cout<<setw(5)<<maze[i][j];
            }
            cout<<endl;
        }

}

void Maze::compare(Stack &s,int i,int j)
{
int p=0,q=0,d;
Position point;
if(maze[i][j]==4)
{
cout<<"恭喜,该迷宫可以走出!"<<"具体步骤如下"<<endl;
/*
while(s.base!=s.top)    //输出栈中的所有元素
{
    p=s.base->data.x;
    q=s.base->data.y;
    s.base++;
cout<<"("<<p<<","<<q<<")"<<"    ";
}
*/
s.Pop(point);
return ;
}
if(maze[i][j]==2||maze[i][j]==1)
{
    cout<<"迷宫不能走出\n";
    return;
}
if(maze[i][j]==0)
{
maze[i][j]=2;
system("cls");
Putmaze();
point.x=i;
point.y=j;
s.Push(point);
cout<<"请输入方向:1表示向右,2表示向下,3表示向左,4表示向上:";
cin>>d;
switch (d)
{
case 2:
    compare(s,i+1,j);  break;
case 1:
    compare(s,i,j+1);break;
case 4:
    compare(s,i-1,j);break;
case 3:
   compare(s,i,j-1);break;
}
return;
}
}



int  main()
{
    Stack s;

    Maze MAZE;
    int m,n;
    MAZE.Getnum();
    m=MAZE.m;
    n=MAZE.n;
    MAZE.Getmaze();
    MAZE.Simplemaze();
    MAZE.Putmaze();
    MAZE.compare(s,1,1);
    return 0;
}

图片说明
图片说明

  • 写回答

2条回答 默认 最新

  • YXTS122 2016-06-18 22:46
    关注
      #include<iostream>
    using namespace std;
    #include<iomanip>
    class Position//位置类(栈数据元素类)
    {
    public:
        int x;//存X坐标
        int y;//存Y坐标
    };
    
    class Stacknode//栈结点类
    {
    public:
        Position data;
        Stacknode *next;
    };
    
    class Stack//栈类
    {
    public:
    Stacknode *top;//栈顶指针
        Stacknode *base;//栈底指针 
        unsigned height;//栈高
        Stack()//无参构造函数
        {
            top=NULL;
            height=0;
            base=NULL;
        }
       ~Stack()//析构函数
        {
            Clear();
        }
      void Clear()
        {
            Position po;
            while(Pop(po));
        }//清空当前栈中元素
        void Push(Position 
        &);//进栈函数
        int Pop(Position &);//出栈函数
    };
    
    void Stack::Push(Position &po)
    {
        Stacknode *p;
        p=new Stacknode;
        p->data=po;
        p->next=NULL;
        if(top)//若栈非空
        {
           top->next=p;
            top=p; //修改栈顶指针
        }
        else//若为空栈
        {
            base=top=p;
        }
        height++;
    }
    
     int Stack::Pop(Position &po)
    {
        Stacknode *p;
        if(height)//若栈中有元素
        {
            po=base->data;
            cout<<"("<<po.x<<","<<po.y<<") ";
            p=base;
            base=base->next;//修改栈底指针,下移一个位置
            delete p;//删除原栈底结点
            height--;//栈高减1
            return 1;
        }
        else
             return 0;
    }
    
    class Maze
    {
    public:
        int m,n,maze[8][8];
        void Getnum()//获取迷宫的行数和列数
        {
            m=8;
            n=8;
        }
        void Getmaze();//构造和输入迷宫,为迷宫四周加上围栏
        void Simplemaze();//简化迷宫
        void Putmaze();//输出迷宫
        void compare(Stack &s,int i,int j);
    };
    
    void Maze::Getmaze()//构造和输入迷宫,为迷宫四周加上围栏
    {
        int i,j;
            for (j=0;j<8;j++)
            {
                maze[0][j]=1;
            }
            for (j=0;j<8;j++)
            {
                maze[7][j]=1;
            }
            for (i=0;i<8;i++)
            {
                maze[i][0]=1;
            }
            for (i=0;i<8;i++)
            {
                maze[i][7]=1;
            }
    
    cout<<"请输入迷宫的形状:"<<endl; 
    for(i=1;i<7;i++)
            {
                for(j=1;j<7;j++)
                {
                    cin>>maze[i][j];
                }
            }
    }
    
    void Maze::Simplemaze()//简化迷宫
    {
            for (int t=1;t<7;t++)
            {
                for (int r=1;r<7;r++)
                {
                    if (maze[t-1][r]+maze[t+1][r]>=3)
                       maze[t][r]=1;
                }
            }
           maze[1][1]=0;
       maze[6][7]=4;
    }
    
    void Maze::Putmaze()//输出迷宫
    {
        for(int i=0;i<8;i++)
            {
                for(int j=0;j<8;j++)
                {
                   cout<<setw(5)<<maze[i][j];
                }
                cout<<endl;
            }
    
    }
    
    void Maze::compare(Stack &s,int i,int j)
    {
    int p=0,q=0,d;
    Position point;
    if(maze[i][j]==4)
    {
    cout<<"恭喜,该迷宫可以走出!"<<"具体步骤如下"<<endl;
    /*
    while(s.base!=s.top)    //输出栈中的所有元素
    {
        p=s.base->data.x;
        q=s.base->data.y;
        s.base=s.base->next;
    cout<<"("<<p<<","<<q<<")"<<"    ";
    }
    */
    while(s.Pop(point));
    return ;
    }
    if(maze[i][j]==2||maze[i][j]==1)
    {
        cout<<"迷宫不能走出\n";
        return;
    }
    if(maze[i][j]==0)
    {
    maze[i][j]=2;
    system("cls");
    Putmaze();
    point.x=i;
    point.y=j;
    s.Push(point);
    cout<<"请输入方向:1表示向右,2表示向下,3表示向左,4表示向上:";
    cin>>d;
    switch (d)
    {
    case 2:
        compare(s,i+1,j);  break;
    case 1:
        compare(s,i,j+1);break;
    case 4:
        compare(s,i-1,j);break;
    case 3:
       compare(s,i,j-1);break;
    }
    return;
    }
    }
    
    
    
    int  main()
    {
        Stack s;
    
        Maze MAZE;
        int m,n;
        MAZE.Getnum();
        m=MAZE.m;
        n=MAZE.n;
        MAZE.Getmaze();
        MAZE.Simplemaze();
        MAZE.Putmaze();
        MAZE.compare(s,1,1);
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示