STL栈求解迷宫问题

用栈求解迷宫问题时,运用其先进后出的性质。

思路:

1.先将入口点入栈

2.while循环,当栈为空,或者栈的第一个点为出口点时退出

3.top获得栈首使其为当前位置,分别判断能否进入四个方向点(未走过),若可以则将新位置压栈,并记录已走过;否则当前位置出栈,重复(3)过程

4.while循环,当栈不为空,输出栈中位置

#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <ctime>
#include<stack>
using namespace std;
typedef struct wz
{
    int x;
    int y;
}wz;
stack<wz> st;
int mg2[20][20]={0};//记录该路径是否走过,防止进入死循环
int mg[100][100]=
{
    {
        0,0,0,0,0,0,0,0,0,0
    },
    {
        0,1,1,0,1,1,1,0,1,0
    },
    {
        0,1,1,0,1,1,1,0,1,0
    },
    {
        0,1,1,1,1,0,0,1,1,0
    },
    {
        0,1,0,0,0,1,1,1,1,0
    },
    {
        0,1,1,1,0,1,1,1,1,0
    },
 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值