经典数据结构之栈的应用-迷宫问题

本文介绍了一种使用栈数据结构解决迷宫寻路问题的方法。通过定义迷宫地图及状态标记,利用栈来记录走过的路径,实现了从起点到终点的有效路径搜索。此方法能够自动寻找从迷宫起点到终点的可行路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        栈是一种常用的数据结构,有着广泛的应用,例如走迷宫,八皇后问题,树的前序遍历,数学公式计算等等。以下是走迷宫问题代码

// Queen.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"
#define LEFT 0
#define RIGHT 1
#define UP 2
#define DOWN 3

#define VISITED 10 //如果走过,则标记为VISITED
#define UNVISITED 9//初始都为UNVISITED

//4个行进方向
typedef struct direction
{
    
int x;
    
int y;
}
DIR;

//坐标点定义
typedef struct pos
{
    
int pos_x;
    
int pos_y;
}
POSITION;

//路径
typedef struct Path
{
    POSITION point;
    
int dir;
}
PATH;

//stack
class Stack
{
private:
    
int top;
    POSITION status[
64];
    
int m_maxNode;
public:
    Stack()
    
{
        top
=-1;
        m_maxNode
=64;
        
for(int i=0;i<64;i++)
            status[i].pos_x
=status[i].pos_y=-1;
    }


    
int Push(POSITION point)
    
{
        
if(top>=m_maxNode-1)
            
return 1;//over flow!
        else
            status[
++top]=point;
        
return 0;
    }


    
int Pop(POSITION *out)
    
{
        
if(top<0)
            
return 1;
        
out->pos_x=status[top].pos_x;
        
out->pos_y=status[top--].pos_y;
        
return 0;
    }


    
int IsEmpty()
    
{
        
if(top==-1return 1;
        
else
            
return 0;
    }


}
;


class Queen
{
private:
    DIR dir[
4];
    
int m_MazeLength;
    
int Maze[9][9];
    
int m_mark[9][9];
    
int m_Path[20];
    
int m_index;
public:
    Queen()
    
{
        dir[LEFT].x
=-1;
        dir[LEFT].y
=0;

        dir[RIGHT].x
=1;
        dir[RIGHT].y
=0;

        dir[UP].x
=0;
        dir[UP].y
=-1;

        dir[DOWN].x
=0;
        dir[DOWN].y
=1;

        m_MazeLength
=8;

        
int tempMaze[9][9]={
                            
{1,1,1,1,1,1,1,1,1},
                            
{1,0,0,0,0,0,0,0,1},
                            
{1,1,1,1,1,1,1,0,1},
                            
{1,0,0,0,0,0,0,0,1},
                            
{1,0,1,1,1,1,1,1,1},
                            
{1,0,0,0,0,1,0,0,1},
                            
{1,0,1,0,0,0,0,1,1},
                            
{1,0,0,0,1,0,0,0,1},
                            
{1,1,1,1,1,1,1,1,1}
                            }
;
    
    
for(int i=0;i<9;i++)
        
for(int j=0;j<9;j++)
        
{
            Maze[i][j]
=tempMaze[i][j];
            m_mark[i][j]
=UNVISITED;
        }

    }



    
int GetPath();

    POSITION GetPosition(POSITION current,
int dire)
    
{
        POSITION temp;
        temp.pos_x
=current.pos_x + dir[dire].x;
        temp.pos_y
=current.pos_y+ dir[dire].y;
        
return temp;

    }

}
;


int Queen::GetPath()
{

    Stack mazeStack;
    POSITION pos;
    pos.pos_x
=1;
    pos.pos_y
=1;
    mazeStack.Push(pos);
    m_mark[pos.pos_y][pos.pos_x]
=VISITED;
    
//迷宫栈不为空,则一直继续
    while(!mazeStack.IsEmpty())
    
{
        POSITION currentpos;
        mazeStack.Pop(
&currentpos);

        
if(currentpos.pos_x==7 &&currentpos.pos_y==7)
        
{
        
            
return 0;
        }

            
//向上通路
        POSITION pos_Up,pos_Down,pos_Left,pos_Right;
        pos_Up
=GetPosition(currentpos,UP);
        pos_Down
=GetPosition(currentpos,DOWN);
        pos_Left
=GetPosition(currentpos,LEFT);
        pos_Right
=GetPosition(currentpos,RIGHT);
        
        
//up direction
        if(Maze[pos_Up.pos_y][pos_Up.pos_x]==0 && m_mark[pos_Up.pos_y][pos_Up.pos_x]==UNVISITED)
        
{
            mazeStack.Push(pos_Up);
            m_mark[pos_Up.pos_y][pos_Up.pos_x]
=VISITED;
        }


        
//left direction
        if(Maze[pos_Left.pos_y][pos_Left.pos_x]==0 && m_mark[pos_Left.pos_y][pos_Left.pos_x]==UNVISITED)
        
{
            mazeStack.Push(pos_Left);
            m_mark[pos_Left.pos_y][pos_Left.pos_x]
=VISITED;
        }


        
//down direction
        if(Maze[pos_Down.pos_y][pos_Down.pos_x]==0 && m_mark[pos_Down.pos_y][pos_Down.pos_x]==UNVISITED)
        
{
            mazeStack.Push(pos_Down);
            m_mark[pos_Down.pos_y][pos_Down.pos_x]
=VISITED;
        }


        
//right direction
        if(Maze[pos_Right.pos_y][pos_Right.pos_x]==0 && m_mark[pos_Right.pos_y][pos_Right.pos_x]==UNVISITED)
        
{
            mazeStack.Push(pos_Right);
            m_mark[pos_Right.pos_y][pos_Right.pos_x]
=VISITED;
        }

        
    }


    
return 1;
}




int main(int argc, char* argv[])
{
    Queen q;
    
if(!q.GetPath())
        printf(
"you have Walked out from the maze successfully\n");
    
else
        printf(
"No Path to walk out from the maze\n");
    
return 0;
}


转载于:https://www.cnblogs.com/Winston/archive/2008/05/31/1211207.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值