用栈求解迷宫问题时,运用其先进后出的性质。
思路:
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
},