加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++超迷你迷宫

发布时间:2020-12-16 07:43:43 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #include iostream#include stackusing namespace std;#define _SIZE_ 10typedef int Array[_SIZE_][_SIZE_];struct Pos{ int x; int y; Pos(){} Pos(

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#include <iostream>
#include <stack>
using namespace std;
#define _SIZE_ 10
typedef 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;
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读