C++ 数据结构算法 学习笔记(18) - 栈的企业级应用案例(2)
迷宫求解
找迷宫通路需要使用回溯法,找迷宫通路是对回溯法的一个很好的应用,实现回溯的过程用到数据结构—栈!
回溯法:对一个包括有很多个结点,每个结点有若干个搜索分支的问题,把原问题分解为若干个子问题求解的 算法;当搜索到某个结点发现无法再继续搜索下去时,就让搜索过程回溯(回退)到该节点的前一个结点,继续 搜索该节点外的其他尚未搜索的分支;如果发现该结点无法再搜索下去,就让搜索过程回溯到这个结点的前一 结点继续这样的搜索过程;这样的搜索过程一直进行到搜索到问题的解或者搜索完了全部可搜索分支没有解存 在为止。
完整代码
完整代码实现:
Maze.h
#ifndef __MAZE_H__
#define __MAZE_H__
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef struct _Position {
int _x;
int _y;
}Position;
#define MaxSize 128
typedef Position ElemType;
typedef struct _SqStack {
ElemType* base;
ElemType* top;
}SqStack;
bool InitStack(SqStack& S)
{
S.base = new ElemType[MaxSize];
if (!S.base)
return false;
S.top = S.base;
return true;
}
bool PushStack(SqStack& S, ElemType e)
{
if (S.top - S.base == MaxSize)
return false;
*(S.top++) = e;
return true;
}
bool PopStack(SqStack& S, ElemType& e)
{
if (S.base == S.top) {
return false;
}
e = *(--S.top);
return true;
}
ElemType* GetTop(SqStack& S)
{
if (S.top != S.base) {
return S.top - 1;
}
else {
return NULL;
}
}
int GetSize(SqStack& S) {
return (S.top - S.base);
}
bool IsEmpty(SqStack& S) {
if (S.top == S.base) {
return true;
}
else {
return false;
}
}
void DestoryStack(SqStack& S) {
if (S.base) {
free(S.base);
S.base = NULL;
S.top = NULL;
}
}
#endif
Maze.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "43.Maze.h"
#define ROW 6
#define COL 6
typedef struct _Maze {
int map[ROW][COL];
}Maze;
void InitMaze(Maze* m, int map[ROW][COL])
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
m->map[i][j] = map[i][j];
}
}
}
void PrintMaze(Maze* m)
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
printf("%d ", m->map[i][j]);
}
printf("\n");
}
printf("\n");
}
int IsValidEnter(Maze * m, Position cur)
{
assert(m);
if ((cur._x == 0 || cur._x == ROW - 1)
|| (cur._y == 0 || cur._y == COL - 1)
&& (m->map[cur._x][cur._y] == 1))
return 1;
else
return 0;
}
int IsNextPass(Maze* m, Position cur, Position next)
{
assert(m);
if (((next._x == cur._x) && ((next._y == cur._y + 1) || (next._y ==
cur._y - 1)))
|| ((next._y == cur._y) && ((next._x == cur._x + 1) || (next._x ==
cur._x - 1)))) {
if (((next._x >= 0 && next._x < ROW) && (next._y >= 0 && next._y
< COL))
&& (m->map[next._x][next._y] == 1)) {
return 1;
}
}
return 0;
}
int IsValidExit(Maze* m, Position cur, Position enter)
{
assert(m);
if ((cur._x != enter._x || cur._y != enter._y)
&& ((cur._x == 0 || cur._x == ROW - 1)
|| (cur._y == 0 || cur._y == COL - 1)))
{
return 1;
}
else
return 0;
}
int PassMaze(Maze* m, Position enter, SqStack* s)
{
assert(m && IsValidEnter(m, enter) == 1);
Position cur = enter;
Position next;
PushStack(*s, cur);
m->map[cur._x][cur._y] = 2;
//PrintMaze(m);
while (!IsEmpty(*s)) {
cur = *GetTop(*s);
//printf("cur: %d %d\n",cur._x, cur._y);
if (IsValidExit(m, cur, enter) == 1)
return 1;
next = cur;
next._y = cur._y - 1;
if (IsNextPass(m, cur, next) == 1)
{
PushStack(*s, next);
m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;
//PrintMaze(m);
continue;
}
next = cur;
next._x = cur._x - 1;
if (IsNextPass(m, cur, next) == 1)
{
PushStack(*s, next);
m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;
//PrintMaze(m);
continue;
}
next = cur;
next._y = cur._y + 1;
if (IsNextPass(m, cur, next) == 1)
{
PushStack(*s, next);
m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;
//PrintMaze(m);
continue;
}
next = cur;
next._x = cur._x + 1;
if (IsNextPass(m, cur, next) == 1)
{
PushStack(*s, next);
m->map[next._x][next._y] = m->map[cur._x][cur._y] + 1;
//PrintMaze(m);
continue;
}
Position tmp;
PopStack(*s, tmp);
}
return 0;
}
int main()
{
int map[ROW][COL] = {
0,0,1,0,0,0,
0,0,1,1,1,0,
0,0,1,0,0,0,
0,1,1,1,1,0,
0,0,1,0,1,0,
0,0,0,0,1,0
};
Maze m;
Position enter;
enter._x = 0;
enter._y = 2;
InitMaze(&m, map);
PrintMaze(&m);
//system("pause");
//exit(0);
SqStack s;
InitStack(s);
int ret = PassMaze(&m, enter, &s);
if (ret) {
printf("Congraturation! You have found the exit!\n");
}
else {
printf("I am not stupid, but there is really no exit!\n");
}
PrintMaze(&m);
system("pause");
return 0;
}