迷宫问题中需要调用构建栈的的函数先赋一些构建栈代码
栈头文件Stack.h
#pragma once
#include<stdio.h>
#include<windows.h>
#include<assert.h>
#include<string.h>
typedef struct Pos//定义结构体坐标
{
int _row;
int _col;
}Pos;
typedef Pos DataType;
typedef struct Stack
{
DataType* _array;
size_t _top;//栈顶
size_t _base;//栈底
}Stack;
// 栈的实现接口
void StackInit(Stack* s);//栈的初始化
void StackPush(Stack* s, DataType x);//入栈
void StackPop(Stack* s); //出栈
DataType StackTop(Stack* s); //求栈顶元素
size_t StackSize(Stack* s);//栈的长度
int StackEmpty(Stack* s); //判断是否为空栈
源文件Stack.c
#include"Stack.h"
void StackInit(Stack* s)//栈的初始化
{
assert(s);
s->_array = NULL;
s->_base = 0;
s->_top = 0;
}
void StackPush(Stack* s, DataType x)//入栈
{
assert(s);
if (s->_top == s->_base)//栈为空或者栈空间不足时
{
size_t size = s->_base > 0 ? s->_base * 2 : 3;
s->_array = realloc(s->_array, size*sizeof(DataType));
if (!s->_array)
{
printf("malloc failure\n");
}
s->_base = size;//开辟的大小赋给栈底
}
s->_array[s->_top] = x;压栈
s->_top++;
}
void StackPop(Stack* s)//出栈
{
assert(s);
if (s->_top > 0)
{
--s->_top;
}
}
DataType StackTop(Stack* s)//求栈顶元素
{
assert(s);
return s->_array[s->_top - 1];
}
size_t StackSize(Stack* s)//栈的长度
{
assert(s);
return s->_top;
}
int StackEmpty(Stack* s) //判断是否为空栈
{
assert(s);
if (s->_top == 0)
{
return 0;
}
return 1;
}
迷宫问题头文件Maze.h
#pragma once
#include"Stack.h"
#define COL 6
#define ROW 6
Stack Shortpath;
void MazePrint();//迷宫打印
int CheckIsAccess(Pos pos);//检查迷宫中坐标是否可通
int MazeGetPath(Pos entry);//迭代法查找通路
void MazeGetPathR(Pos entry);//递归法查找通路
int CheckIsShortAccess(Pos next,Pos pos);//检查迷宫中多条通路坐标是否可通
void MazeGetShortPath(Pos entry,Stack *path);//多条通路找最短通路(有环形通路)
源文件Maze.c
#include"Maze.h"
int maze[ROW][COL] =
{
{ 0, 0, 0, 0, 0, 0 },
{ 0, 1, 1, 1, 0, 0 },
{ 0, 1, 0, 1, 0, 0 },
{ 0, 1, 1, 1, 0, 0 },
{ 0, 1, 0, 1, 1, 1 },
{ 0, 1, 0, 0, 0, 0 },
};
void MazePrint()//迷宫打印
{
size_t row = 0;
size_t col = 0;
for (row; row < ROW; row++)
{
col = 0;
for (col; col < COL; col++)
{
printf("%-2d ", maze[row][col]);
}
printf("\n");
}
printf("\n");
}
int CheckIsAccess(Pos pos)//检查迷宫中坐标是否可通
{
if (pos._col >= 0 && pos._col < COL && pos._row >= 0 && pos._row < ROW && maze[pos._row][pos._col] == 1)
{
return 1;
}
else
{
return 0;
}
}
int MazeGetPath(Pos entry)//迭代法查找通路
{
Stack s;
Pos cur = entry;
Pos next;
StackInit(&s);//栈初始化
StackPush(&s,cur);//入栈
while (StackEmpty(&s))
{
cur = StackTop(&s);//返回栈顶的位置
next = cur;
maze[cur._row][cur._col] = 2;//做过的路程用2做标记
if (next._row == 0 || next._col == COL - 1)
{
printf("找到出口:(%d,%d)\n", next._row, next._col);
return 1;
}
//此时没有找到出口向它上下左右探索
next._row += 1;//上
if (CheckIsAccess(next))
{
StackPush(&s,next);
continue;
}
next = cur;
next._row -= 1;//下
if (CheckIsAccess(next))
{
StackPush(&s, next);
continue;
}
next = cur;
next._col += 1;//左
if (CheckIsAccess(next))
{
StackPush(&s, next);
continue;
}
next = cur;
next._col += 1;//右
if (CheckIsAccess(next))
{
StackPush(&s, next);
continue;
}
StackPop(&s);//遇到死胡同,出栈
}
return 0;
}
void MazeGetPathR(Pos entry)//递归法查找通路
{
Pos next = entry;
if (next._row == 0 || next._col == COL - 1)
{
printf("找到出口:(%d,%d)\n", next._row, next._col);
}
maze[next._row][next._col] = 2;
next._row += 1;//上
if (CheckIsAccess(next))
{
MazeGetPathR(next);
}
next = entry;
next._row -= 1;//下
if (CheckIsAccess(next))
{
MazeGetPathR(next);
}
next = entry;
next._col += 1;//右
if (CheckIsAccess(next))
{
MazeGetPathR(next);
}
next = entry;
next._col-= 1;//左
if (CheckIsAccess(next))
{
MazeGetPathR(next);
}
}
int CheckIsShortAccess(Pos next, Pos cur)//检查迷宫中坐标是否可通
{
if (next._col >= 0 && next._col < COL &&
next._row >= 0 && next._row < ROW &&
(maze[next._row][next._col] == 1||
maze[next._row][next._col] > maze[cur._row][cur._col]))
{
return 1;
}
else
{
return 0;
}
}
void MazeGetShortPath(Pos entry, Stack* path)//多条通路找最短通路(有环形通路)
{
Pos cur = entry;
Pos next = cur;
if (0 == StackEmpty(path))
{
maze[next._row][next._col] = 2;//出口开始1值变为2
}
else
{
Pos prev = StackTop(path);
maze[next._row][next._col] = maze[prev._row][prev._col] + 1;//随后每走一步加1
}
StackPush(path,next);
if ( COL - 1 == next._col||0 == next._row)
{
printf("找到出口:(%d,%d)\n", next._row, next._col);
MazePrint();
if (!StackEmpty(&Shortpath) || (StackSize(path) < StackSize(&Shortpath)))//最开始先设定一个最短路径置为空,找到第一个出口后将第一个出来路径给最短路径,找到第二个出口时,如果第二个出口路径小于最短路径,则释放掉当前最短路径,把现在小的路径赋给最短路径
{
if (Shortpath._array)
{
free(Shortpath._array);
}
Shortpath._array = (DataType*)malloc(sizeof(DataType)*path->_top);//开辟当前最短路径的长度
memcpy(Shortpath._array, path->_array, sizeof(DataType)*path->_top);//按字节复制
Shortpath._top = path->_top;
Shortpath._base = path->_base;
}
}
next = entry;
next._row += 1;//上
if (CheckIsShortAccess(next, cur))
{
MazeGetShortPath(next,path);
}
next = entry;
next._row -= 1;//下
if (CheckIsShortAccess(next, cur))
{
MazeGetShortPath(next,path);
}
next = entry;
next._col += 1;//右
if (CheckIsShortAccess(next, cur))
{
MazeGetShortPath(next,path);
}
next = entry;
next._col -= 1;//左
if (CheckIsShortAccess(next, cur))
{
MazeGetShortPath(next,path);
}
StackPop(path);//遇到不通道路回溯出栈
}
测试迭代法和递归查找通路代码:
#include"Maze.h"
Test1()
{
Pos entry = { 5, 1 };
Stack path;
StackInit(&path);
MazePrint();
MazeGetPath(entry);
MazePrint();
}
测试结果
多条通路找最短通路(有环形通路)这个问题有点复杂,这里详细解释一下
这块代码是做标记用的,每走一步给当前步加1,最开始是2,随后逐渐增加找到第一个出口时迷宫显示如下图:
多条通路找最短路径测试代码
Test2()
{
Pos entry = { 5, 1 };
Stack path;
StackInit(&path);
StackInit(&Shortpath);
MazePrint();
MazeGetShortPath(entry,&path);
while (StackEmpty(&Shortpath))//打印最短路径坐标
{
printf("(%d,%d)<-", StackTop(&Shortpath)._row,StackTop(&Shortpath)._col);
StackPop(&Shortpath);
}
printf("entry\n");
}
int main()
{
//Test1();
Test2();
system("pause");
return 0;
}
测试结果: