栈实现迷宫路径

#include 
#include 
#include 

#define END 13
#define START 1
using namespace std;
/*
 * 栈回溯实现一条迷宫路径
 * 优点:非递归实现
 * 不足:1.只能找到一条路径
 *       2. 不是最短路径
 */
 
 //定义栈中存放的数据类型
typedef struct Mstacknode
{
    int x1;
    int y1;
    int direction;
}MSTACK;

//迷宫数组
//int maze[6][6] = {                    //水平向右为y正;竖直向下为X正
//	{2,2,2,2,2,2},
//	{2,0,0,0,2,2},
//	{2,0,2,0,0,2},
//	{2,0,0,2,0,2},
//	{2,2,0,0,0,2},
//	{2,2,2,2,2,2},
//	};
int maze[END+2][END+2] = {                    //水平向右为y正;竖直向下为X正
	{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
	{2,0,0,0,2,0,2,2,2,2,2,0,0,0,2},
	{2,0,2,0,2,0,0,0,0,0,0,0,2,0,2},
	{2,0,2,0,0,0,2,2,0,2,0,2,0,0,2},
	{2,2,2,0,2,2,0,0,0,2,0,2,2,0,2},
	{2,0,0,0,2,2,2,2,0,0,0,0,0,0,2},
	{2,2,0,2,2,0,0,0,2,2,2,0,2,2,2},
	{2,0,0,0,0,0,2,2,2,2,0,0,2,0,2},
	{2,2,2,0,2,2,2,0,2,0,0,2,2,0,2},
	{2,0,2,0,2,2,2,0,0,2,0,0,0,0,2},
	{2,0,0,0,2,2,0,2,2,2,2,0,2,0,2},
	{2,2,2,0,0,2,0,2,2,0,2,2,2,0,2},
	{2,0,2,2,0,0,0,0,2,0,2,0,0,0,2},
	{2,0,0,2,0,0,0,2,2,0,0,0,2,0,2},
	{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}
};

//声明函数
void MoveSmaze(int&,int&);
void Print();
stack <MSTACK> S; // 声明栈中的数据类型
MSTACK MS;

//main函数
int main()
{
    int mx,my;
    mx=START;
    my=START;
    Print();
    cout<<endl;
    MoveSmaze(mx,my);
    cout << "Hello world!" << endl;
    return 0;
}

//走迷宫主函数
void MoveSmaze(int&x,int&y)
{
    //起点
    MS.x1=x;
    MS.y1=y;
    MS.direction=0;
    S.push(MS);          //将0入栈
    while((x>0&&x<(END+1))&&(y>0&&y<(END+1)))
    {
        if(x==END&&y==END)
        {
            int Count=0;
            maze[x][y]=1;
            Print();
            while(!S.empty())                      //出栈
            {
                string dr;
                if(S.top().direction==-1)
                    dr="东";
                else if(S.top().direction==-2)
                    dr="南";
                else if(S.top().direction==-3)
                    dr="西";
                else if(S.top().direction==-4)
                    dr="北";
                else
                    dr="|";
                cout<<"X:"<<S.top().x1<<"\tY:"<<S.top().y1<<"\t方向:"<<dr<<endl;
                if(S.top().direction==0)
                    Count++;
                S.pop();
            }
            cout<<"走完需要"<<Count<<"步\n";
            return;
        }
        //先读取栈顶,不为0,则选方向
        int n;
        n=S.top().direction;
        maze[x][y]=1;
        MS.x1=x;
        MS.y1=y;
        if(maze[x][y+1]==0)
        {
            MS.direction=-1;   //东,-1
            S.push(MS);
        }
        if(maze[x+1][y]==0)
        {
            MS.direction=-2;   //南,-2
            S.push(MS);
        }
        if(maze[x][y-1]==0)
        {
            MS.direction=-3;   //西,-3
            S.push(MS);
        }
        if(maze[x-1][y]==0)
        {
            MS.direction=-4;   //北,-4
            S.push(MS);
        }
        n=S.top().direction;
        //四周方向都不通
        while(S.top().direction==0)            //,当前栈顶为0,则出栈两次,换方向走 直到栈顶不为0
        {
            for(int i=0;i<2&&S.empty()==false;i++)
            {
                maze[S.top().x1][S.top().y1]=3;
                S.pop();
                x=S.top().x1;
                y=S.top().y1;

            }
        }
        maze[S.top().x1][S.top().y1]=1;
        n=S.top().direction;
        switch(n)  //选择要走的方向
        {
        case -1:
            y++;
            break;
        case -2:
            x++;
            break;
        case -3:
            y--;
            break;
        case -4:
            x--;
            break;
        default:
            break;
        }
        MS.x1=x;
        MS.y1=y;
        MS.direction=0;
        S.push(MS);          //将0入栈

    }
}

//打印地图
void Print()
{
    for(int i=0;i<END+2;i++)
    {
        for(int j=0;j<END+2;j++)
        {
            if(maze[i][j]==2)
                cout<<"█";
            else if(maze[i][j]==1)
                cout<<"★";
            else if(maze[i][j]==3)
                cout<<"□";
            else
                cout<<"  ";
        }
        cout<<endl;
    }
}

总结

这是通过栈来实现迷宫路径的探索。由于时间仓促,其中代码逻辑还存在许多不足,比如:未能实现简单路径以及路径只有一条的问题,之后有时间我会继续完善。有不足之处,欢迎指正。

你可能感兴趣的:(栈实现迷宫路径)