C++超迷你迷宫

C++超迷你迷宫,第1张

概述C++超迷你迷宫

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#include <iostream>#include <stack>using namespace std;#define _SIZE_ 10typedef int Array[_SIZE_][_SIZE_];struct Pos{    int x;    int y;    Pos(){}    Pos(int val1,int val2):x(val1),y(val2){}};//记录位置x,y,相当于记录的相应位置坐标。struct Man{    Pos pos;};class Maze{    public:Maze(int a[][_SIZE_])    {        for(int i=0;i<_SIZE_;i++)        {            for(int j=0;j<_SIZE_;j++)            {                   arr[i][j] = a[i][j];            }        }  }voID Printf()    {        for(int i=0;i<_SIZE_;i++)        {            for(int j=0;j<_SIZE_;j++)            {                cout<<arr[i][j]<<" ";            }            cout<<endl;        }    }    voID InitGrial(const Pos &start,const Pos &end)    {        Man man;        man.pos.x = start.x;        man.pos.y = start.y;    //  man.kind = RIGHT;        stack<Man> st;        stack<Man> sh;        st.push(man);//此处才把起始位置放进栈st里面。        Postion(man);//记住man走过的足迹.        while(!st.empty())        {            Pos NewPos;            Man man = st.top();            if (InitNewPos(NewPos,man))            {                               Man NewMan;            NewMan.pos = NewPos;            st.push(NewMan);            Postion(NewMan);            if(NewMan.pos.x==end.x && NewMan.pos.y==end.y)break;            }            else            {                sh.push(st.top());                st.pop();            }        }           while(!sh.empty())        {            Man man = sh.top();            arr[man.pos.x][man.pos.y]=0;            sh.pop();        }    }    bool InitNewPos(Pos &pos,Man &man)    {        if(arr[man.pos.x][man.pos.y+1]==0)             {                pos.x=man.pos.x;                pos.y=man.pos.y+1;                return true;             }        if(arr[man.pos.x+1][man.pos.y]==0)            {            pos.x=man.pos.x+1;            pos.y=man.pos.y;            return true;            }        if(arr[man.pos.x][man.pos.y-1]==0)            {            pos.y = man.pos.y-1;            pos.x = man.pos.x;            return true;            }        if(arr[man.pos.x-1][man.pos.y]==0)            {            pos.x = man.pos.x-1;            pos.y = man.pos.y;            return true;                    }        return false;    }    voID Postion(Man &man)    {        arr[man.pos.x][man.pos.y]=2;    }    private:    Array arr;};int main(){    int a[][10]=            {1,1,1};            Maze ma(a);    Pos start(0,1);    Pos end(9,7);//定义开始的位置以及结束的位置。    ma.InitGrial(start,end); //然我们开始吧,实在想不出名字了,Grial是圣杯的意思.    ma.Printf();        return 0;}

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的C++超迷你迷宫全部内容,希望文章能够帮你解决C++超迷你迷宫所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1232055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存