栈-迷宫求解
魔改栈
#include "stdio.h"
#include "stdlib.h"
typedef int DirectiveType;
#define ElemType int
#define Status int
#define OK 1
#define ERROE 0
#define RANGE 100
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define RANGE 100
#define ROW 10
#define COL 10
typedef struct {
int m, n;
int arr[RANGE][RANGE];
}MazeType;
typedef struct{
int row;
int col;
}PosType;
typedef struct{
int step;
PosType seat;
DirectiveType di;
}SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}MStack;
Status Init_SqStack(MStack *S){
S->base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push_SqStack(MStack *S,SElemType e){
if(S->top-S->base >= S->stacksize){
S->base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*(S->top++)=e;
return OK;
}
SElemType GetTop_Stack(MStack *S){
SElemType p;
if(S->top==S->base){
printf("栈空\n");
return;
}
p=*(S->top-1);
return p;
}
SElemType Pop_Stack(MStack *S){
if(S->top==S->base){
printf("栈空\n");
return;
}
return *(--S->top);
}
int Length_Stack(MStack *S){
return S->top-S->base;
}
Status Empty_Stack(MStack *S){
if(S->top == S->base)
return OK;
else
return ERROE;
}
迷宫运算
#include "Stack_Maze.h"
Status InitMaze(MazeType *maze, int a[ROW][COL], int row, int col)
{
int i, j;
for (i = 1; i<row - 1;i++)
{
for (j = 1; j<col - 1; j++)
{
maze->arr[i][j] = a[i][j];
}
}
for (j = 0; j<row; j++)
maze->arr[0][j] = maze->arr[row-1][j] = 1;
for (i = 0; i<col; i++)
maze->arr[i][0] =maze->arr[i][col - 1] = 1;
return OK;
}
Status Pass(MazeType *maze, PosType curpos){
if (maze->arr[curpos.row][curpos.col]== 0)
return OK;
else
return ERROE;
}
Status FootPrint(MazeType *maze, PosType curpos){
maze->arr[curpos.row][curpos.col]= 6;
return OK;
}
Status MarkPrint(MazeType *maze, PosType curpos){
maze->arr[curpos.row][curpos.col]= 4;
return OK;
}
Status PosEqual(PosType pos1, PosType pos2){
if (pos1.row == pos2.row && pos1.col == pos2.col)
return OK;
else
return ERROE;
}
PosType NextPos(PosType curpos, DirectiveType di) {
PosType pos = curpos;
switch (di){
case 1:pos.col++;break;
case 2:pos.row++;break;
case 3:pos.col--;break;
case 4:pos.row--;break;
}
return pos;
}
SElemType CreateSElem(int step, PosType pos, DirectiveType di){
SElemType e;
e.step = step;
e.seat = pos;
e.di = di;
return e;
}
Status MazePath(MazeType *maze, PosType start, PosType end){
MStack *s,S;
SElemType e;
PosType curpos = start;
int curstep = 1;
s=&S;
Init_SqStack(s);
do {
if (Pass(maze, curpos))
{
FootPrint(maze,curpos);
e = CreateSElem(curstep,curpos, 1);
Push_SqStack(s, e);
if (PosEqual(curpos,end))
return OK;
curpos = NextPos(curpos,1);
curstep++;
}
else
{
if (!Empty_Stack(s))
{
e=Pop_Stack(s);
while (e.di == 4&& !Empty_Stack(s))
{
MarkPrint(maze,e.seat);
e=Pop_Stack(s);
}
if (e.di<4)
{
e.di++;
Push_SqStack(s, e);
curpos =NextPos(e.seat, e.di);
}
}
}
} while (!Empty_Stack(s));
return ERROE;
}
测试
int Text_Maze_Use2(){
int i, j;
PosType start, end;
MazeType MAZE,*maze;
int a[ROW][COL] = {
{ 1,1,1,1,1,1,1,1,1,1 },
{ 1,0,0,1,0,0,0,1,0,1 },
{ 1,0,0,1,0,0,0,1,0,1 },
{ 1,0,0,0,0,1,1,0,0,1 },
{ 1,0,1,1,1,0,0,0,0,1 },
{ 1,0,0,0,1,0,0,0,0,1 },
{ 1,0,1,0,0,0,1,0,0,1 },
{ 1,0,1,1,1,0,1,1,0,1 },
{ 1,1,0,0,0,0,0,0,0,1 },
{ 1,1,1,1,1,1,1,1,1,1 }
};
maze=&MAZE;
printf("\n-------------------------------------------------\n");
printf("\n原始迷宫如下:\n");
printf("(其中'1'表示墙,'0'表示通道)\n");
for (i = 0; i<10; i++)
{
for (j = 0; j<10; j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
InitMaze(maze, a, ROW, COL);
start.row = 1;
start.col = 1;
end.row = 8;
end.col = 8;
if (MazePath(maze, start,end))
{
printf("\n-------------------------------------------------\n");
printf("\n解迷宫路径如下:\n");
printf("(其中'6'表示求解路径,'4'表示死胡同)\n");
for (i = 0; i<10; i++)
{
for (j = 0; j<10; j++){
printf("%d ",maze->arr[i][j]);
}
printf("\n");
}
}
else
printf("\n---------从入口到出口没有通路!-----\n");
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7dfd4454380a9098c76b198013da7704.png)