转自未空blog
//我刚开始对STACK的内存分配那有点问题,后来用这个代码调试了下,感觉有点明白了, 地址由高到低分配,然后代码中的base和top刚开始指向地址最低的地方,内存不够时重新在原有基础上添加内存,top指向原有的栈顶,然后继续存入数据
/*
#include<stdio.h>
#include<iostream>
#include<limits.h>
#include<stdlib.h>
#include<iomanip>
#define STACK_INIT_SIZE 1
#define STACKINCREMENT 10
#define OK 1
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define ERROR 0
#define MAXLENGTH 25;
using namespace std;
typedef int Status;
typedef int MazeType[25][25];
typedef int ElemType;
struct PosType{
int x;
int y;
};
struct SElemType{ //这个是栈的元素
int ord; //通道块的序号
PosType seat; //通道块在迷宫的坐标位置
int direc; //方向
};
PosType curpos; //定义当前位置
PosType direction[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; //定义四个方向
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &L) //构建一个栈
{
L.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); //动态分配内存
if (!L.base) exit(OVERFLOW);
L.top = L.base;
cout << endl << L.top << " " << L.base << endl << endl;
L.stacksize = STACK_INIT_SIZE; //初始化长度为0
return OK;
}
Status Push(SqStack &L, SElemType &e) //栈中插入元素e
{
if(L.top - L.base >= L.stacksize)
{
cout << "先前" << L.base << "&" << L.top << endl;
L.base = (SElemType*)realloc(L.base, (L.stacksize+1)* sizeof(SElemType));
cout <<"扩大内存" << L.base << endl;
cout << L.stacksize << endl;
L.top = L.base + L.stacksize;
cout << " 22222222"<< L.top << endl;
L.stacksize += 1;
}
*L.top++ = e;
//L.top++;
// L.stacksize++;
return OK;
}
Status Pop(SqStack &L, SElemType &e)
{
if (L.top == L.base) return ERROR;
e = *--L.top;
// cout << L.top << "测试" << endl;
// L.stacksize--;
return OK;
}
Status IsStackEmpty(SqStack S)
{
if (S.top == S.base) return true;
else return false;
}
MazeType maze;
int i, j, m, n; //m,n表示行数列数
void PrintMaze() //输出迷宫
{
int i, j;
for (i = 0; i < m; i++){
for (j = 0; j < n; j++)
cout << setw(3) << maze[i][j];
cout << endl;
}
}
PosType Start, End; //入口和出口分别用begin 和and 表示
Status InitMaze() //设置迷宫
{
cin >> m >> n; //迷宫m行n列
for (i = 0; i < m; i++)
for (j = 0; j < n; j++){
maze[i][j] = 1; //通道用1表示
}
for (i = 0; i < n; i++){
maze[0][i] = 0; //迷宫0表示墙
maze[m - 1][i] = 0; //第一行加最后一行全部0
}
for (j = 0; j < m; j++){ //第一列最后一列全部0
maze[j][0] = 0;
maze[j][n - 1] = 0;
}
cout << "迷宫的初始结构如下: " << endl;
PrintMaze();
cout << "请输入迷宫的墙数:" << endl;
int num;
int x1, y1;
cin >> num;
cout << "请输入迷宫的墙的坐标:" << endl;
for (i = 0; i < num; i++){
cin >> x1 >> y1;
maze[x1][y1] = 0;
}
cout << "请输入入口的坐标:" << endl;
cin >> Start.x >> Start.y;
cout << "请输入出口的坐标: " << endl;
cin >> End.x >> End.y;
PrintMaze();
return 0;
}
int curstep = 1; //走到第几个格子了,初值在入口处为一
Status CanPass(PosType b)
{
cout << maze[b.x][b.y] << endl;
if (maze[b.x][b.y] == 1)
return OK;
else return ERROR;
}
void FootPrint(PosType b) //把某个点b变成足迹
{
maze[b.x][b.y] = curstep; //走过以后做个标记-1
}
Status NextPos(PosType &e, int direc)
{
curpos.x += direction[direc].x;
curpos.y += direction[direc].y;
return OK;
}
Status MarkPrint(PosType b)
{
maze[b.x][b.y] = -1;
return OK;
}
Status MazePath(PosType start, PosType end)
{
curpos = start; //初始化位置
SqStack S;
InitStack(S);
cout << S.base << " " << S.stacksize << " " << S.top << endl;
SElemType e;
do{
if (CanPass(curpos)){ //如果当前位置能通过
FootPrint(curpos); //那就在这个点留下足迹
e.ord = curstep; //栈元素e的编号为curstep
e.seat = curpos; //通道块在迷宫的坐标位置为当前位置
e.direc = 0; //然后把方向初始化为向东
Push(S, e);
cout << "Push" <<S.top << "测试" << endl;//满足要求e入栈
curstep++;
cout << curstep << " " << curpos.x << "," << curpos.y << endl;
if (curpos.x == end.x&&curpos.y == end.y)
return TRUE;
NextPos(curpos, e.direc);
}
else{ //当前位置不能通过
if (!IsStackEmpty(S)){
Pop(S, e);
curstep--;
while (e.direc == 3 && !IsStackEmpty(S)) //前一个位置在最后一个方向且不是空栈
{
MarkPrint(e.seat);
Pop(S, e);
cout << "POP" << S.top << "测试" << endl;
curstep--;
}
if (e.direc < 3){
e.direc++;
Push(S, e);
curstep++;
curpos = e.seat;
NextPos(curpos, e.direc);
}
}
}
} while (!IsStackEmpty(S));
return false;
}
int main()
{
InitMaze();
if (MazePath(Start, End)){
cout << "迷宫的一条路径如下:" << endl;
PrintMaze();
}
else cout << "Error!" << endl;
return 0;
}*/
这个当时有个小细节有错.....改了挺长时间的
#include<stdio.h>
#include<iostream>
#include<limits.h>
#include<stdlib.h>
#include<iomanip>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define ERROR 0
#define MAXLENGTH 25;
using namespace std;
typedef int Status;
typedef int MazeType[25][25];
typedef int ElemType;
struct PosType{
int x;
int y;
};
struct SElemType{ //这个是栈的元素
int ord; //通道块的序号
PosType seat; //通道块在迷宫的坐标位置
int direc; //方向
};
PosType curpos; //定义当前位置
PosType direction[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; //定义四个方向
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &L) //构建一个栈
{
L.base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); //动态分配内存
if (!L.base) exit(OVERFLOW);
L.top = L.base;
L.stacksize = 0; //初始化长度为0
return OK;
}
Status Push(SqStack &L, SElemType &e) //栈中插入元素e
{
*L.top++ = e;
//L.top++;
L.stacksize++;
return OK;
}
Status Pop(SqStack &L, SElemType &e)
{
if (L.top == L.base) return ERROR;
e = *--L.top;
L.stacksize--;
return OK;
}
Status IsStackEmpty(SqStack S)
{
if (S.top == S.base) return true;
else return false;
}
MazeType maze;
int i, j, m, n; //m,n表示行数列数
void PrintMaze() //输出迷宫
{
int i, j;
for (i = 0; i < m; i++){
for (j = 0; j < n; j++)
cout << setw(3) << maze[i][j];
cout << endl;
}
}
PosType Start, End; //入口和出口分别用begin 和and 表示
Status InitMaze() //设置迷宫
{
cin >> m >> n; //迷宫m行n列
for (i = 0; i < m; i++)
for (j = 0; j < n; j++){
maze[i][j] = 1; //通道用1表示
}
for (i = 0; i < n; i++){
maze[0][i] = 0; //迷宫0表示墙
maze[m - 1][i] = 0; //第一行加最后一行全部0
}
for (j = 0; j < m; j++){ //第一列最后一列全部0
maze[j][0] = 0;
maze[j][n - 1] = 0;
}
cout << "迷宫的初始结构如下: " << endl;
PrintMaze();
cout << "请输入迷宫的墙数:" << endl;
int num;
int x1, y1;
cin >> num;
cout << "请输入迷宫的墙的坐标:" << endl;
for (i = 0; i < num; i++){
cin >> x1 >> y1;
maze[x1][y1] = 0;
}
cout << "请输入入口的坐标:" << endl;
cin >> Start.x >> Start.y;
cout << "请输入出口的坐标: " << endl;
cin >> End.x >> End.y;
PrintMaze();
return 0;
}
int curstep = 1; //走到第几个格子了,初值在入口处为一
Status CanPass(PosType b)
{
cout << maze[b.x][b.y] << endl;
if (maze[b.x][b.y] == 1)
return OK;
else return ERROR;
}
void FootPrint(PosType b) //把某个点b变成足迹
{
maze[b.x][b.y] = curstep; //走过以后做个标记-1
}
Status NextPos(PosType &e, int direc)
{
curpos.x += direction[direc].x;
curpos.y += direction[direc].y;
return OK;
}
Status MarkPrint(PosType b)
{
maze[b.x][b.y] = -1;
return OK;
}
Status MazePath(PosType start, PosType end)
{
curpos = start; //初始化位置
SqStack S;
InitStack(S);
cout << S.base << " " << S.stacksize << " " << S.top << endl;
SElemType e;
do{
if (CanPass(curpos)){ //如果当前位置能通过
FootPrint(curpos); //那就在这个点留下足迹
e.ord = curstep; //栈元素e的编号为curstep
e.seat = curpos; //通道块在迷宫的坐标位置为当前位置
e.direc = 0; //然后把方向初始化为向东
Push(S, e); //满足要求e入栈
curstep++;
cout << curstep << " " << curpos.x << "," << curpos.y << endl;
if (curpos.x == end.x&&curpos.y == end.y)
return TRUE;
NextPos(curpos, e.direc);
}
else{ //当前位置不能通过
if (!IsStackEmpty(S)){
Pop(S, e);
curstep--;
while (e.direc == 3 && !IsStackEmpty(S)) //前一个位置在最后一个方向且不是空栈
{
MarkPrint(e.seat);
Pop(S, e);
curstep--;
}
if (e.direc < 3){
e.direc++;
Push(S, e);
curstep++;
curpos = e.seat;
NextPos(curpos, e.direc);
}
}
}
} while (!IsStackEmpty(S));
return false;
}
int main()
{
InitMaze();
if (MazePath(Start, End)){
cout << "迷宫的一条路径如下:" << endl;
PrintMaze();
}
return 0;
}
本文介绍了一种使用栈数据结构解决迷宫问题的方法,详细讲解了如何利用栈进行递归搜索,找到从迷宫入口到出口的路径。通过动态内存分配和栈操作,实现了对迷宫路径的有效探索。
3万+

被折叠的 条评论
为什么被折叠?



